Previous Thread
Next Thread
Print Thread
Rate Thread
#17494 09/27/06 12:29 PM
Joined: Sep 2006
Posts: 3
H
Junior Member
Junior Member
H Offline
Joined: Sep 2006
Posts: 3
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);
	}
}

Sponsored Links
▼ Sponsored Links ▼ ▲ Sponsored Links ▲
Joined: Oct 2006
Posts: 11
V
UGN Newbie
UGN Newbie
V Offline
Joined: Oct 2006
Posts: 11
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----
Joined: Oct 2006
Posts: 11
V
UGN Newbie
UGN Newbie
V Offline
Joined: Oct 2006
Posts: 11
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.

Last edited by Gizmo; 11/09/06 04:33 AM.

Hacking is an art that is to be mastered to get the rewards from it!
----Virtual Ranger - VR----

Link Copied to Clipboard
Member Spotlight
None yet
Forum Statistics
Forums41
Topics33,840
Posts68,858
Members2,176
Most Online3,253
Jan 13th, 2020
Latest Postings
Top Posters
UGN Security 41,392
Gremelin 7,203
§intå× 3,255
SilentRage 1,273
Ice 1,146
pergesu 1,136
Infinite 1,041
jonconley 955
Girlie 908
unreal 860
Top Likes Received
Ghost 2
Cyrez 1
Girlie 1
unreal 1
Crime 1
Powered by UBB.threads™ PHP Forum Software 8.0.0