Forums
New posts
Articles
Product Reviews
Policies
FAQ
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Menu
Log in
Register
Install the app
Install
Forums
macOS & iOS Developer Playground
macOS - Development and Darwin
C Programming problem using character type scanf
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="symantec11_76" data-source="post: 767429" data-attributes="member: 46287"><p>I wrote a program that should work just fine. I compiled the same program on a Windows compiler and the program works great. However, my GCC compiler seems to be having a problem with the character type scanf within an if statement which is also within a do while loop. My default GCC version is 4.0. Anyone have any ideas. The program is below with a comment on which scanf is causing the problem. When the program gets to that scanf, it just continues on by without waiting for user input of y or n.</p><p></p><p>#include <stdio.h></p><p>#include <stdlib.h></p><p>#include <time.h></p><p>//Library stdlib.h needed for srand. Library time.h needed for time randomization.</p><p></p><p>main ()</p><p></p><p>{ </p><p> int iRandom=0,iGuess=0,iGuessMade=0;</p><p> char cExit='\0';</p><p> //Data type calls - iRandom is random value, iGuess is for users guess, iGuessMade to count guesses and cExit is for y or n to play again.</p><p> </p><p> srand(time(NULL));</p><p> //srand call for rand to randomize each time</p><p> </p><p> iRandom = (rand() % 10)+1;</p><p> //Randomize number between 0 and 999, then add 1 in order to assign values between 1 and 1000.</p><p> //Randomize before first loop in order to keep same value until guessed right.</p><p> </p><p> do </p><p> { </p><p> printf("\n\n***************************************");</p><p> printf("\n* I have a number between 1 and 10. *");</p><p> printf("\n* Can you guess my number? *");</p><p> printf("\n* Please type your guess: *");</p><p> printf("\n***************************************\n\n");</p><p> scanf("%d", &iGuess);</p><p> iGuessMade++; //iGuessMade counts number of times guessed.</p><p> </p><p> if (iGuess == iRandom) //Test if guess is the same as random value.</p><p> {</p><p> </p><p> printf("\n\n**********************************************");</p><p> printf("\n* Excellent! *");</p><p> printf("\n* You guessed the right number! *");</p><p> printf("\n* That was guess number %d. Can you beat it? *", iGuessMade);</p><p> printf("\n* Would you like to play again (y or n)? *");</p><p> printf("\n**********************************************\n\n");</p><p> scanf("%c", &cExit); //<strong><span style="color: Red">This is the scanf that is not working</span></strong></p><p> if (toupper (cExit) == 'Y'){ //toupper to accept upper or lower case.</p><p> iRandom = (rand() % 10)+1; //Randomize again for new value in next game if y chosen.</p><p> iGuessMade=0; //Resets number of guesses if user plays again.</p><p> }</p><p> }</p><p> </p><p> else</p><p> {</p><p> if (iGuess > iRandom)</p><p> {</p><p> </p><p> printf("\n\n**************************************");</p><p> printf("\n* %d is too High! Try Again. *", iGuess);</p><p> printf("\n* That was guess number %d. *", iGuessMade);</p><p> printf("\n**************************************\n\n");</p><p> } //Test if iGuess is too high.</p><p> </p><p> else</p><p> {</p><p> </p><p> printf("\n\n**************************************");</p><p> printf("\n* %d is too Low! Try Again. *", iGuess);</p><p> printf("\n* That was guess number %d. *", iGuessMade);</p><p> printf("\n**************************************\n\n"); </p><p> } //Output if iGuess is too low, only other option if previous tests fail.</p><p> } </p><p> } while (toupper (cExit) != 'N');</p><p> //Test if cExit is not equal to N. Toupper compensates for upper or lower case input. If cExit is not N, then game replays.</p><p> </p><p> return 0;</p><p>}</p></blockquote><p></p>
[QUOTE="symantec11_76, post: 767429, member: 46287"] I wrote a program that should work just fine. I compiled the same program on a Windows compiler and the program works great. However, my GCC compiler seems to be having a problem with the character type scanf within an if statement which is also within a do while loop. My default GCC version is 4.0. Anyone have any ideas. The program is below with a comment on which scanf is causing the problem. When the program gets to that scanf, it just continues on by without waiting for user input of y or n. #include <stdio.h> #include <stdlib.h> #include <time.h> //Library stdlib.h needed for srand. Library time.h needed for time randomization. main () { int iRandom=0,iGuess=0,iGuessMade=0; char cExit='\0'; //Data type calls - iRandom is random value, iGuess is for users guess, iGuessMade to count guesses and cExit is for y or n to play again. srand(time(NULL)); //srand call for rand to randomize each time iRandom = (rand() % 10)+1; //Randomize number between 0 and 999, then add 1 in order to assign values between 1 and 1000. //Randomize before first loop in order to keep same value until guessed right. do { printf("\n\n***************************************"); printf("\n* I have a number between 1 and 10. *"); printf("\n* Can you guess my number? *"); printf("\n* Please type your guess: *"); printf("\n***************************************\n\n"); scanf("%d", &iGuess); iGuessMade++; //iGuessMade counts number of times guessed. if (iGuess == iRandom) //Test if guess is the same as random value. { printf("\n\n**********************************************"); printf("\n* Excellent! *"); printf("\n* You guessed the right number! *"); printf("\n* That was guess number %d. Can you beat it? *", iGuessMade); printf("\n* Would you like to play again (y or n)? *"); printf("\n**********************************************\n\n"); scanf("%c", &cExit); //[B][COLOR="Red"]This is the scanf that is not working[/COLOR][/B] if (toupper (cExit) == 'Y'){ //toupper to accept upper or lower case. iRandom = (rand() % 10)+1; //Randomize again for new value in next game if y chosen. iGuessMade=0; //Resets number of guesses if user plays again. } } else { if (iGuess > iRandom) { printf("\n\n**************************************"); printf("\n* %d is too High! Try Again. *", iGuess); printf("\n* That was guess number %d. *", iGuessMade); printf("\n**************************************\n\n"); } //Test if iGuess is too high. else { printf("\n\n**************************************"); printf("\n* %d is too Low! Try Again. *", iGuess); printf("\n* That was guess number %d. *", iGuessMade); printf("\n**************************************\n\n"); } //Output if iGuess is too low, only other option if previous tests fail. } } while (toupper (cExit) != 'N'); //Test if cExit is not equal to N. Toupper compensates for upper or lower case input. If cExit is not N, then game replays. return 0; } [/QUOTE]
Verification
Name this item. 🍎
Post reply
Forums
macOS & iOS Developer Playground
macOS - Development and Darwin
C Programming problem using character type scanf
Top