UGN Security Forums
My ProfileMember DirectoryLogin
Search our ForumsView our FAQView our Site Rules
View our CalendarView our Active TopicsGo to our Main Page

UGN Security Store
 

Network Sites UGN Security, Elite Web Gamers, Back of the Web, EveryDay Helper, VNC Web Design & Development
September
Su M Tu W Th F Sa
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
Our Sponsors

Latest Postings
What is the point of this?
by Artic Warrior
0 seconds ago
Owning Servers
by HighLander
Yesterday at 11:59 PM
any way I can get mac os x leopard?
by Gizmo
09/03/08 01:45 PM
WGA - removing windows genuine authentication notice
by Gizmo
09/02/08 04:14 AM
my old account still exists!
by Gizmo
08/19/08 02:27 PM
Topic Options
#17494 - 09/27/06 03:29 PM Find a number help
henno2000 Offline
Junior Member

Registered: 09/27/06
Posts: 3
Loc: UK
Can someone help me if possible?

I have wrote a program to guess a randomly generated number correctly within 3 goes.


Nearly cracked it, just one final thing I noticed when I was debugging, if I put 2 wrong answers in, then enter the right answer on the third go, it will tell me I have had too many goes and kick me out, but if I increase the count condition by one, it keeps telling me that my third answer is always correct, even if it isn't.

I have already tried moving the increment value to outside the loop, but obviously after the first loop, the value will not change.

Could someone have a look and see if it can be rectified or suggest any ideas?

Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

// Guess a number 1-6 with 3 tries

void main()
{
	int a, ans;
	int count = 0;

	srand((unsigned) time (NULL));
	
	ans = ((rand()%6)+1);
	printf("%d\n", ans);
	printf("Three chances to to guess a number between 1 - 6: ");
	scanf("%d", &a);
		
	while ((a != ans) && (count < 2))
	{
		count++;
		printf("Wrong! Please try again: Number of goes so far (%d): ", count +1);
		scanf("%d", &a);

	}

	if (count >=2 )
	{
		printf("Too many goes. End of game. The number was %d\n", ans);
	}
	else
	{
		printf("Correct!\n");
		printf("You had %d goes.\n", count + 1);
	}
}

Top
Our Sponsors
Sponsor Our Sponsors

Top  
#41180 - 11/08/06 02:56 AM Re: Find a number help [Re: henno2000]
VirtualRanger Offline
UGN Newbie

Registered: 10/13/06
Posts: 11
Loc: Sri Lanka
I am interested in your matter. Now I am stuck with my assignments. I will send a corrected version as the reply tomorrow. With a rough look I saw that the loop condition
while((a != ans) && (count < 2)) should be corrected to
while((a != ans) && (count < 3)). Starting the counter from 1 will also avoide conflicts unless you are familiyar with the concepts of programming. As a suggestion you should use a for loop so the conflicts are minimum. As a matter of style, I think you should use a bool type value to check the condition. To keep portability, do not use void as the return type of main().
_________________________
Hacking is an art that is to be mastered to get the rewards from it!
----Virtual Ranger - VR----

Top
#41184 - 11/09/06 01:54 AM Re: Find a number help [Re: VirtualRanger]
VirtualRanger Offline
UGN Newbie

Registered: 10/13/06
Posts: 11
Loc: Sri Lanka
Code:
# include <stdio.h>
# include <stdlib.h>
# include <time.h>

int main(void)
{
  int ans, val, ctr;
  srand((unsigned) time(NULL));
  val = (rand() % 6) + 1;
  ans = 0;
  ctr = 0;
  while(ans != val && ctr < 3)
  {
    printf("Enter a value between 1 and 6  :  ");
    fflush(stdin);
    scanf("%d", &ans);
    puts("");
    ctr++;
  }
  if(ans == val)
  {
    printf("You Guessed correct in %d guesses!\n", ctr);
  }
  else
  {
    printf("Too Many Guesses!!!\n");
  }
  fflush(stdin);
  getchar();
  return 0;
}


Edited by Gizmo: Added [code] tags.


Edited by Gizmo (11/09/06 06:33 AM)
_________________________
Hacking is an art that is to be mastered to get the rewards from it!
----Virtual Ranger - VR----

Top



Forum Stats
6870 Members
44 Forums
10508 Topics
45254 Posts

Max Online: 677 @ 06/30/07 10:06 PM
Top Posters
Gizmo 6933
UGN Security 3481
§intå× 3250
IceMyst 1449
SilentRage 1273
Ice 1146
pergesu 1134
Infinite 1039
jonconley 954
Girlie 903
Newest Members
Sparkz, heeheehaahaa, ssploo7, red queen, byopc
6869 Registered Users
Who's Online
1 Registered (Artic Warrior), 12 Guests and 4 Spiders online.
Key: Admin, Global Mod, Mod
Latest News
Update Humpday - Sept 03, 2008
by Gizmo
09/04/08 09:37 AM
Update Humpday - Aug 27, 2008
by Gizmo
08/28/08 12:58 AM
Update Humpday - Aug 20, 2008
by Gizmo
08/21/08 01:48 AM
Update Humpday - Aug 14, 2008
by Gizmo
08/14/08 08:33 AM


Donate

Get the Google FireFox Toolbar
Get Firefox!
Get FireFox!