Getchar() Problems

Discussion in 'Mac Programming' started by Halfmaster1, Sep 6, 2011.

  1. Halfmaster1 macrumors newbie

    Joined:
    Dec 28, 2010
    #1
    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.
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    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.
     
  3. willieva macrumors 6502

    Joined:
    Mar 12, 2010
    #3
    Plus you need to understand the difference between && and ||, and how statements like "if" and "while" evaluate what's inside of their parentheses.
     
  4. Halfmaster1 thread starter macrumors newbie

    Joined:
    Dec 28, 2010
    #4
    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.
     
  5. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #5
    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.
     
  6. SidBala macrumors 6502a

    Joined:
    Jun 27, 2010
    #6
    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.
     
  7. Guiyon macrumors 6502a

    Joined:
    Mar 19, 2008
    Location:
    North Shore, MA
    #7

    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!
     

Share This Page