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

Joined: Oct 2006
Posts: 11
V
UGN Newbie
Offline
UGN Newbie
V
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
Offline
UGN Newbie
V
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
Posts: 30
Joined: June 2002
Forum Statistics
Forums41
Topics33,840
Posts68,858
Average Daily Posts1
Members2,176
Most Online3,253
Jan 13th, 2020
Latest Postings
Where and how do you torrent?
by danni75 - 03/01/24 05:58 AM
Animation,
by JohanKaariainen - 08/15/19 01:18 AM
Blackbeard.....
by Gremelin - 10/03/18 07:02 PM
my old account still exists!
by Crime - 08/10/18 02:47 PM
Okay WTF?
by HenryMiring - 09/27/17 01:45 AM
The History Thread...
by Gremelin - 08/11/17 12:11 PM
My friend NEEDS your HELP!
by Lena01 - 07/21/17 12:06 AM
I'm having fun with this guy.
by gabithompson730 - 07/20/17 01:50 AM
I want to upgrade my phone
by gabithompson730 - 07/20/17 01:49 AM
Doom 3
by Cyrez - 09/11/14 08:58 PM
Amazon Gift Card Generator/KeyGen?te
by Gecko666 - 08/22/14 09:21 AM
AIM scene 99-03
by lavos - 09/02/13 08:06 AM
Planetside 2
by Crime - 03/04/13 07:10 AM
Beta Testers Wanted
by Crime - 03/04/13 06:55 AM
Hello Everyone
by Gremelin - 02/12/12 06:01 PM
Tracfone ESN Generator
by Zanvin Green - 01/18/12 01:31 PM
Python 3 issue
by Testing - 12/17/11 09:28 PM
tracfone airtime
by Drache86 - 07/30/11 03:37 AM
Backdoors and the Infinite
by ZeroCoolStar - 07/10/11 03:52 AM
HackThisZIne #12 Releaseed!
by Pipat2 - 04/28/11 09:20 PM
gang wars? l33t-wars?
by Gremelin - 04/28/11 05:56 AM
Consolidate Forums
by diggin2deep - 04/21/11 10:02 AM
LAN Hacking Noob
by Gremelin - 03/12/11 12:42 AM
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
Crime 1
Ice 1
Dartur 1
Cyrez 1
Powered by UBB.threads™ PHP Forum Software 7.7.5