New To Mac-Forums?

Welcome to our community! Join the discussion today by registering your FREE account. If you have any problems with the registration process, please contact us!

Get your questions answered by community gurus Advice and insight from world-class Apple enthusiasts Exclusive access to members-only contests, giveaways and deals

Join today!

 
Start a Discussion
 

Mac-Forums Brief

Subscribe to Mac-Forums Brief to receive special offers from Mac-Forums partners and sponsors

Join the conversation RSS
OS X - Development and Darwin Discussion and questions about development for Mac OS X.

C Programming problem using character type scanf


Post Reply New Thread Subscribe

 
Thread Tools
symantec11_76

 
Member Since: Jan 18, 2008
Posts: 5
symantec11_76 is on a distinguished road

symantec11_76 is offline
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); //This is the scanf that is not working
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;
}

Macbook
OS X 10.5.5
XCode 3.1.2
QUOTE Thanks
rashiki

 
Member Since: Dec 26, 2008
Posts: 5
rashiki is on a distinguished road

rashiki is offline
Hi,

I am running into the same problem. I am starting out writing simple console programs using a cocoa project with XCode 2.5 and have been running into the same problem as you have with character variables and "while" looping. Funny thing is that if I do a "while" loop, if I set the "char" variable to 'y' for "yes" outside the loop first (so that it enters), and then use "scanf" inside the "while" loop, it will actually ask me for the letter, but just that first time. If I enter 'y' to do the loop again, it totally blows by the "scanf" the next time and then exits the "while" loop.

Have you figured anything out yet? I used an integer variable instead and used by conditional test with '0' or '1' and it doesn't blow by the "scanf" this way. Only with a character variable.

Ryan
QUOTE Thanks
symantec11_76

 
Member Since: Jan 18, 2008
Posts: 5
symantec11_76 is on a distinguished road

symantec11_76 is offline
I determined the problem was that there was remnants in the scanf before getting to my character scanf. Therefore, the scanf just went on through. The solution was to make the scanf skip any white space by adding a space before the scan. Here is my working program below.



#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>
//Library stdlib.h needed for srand. Library time.h needed for time randomization.
//Library ctype.h needed for toupper upper case adjustment.

int 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() % 1000)+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 1000. *");
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); //Space added before %c to skip white space before next character input. GCC & Miracle C compatible.
if (toupper (cExit) == 'Y'){ //toupper to accept upper or lower case.
iRandom = (rand() % 1000)+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;
}

Macbook
OS X 10.5.5
XCode 3.1.2
QUOTE Thanks
xstep

 
xstep's Avatar
 
Member Since: Jun 25, 2005
Location: On the road
Posts: 3,231
xstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to all
Mac Specs: 2011 MBP, i7, 16GB RAM, MBP 2.16Ghz Core Duo, 2GB ram, Dual 867Mhz MDD, 1.75GB ram, ATI 9800 Pro vid

xstep is offline
The net is full of stuff about scanf. This one is interesting and read the last comment on it.

CameraTime - Time lapse photography for novice and advanced users.

When asking questions, post the version of your software. You'll receive better answers.

Please post your results to the thread as it is good feedback.
QUOTE Thanks

Post Reply New Thread Subscribe


« How to support Disk Arbitration Framework in 10.3.9 | XCode and VI »
Thread Tools

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread
Thread Starter
Forum
Replies
Last Post
Movable Type Problem Quest Web Design and Hosting 8 12-18-2007 10:21 PM
Adobe Programs, Type & File Saving for Print kameelean OS X - Apps and Games 0 07-09-2007 03:39 PM
Problem printing and installing software w/10.2.8 Larry Joubert OS X - Operating System 0 01-14-2007 01:39 PM
Screen saver password problem Biturbo V12 AMG Apple Notebooks 2 12-29-2005 05:10 AM
osx install blue screen hell David123 OS X - Operating System 10 08-22-2004 04:40 PM

All times are GMT -4. The time now is 03:12 AM.

Powered by vBulletin
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
X

Welcome to Mac-Forums.com

Create your username to jump into the discussion!

New members like you have made this community the ultimate source for your Mac since 2003!


(4 digit year)

Already a member?