This program isn't working, it doesn't get the character, just exits.
________________________________________________________________________________________
//Program Description: The program chooses a random integer to be guessed by the user.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int getrandomint();
main()
{
-- - - - - - int number; - - - - - // random number
-- - - - - - int userin; - - - // number input by user
-- - - - - - srand(time(NULL));
-- - - - - - printf("I have a number between 1 and 1000.\n");
-- - - - - - printf("Can you guess my number?\n");
-- - - - - - do - - - - - - - // decides, at the end, wether to loop back or not
-- - - - - - {
-- - - - - - - - - - - - - number = getrandomint();
-- - - - - - - - - - - - - do - - - - - // loops until the user guesses the correct number
-- - - - - - - - - - - - - {
-- - - - - - - - - - - - - - - - - - - printf("\nEnter guess: ");
-- - - - - - - - - - - - - - - - - - - scanf("%d", &userin);
-- - - - - - - - - - - - - - - - - - - if(userin < number) - // compares random number and number input by user
-- - - - - - - - - - - - - - - - - - - - - - - - - printf("\nToo low. Try again.");
-- - - - - - - - - - - - - - - - - - - else if(userin > number)
-- - - - - - - - - - - - - - - - - - - - - - - - - printf("\nToo high. Try again.");
-- - - - - - - - - - - - - - - - - - - else
-- - - - - - - - - - - - - - - - - - - - - - - - - printf("\nExcellent! You guessed the number!\n");
-- - - - - - - - - - - - - }
-- - - - - - - - - - - - - while(userin != number);
-- - - - - - - - - - - - - printf("\nWould you like to try again (press y or n)? %c", getchar());
-- - - - - - }
-- - - - - - while(getchar() == 'y');
}
int getrandomint()
{
-- - - - - - int x;
-- - - - - - x = 1 + rand() % 1000;
-- - - - - - return x;
}
________________________________________________________________________________________
It does get the character however you you change the printf before getchar to this:
printf("\nWould you like to try again (press y or n)? %c", getchar());
Anyone know what the problem could be?
________________________________________________________________________________________
//Program Description: The program chooses a random integer to be guessed by the user.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int getrandomint();
main()
{
-- - - - - - int number; - - - - - // random number
-- - - - - - int userin; - - - // number input by user
-- - - - - - srand(time(NULL));
-- - - - - - printf("I have a number between 1 and 1000.\n");
-- - - - - - printf("Can you guess my number?\n");
-- - - - - - do - - - - - - - // decides, at the end, wether to loop back or not
-- - - - - - {
-- - - - - - - - - - - - - number = getrandomint();
-- - - - - - - - - - - - - do - - - - - // loops until the user guesses the correct number
-- - - - - - - - - - - - - {
-- - - - - - - - - - - - - - - - - - - printf("\nEnter guess: ");
-- - - - - - - - - - - - - - - - - - - scanf("%d", &userin);
-- - - - - - - - - - - - - - - - - - - if(userin < number) - // compares random number and number input by user
-- - - - - - - - - - - - - - - - - - - - - - - - - printf("\nToo low. Try again.");
-- - - - - - - - - - - - - - - - - - - else if(userin > number)
-- - - - - - - - - - - - - - - - - - - - - - - - - printf("\nToo high. Try again.");
-- - - - - - - - - - - - - - - - - - - else
-- - - - - - - - - - - - - - - - - - - - - - - - - printf("\nExcellent! You guessed the number!\n");
-- - - - - - - - - - - - - }
-- - - - - - - - - - - - - while(userin != number);
-- - - - - - - - - - - - - printf("\nWould you like to try again (press y or n)? %c", getchar());
-- - - - - - }
-- - - - - - while(getchar() == 'y');
}
int getrandomint()
{
-- - - - - - int x;
-- - - - - - x = 1 + rand() % 1000;
-- - - - - - return x;
}
________________________________________________________________________________________
It does get the character however you you change the printf before getchar to this:
printf("\nWould you like to try again (press y or n)? %c", getchar());
Anyone know what the problem could be?