Thread: small problem

  1. #1
    ucfmustang
    Guest

    small problem

    hey can anyone tell me why when the number entered equals the random number, the messages print twice up until the scanf for the character variable g?
    Code:
    /* Name: Jesse Ranto
     * USER ID: je817659
     * Assignment Number: 3
     * Title: HI-LO Guessing Game
     * Date: 2/11/03
     * Description of the program: Write a C program that plays the HI-LO Guessing game with numbers. The    program should pick a random number between 1 and 100 (inclusive), then repeatedly prompt the user to    guess the number. On each guess, report to the user whether the guess was high or low. Continue       accepting guesses until the user guesses correctly or chooses to quit. Count the number of guesses and    if the user wants to play again. Continue playing games until the user chooses to stop.
    */
    
    #include <stdio.h> //for standard input output
    #include <stdlib.h> //for using random functions
    #include <time.h> //for using time function
    #include <math.h>
    
    int main()
    
    {
    	char g;
    	int x, y; /*x is the variable for the random number; y is the value for the users entry */
    	int c = 1; /*counter*/
    	srand(time(NULL)); /*turns on the random generator*/
    	x=rand()%100;
    	printf("I have a number between 1 and 100 \n");
    	printf("Can you guess my number: ");
    	scanf("%d", &y);
    	while (y != 999) {
    		if (y > x) { /*Code runs when guess is higher*/
    			printf("Too high. Try again. ");
    			printf("Please enter your guess or 999 to quit: ");
    			scanf("%d", &y);
    			c = c++;
    				}
    		if (y < x) { /*Code runs when guess is lower*/
    			printf("Too low. Try again. ");
    			printf("Please enter your guess or 999 to quit: ");
    			scanf("%d", &y);
    			c = c++;
    		 		}
    		if (y == x) { /*Code runs when guess equals random number */
    			printf("You have found the number. \n");
    			printf("You have taken %d guesses. \n", c);
    			printf("Would you like to play again? [Y/N] ");
    			scanf("%c", &g);
    			if (g == 'n' || g == 'N') {
    				printf("Thanks for playing. Goodbye. \n");
    				return 0;
    				                  }
    			else if (g == 'y' || g == 'Y') {
    				x = rand()%100;
    				c = 1;
    				printf("The number has been changed. \n Enter your new guess: ");
    				scanf("%d", &y);
    						}
                                   }
    			}
    		printf("The number was %d. \n", x); /*Code runs when user enters 999 */
    		printf("Thanks for playing. Goodbye \n");
    		return 0;
    			
    }


    CODE TAGS added by Hammer

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>c = c++;
    This is wrong. You want just this
    >>c++;

    The program goes through an extra iteration of the loop because there's a newline character in the buffer when you do this for the first time:
    >>scanf("%c", &g);

    Try adding this just before that line:
    >>while (getchar() != '\n');
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Small Problem with double and integer adding?
    By Nathan the noob in forum C++ Programming
    Replies: 5
    Last Post: 03-28-2009, 04:16 PM
  2. Visual C++ small problem
    By gadu in forum C++ Programming
    Replies: 0
    Last Post: 03-10-2009, 10:45 PM
  3. Small problem with this array...
    By Merholtz in forum C Programming
    Replies: 7
    Last Post: 11-03-2008, 04:16 PM
  4. Help with a small problem (beginner)
    By piffo in forum C Programming
    Replies: 13
    Last Post: 09-29-2008, 04:37 PM
  5. Need Big Solution For Small Problem
    By GrNxxDaY in forum C++ Programming
    Replies: 8
    Last Post: 08-01-2002, 03:23 AM