Thread: Good single character option function

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    25

    Good single character option function

    Hi,

    I want to have the following in my program:

    "Encrypt message ? (Y / N):"

    If the user enters Y then a boolean variable is set to true and if they enter N a boolean variable is set to false.

    Any other character should result in the message above being displayed again.

    I've tried a number of different methods such as using getchar() and fgets(), but I can't find an elegant solution.

    Does anyone have any ideas?

    Thanks.

  2. #2
    Registered User
    Join Date
    Nov 2004
    Location
    USA
    Posts
    516
    Code:
    do
    {
       displaymessage();
       char c;
       scanf("%c",&c);
    }while(!(c=='N' || c=='Y'));
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    25
    Is there a way to achieve the same thing without using scanf?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You could use getchar() too, I suppose.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    25
    char key;

    Code:
    do
    {
       printf("Encrypt (Y / N)");
       key = getchar();
    
    }while(!(key=='N' || key=='Y'));
    results in the message being printed loads of times when you enter anything other than 'Y' or 'N'

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    results in the message being printed loads of times when you enter anything other than 'Y' or 'N'
    You need to discard the newline left in the buffer with an extra getchar(), e.g.,
    Code:
    do
    {
        printf("Encrypt (Y / N)");
        key = getchar();
        getchar();
    } while (!(key == 'N' || key == 'Y'));
    Note that getchar() returns an int, not a char, though in this case it probably does not matter.

    EDIT:
    Actually, you should do more than discard the newline left in the buffer. You should discard all that is left in the buffer. For an example of how to do that, read How do I flush the input buffer?
    Last edited by laserlight; 03-15-2008 at 02:23 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Mar 2008
    Posts
    25
    I've tried this:

    Code:
    do
    {
        printf("Encrypt (Y / N)");
        while ((key = getchar()) != '\n' && key != EOF);
        getchar();
    } while (!(key == 'N' || key == 'Y'));
    But it doesn't seem to work.

    Any ideas?

    Thanks.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Do you mean to have an infinite, if empty, while loop? Edit: sorry, thought that the getchar below was in the loop -- just never mind.
    Last edited by tabstop; 03-15-2008 at 05:25 PM.

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    I'd reccomend using:
    Code:
    char key[2]={0};
    do{
      displaymessage();
      scanf("&#37;1s",key);
    }while(key[0]!='N' || key[0] != 'Y');
    That'll take care of the pesky whitespace characters in the buffer. However, that'll still act weird if the user enters a word like "ORANGE", instead of a single character. It will read in the and discard characters up to the 'N', displaying the message 4 times (3 times after ORANGE is typed). Then it will proceed to the next instruction with "GE" still in the buffer.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  10. #10
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    How "doesn't it seem to work", be more specific.

    key is a char? right? Why are you comparing it in your loop with EOF?

    Code:
    do
    {
        printf("Encrypt (Y / N)");
        while ((key = getchar()) != '\n' && key != EOF);
        getchar();
    } while (!(key == 'N' || key == 'Y'));

    This getchar(); call is not needed, unless you want your user to press enter twice after inputting the number ofcourse?
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  11. #11
    Registered User
    Join Date
    Mar 2008
    Posts
    25
    Quote Originally Posted by Neo1 View Post
    How "doesn't it seem to work", be more specific.

    key is a char? right? Why are you comparing it in your loop with EOF?

    Code:
    do
    {
        printf("Encrypt (Y / N)");
        while ((key = getchar()) != '\n' && key != EOF);
        getchar();
    } while (!(key == 'N' || key == 'Y'));

    This getchar(); call is not needed, unless you want your user to press enter twice after inputting the number ofcourse?
    Sorry, that getchar() was meant to be removed. It actually looks like:

    Code:
    do
    {
        printf("Encrypt (Y / N)");
        while ((key = getchar()) != '\n' && key != EOF);
       
    } while (!(key == 'N' || key == 'Y'));

  12. #12
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    eponymous: You are flushing the input buffer but not getting a character for key now. I imagine you want something like this instead:
    Code:
    do
    {
        printf("Encrypt (Y / N)");
        while ((key = getchar()) != '\n' && key != EOF);
        key = getchar();
    } while (!(key == 'N' || key == 'Y'));
    It may also be a good idea to convert to uppercase too.

  13. #13
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    If your compiler/platform supports the <conio.h> header, why not just use getch() or getche()? There's other solutions listed in the FAQ section.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Replies: 5
    Last Post: 02-08-2003, 07:42 PM