Thread: need help with getchar

  1. #1
    Registered User
    Join Date
    Feb 2004
    Posts
    8

    need help with getchar

    in order to start my program, i need to create a type of menu using getchar and i have no idea where to start. the program is supposed to output this:

    Enter P for payment calculation, L for loan calculation, or Q to quit:

    and depending on the input runs specific functions in the program. so far, this is what i have (note: i am very new to C to cut me some slack if this looks terrible :P)

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main()
    {
            int c;
            printf("Enter P for payment calculation, L for loan calculation,
    or Q to quit:");
            c = getchar();
    
    }

    where am i supposed to go from here? i figure i can use an if statement if getchar returns an integer, but what values should i expect for P, L, and Q? am i even thinking about this right?

  2. #2
    Registered User
    Join Date
    Feb 2004
    Posts
    46
    Code:
    c = getchar();
    if (c == EOF || (c != 'P' && c != 'L' && c != 'Q')) {
        // Handle bad input
    }
    // Use c

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    ... or use a switch statement
    Code:
    int c = getchar();
    
    switch (c)
    {
      case EOF:
        /* Handle EOF */
        break;
    
      case 'p':
      case 'P':
        /* Handle P and p */
        break;
    
      default:
        /* Handle anything else */
        break;
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Feb 2004
    Posts
    79
    Originally posted by Hammer
    ... or use a switch statement
    Code:
    int c = getchar();
    
    switch (c)
    {
      case EOF:
        /* Handle EOF */
        break;
    
      case 'p':
      case 'P':
        /* Handle P and p */
        break;
    
      default:
        /* Handle anything else */
        break;
    }
    That's really bright testing for 'p' twice. How embarrassing.
    R.I.P C89

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by c99
    That's really bright testing for 'p' twice. How embarrassing.
    How about actually looking at a C book for starters, then you'll have a clue as to what's acutally going on here. All of your posts I've read you've provided nothing useful, and your attempts at wit are falling short, so don't waste your time.

    Now, for the uneducated, this means you, they're testing for both upper case and lower case. So yes, it was bright. It saves a function call to tolower or toupper, and is a faster solution. However, the only problem here was a typo.

    But I'm sure Hammer doesn't need me defending them. If you're trying for the position of resident $$$$$$$, that position is filled. Go some place else.



    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >However, the only problem here was a typo.

    I don't even think there was even a typo. There's a lower case p and an upper case p. Unless of course the post has been edited.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by swoopy
    >However, the only problem here was a typo.

    I don't even think there was even a typo. There's a lower case p and an upper case p. Unless of course the post has been edited, which is impossible to know.
    You're right. There is no typo. It's my font. I didn't think there was a typo, then doubted myself (due to the font size I'm using). Thanks for pointing that out. On a side note, the post couldn't have been edited. The board comments the fact that it's been edited if you do it after more than a few seconds (maybe a minute tops) of the original post. Looking at the time of C99's post puts it hours later than Hammer's.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Feb 2004
    Posts
    79
    quzah.

    Get off my case you uneducated, clueless, insignificant peon.

    If I see a bug, I will point it out. If I see a typo, I will also point that out.

    And why not? I've endured members pedantics thus far.

    Resident $$$$$$$? Filled? Don't worry I'm not after your position.

    case 'p':
    case 'p':

    That might well be a typo, at least now the thread poster is notified
    and can edit the post.
    R.I.P C89

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by c99
    Get off my case you uneducated, clueless, insignificant peon.

    If I see a bug, I will point it out. If I see a typo, I will also point that out.

    And why not? I've endured members pedantics thus far.

    Resident $$$$$$$? Filled? Don't worry I'm not after your position.

    case 'p':
    case 'p':

    That might well be a typo, at least now the thread poster is notified
    and can edit the post.
    It isn't a typo. Look again. It's upper case and lower case, as I originally thought. You are wrong on both accounts. It is not a bug. It is not a typo. You are simply wrong.

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Feb 2004
    Posts
    79
    quzah.

    Make up your mind.
    R.I.P C89

  11. #11
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Originally posted by c99
    quzah.

    Get off my case you uneducated, clueless, insignificant peon.

    If I see a bug, I will point it out. If I see a typo, I will also point that out.

    And why not? I've endured members pedantics thus far.

    Resident $$$$$$$? Filled? Don't worry I'm not after your position.

    case 'p':
    case 'p':

    That might well be a typo, at least now the thread poster is notified
    and can edit the post.
    It is not a typo - copy and past each p individually into your favourite editor, placing them side by side, and you will see that indeed there is an upper case 'P' and a lower case 'p'.

    It kind of looks like you are still burned about the other night, C99. Too bad you have not gotten over that yet. You are not helping yourself by being a troll.

  12. #12
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Is c99 going for the record for the fastest banning?

  13. #13
    Registered User
    Join Date
    Feb 2004
    Posts
    79
    Kermit.

    No, I will not copy and paste code into my favorite text editor just
    so I can properly view what it actually is.

    Hopefully the forum moderators can quite plainly see that the
    forum font needs to be re-thought. Remember C is case sensitive
    so therefor so should be the forum fonts.

    I've witnessed two occassions now in two days whereby members
    have had trouble reading the font.

    And Thantos, care to explain your comment, or is that just a cheap
    shot across by bowel.
    R.I.P C89

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If you're including me in the "two cases", leave me out. My font is manually over-ridden, different from the forum defaults.

    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Well then C99, if you are unwilling to take the time to see if what you are typing is correct (ie, that Hammer used two small 'p's instead of a capital and a small) then maybe you should also not take the time to post about other people's alleged mistakes.

    If you have such a great problem with the fonts on the board, I am sure you can private message one of the lead mods with a request that something be done about that issue.

    ~/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getchar() problem
    By jlharrison in forum C Programming
    Replies: 6
    Last Post: 01-25-2006, 02:49 PM
  2. getchar buffer size
    By oncemyway in forum C Programming
    Replies: 3
    Last Post: 08-02-2005, 12:49 AM
  3. getchar() problem from K&R book
    By anemicrose in forum C Programming
    Replies: 13
    Last Post: 04-04-2004, 11:06 PM
  4. help with getchar lol
    By Taco Grande in forum C Programming
    Replies: 5
    Last Post: 03-18-2003, 09:25 PM
  5. Can anybody take a look at this?
    By TerryBogard in forum C Programming
    Replies: 10
    Last Post: 11-21-2002, 01:11 PM