Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Halfmaster1

macrumors newbie
Original poster
Dec 28, 2010
19
0
I am using C, and tried this code to get the user to decide what to do:

Code:
while (choice!='a'&&choice!='b'&&choice!='c') {
		printf("Please enter a for option 1, b for option 2, or c for option 3.\n");
		choice=getchar();
		printf("\b");
	}

But when I type, say, d in, then the program is supposed to just ask you again, but instead, it asks you twice. That's why I add \b, but it doesn't help. Also, when I type multiple letters, then I end up with it asking to choose an option a lot more.

So, how could I fix this? A certain part of my code accepts anything, but it ends up getting input from the last question.
 
In the posted code, you're not accounting for the fact that you're actually pressing the 'd' key followed by the RETURN or ENTER key. That's two key presses, so two characters input.

If you print the decimal (or hex or octal) number of the value of choice, you should get a better understanding of what's actually happening.

Trying to printf() a backspace does nothing useful. Backspace is an output formatting character. It has no effect on what's being read from the input.
 
Plus you need to understand the difference between && and ||, and how statements like "if" and "while" evaluate what's inside of their parentheses.
 
Plus you need to understand the difference between && and ||, and how statements like "if" and "while" evaluate what's inside of their parentheses.

I get those completely, I used to (and still use) GML, but the only free version was for windows, and I don't like windows, so I started C. I just don't get the input.

I also understand the value of different chars, a bit, I just don't know what to code.



Just came up with an idea while typing this, going to try it.

I tried this:

Code:
while (choice!='a'&&choice!='b'&&choice!='c') {
		printf("Please enter a for option 1, b for option 2, or c for option 3.\n");

                choice=0;
		
		while (!(choice<'z'||choice>'a'||choice<'Z'||choice>'A')){
			choice=getchar();
		}
                }


But now it asks the question and doesn't even give you time to answer, just asks it again.




But it just

Edit:

keeps asking it over and over.


Forgot to finish my sentence.
 
Analyze the condition logic of your inner loop. In particular, step through exactly what happens when choice is 0 on entry to the loop. If you think it should work, post your actual written-down steps.

You also might want to look at library function isalpha(), and consider a do/while loop.

Or look at function fgets(), which reads a line, and consider how you'd parse the line to return only the first char. Break it down. Consider a function that does all the stuff you do now as an inline inner loop.
 
That boolean logic looks wrong.

Try to use positive logic so it is easier to read (as in try to not use !).

Code:
//            [B]Outside lowercase                Outside uppercase[/B]
while((choice < 'a' || choice > 'z') && (choice < 'A' || choice > 'Z'))
// flush out the chars here

Boolean logic is very easy to get wrong. I hope the above expression is correct.
 
That boolean logic looks wrong.

Try to use positive logic so it is easier to read (as in try to not use !).

Code:
//            [B]Outside lowercase                Outside uppercase[/B]
while((choice < 'a' || choice > 'z') && (choice < 'A' || choice > 'Z'))
// flush out the chars here

Boolean logic is very easy to get wrong. I hope the above expression is correct.


If he includes ctype.h, he should be able to use isalpha which will return non-zero if the character is an alphabetical character. Pop open a terminal and type 'man isalpha' for more info on it. Looks like Chown beat me to this though

Edit: Missed Chown's post!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.