PDA

View Full Version : how can i do: if (inputChar != 'Y') in C?


singularity2006
02-25-2004, 03:42 AM
how can i do: if (inputChar != 'Y') in C?

I have to write a prompt that asks the following:

"Do you wish to continue (y/n)?:"

And I'm supposed to scanf the input of 'y' or 'n' but I have no idea how to write the if then comparison statement to continue the loop or exit the program. Ideas?

This is all I have so far:


printf("\nWould you like to play again (Y/N):");
scanf(" %c", &again_prompt);
if (again_prompt == 'y')
{
go_indicator = 1;
}
else if (again_prompt == 'n');
{
go_indicator = 0;
}


But from my observations, that method definitely does not work. So how do I do it?

iC4
02-25-2004, 07:25 AM
why not like this?


char quit = 'y';
while(quit!='n' && quit!='N')
{
//do game stuff here
printf("\nWould you like to play again (Y/N):");
scanf(" %c", &quit);
}
//exit here

singularity2006
02-25-2004, 07:31 AM
aaahhh, that works too! Yeah, thanks. I figured it out. It wasn't an issue of the If statement but an issue of the while loop it was trying to break from... boy, so strange. Here's what I had going:

go_indicator = 1

Do loop

if then condition to change go_indicator to 0

while go_indicator = 1

For some reason, the if then there wouldn't change the value of the go_indicator. I just ended up leaving it as is and just set a straight break; instead of trying to change the indicator to naturally break from the loop..... ah, so burned out. Stupid lab.... so burned and blind. I'm tempted to print it on binder paper just to tick off the teacher... :surprised

Hugh
02-25-2004, 07:46 AM
iC4 - a little comment on your code...

If someone were to take a look at your code later they might (as I did briefly) get slightly confused - you've got a variable 'quit' which is either 'y' or 'n' - however when quit == 'y' you want to keep playing and when quit == 'n' you want to quit...

I believe that a very quick glance at that kind of code block should tell the reader what it does - yours is a little confusing....

iC4
02-25-2004, 08:15 AM
hehe...just recognized this...yep you are right :)

was just a little quick test......;)

CGTalk Moderation
01-17-2006, 02:00 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.