Problem grabbing k/b input

This is a discussion on Problem grabbing k/b input within the C Programming forums, part of the General Programming Boards category; Hi all. thx for reading! I am having a problem writing a tax program that is quite simple but for ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    4

    Problem grabbing k/b input

    Hi all. thx for reading!

    I am having a problem writing a tax program that is quite simple but for some reason i cant get the keyboard inputs to work correctly. I have tried interchanging getchar for scanf, using while loops to try and eat excess characters in the input buffer (using both scanf and getchar) and even simply adding extra getchar's around the input functions to try and eat anything extra.

    What happens is, when I get to an input line, it skips past it directly to the next one. I would say that it appears to be on the third function call for character input, rotating whenever I add a while loop before one of the input calls to eat up extra characters. I tried adding while loops to each function to no avail, the pattern breaks.


    The prog might have some other inneffectual errors, like printing something to the screen before it should, but they shouldnt have an effect on the input problem(?). Hard to debug when I seem to identify the main bug!

    Any suggestions would be greatly appreciated. Thanks all.


    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    
    int main(void){
        
        int terminate2 = 0, totalwages, taxes2pay;
        char status, mstatus, terminate; 
        while(terminate != 'n' || terminate != 'N'){
                        printf("\nPlease enter your tax filing catagory. Options are:\n\nSingle\nHead of Household\nMarried (Joint or Separate)\n\n:");
                        scanf("%c", &status);
                        status = toupper(status);
                        printf("Enter wages earned last tax year: ");
                        scanf("%d", &totalwages);
                        switch (status){
                               case 'S' :
                                    if (totalwages > 17850.00){ 
                                         taxes2pay = (0.15 * 17850.00) + (0.28 * (totalwages - 17850.00));
                                         }
                                    else {
                                         taxes2pay = (0.15 * totalwages);
                                         }
                                    break;
                               case 'H' : 
                                    if (totalwages > 17850.00){ 
                                         taxes2pay = (0.15 * 23900.00) + (0.28 * (totalwages - 17850.00));
                                         }
                                    else {
                                         taxes2pay = (0.15 * totalwages);
                                         }
                                    break;          
                               case 'M': 
                                    while(terminate2 != 1){
                                       printf("Are you filing Joint or Separate?\n\n:");
                                       scanf("%c", &mstatus);      
                                       toupper(mstatus);
                                       if (mstatus == 'J'){
                                              if (totalwages > 17850.00){ 
                                                    taxes2pay = (0.15 * 29750.00) + (0.28 * (totalwages - 29750.00));
                                                    terminate2++;
                                                    }
                                              else {
                                                    taxes2pay = (0.15 * totalwages);
                                                    terminate2++;
                                                    }
                                       }       
                                       else if (mstatus == 'S'){
                                              if (totalwages > 17850.00){ 
                                                    taxes2pay = (0.15 * 14875.00) + (0.28 * (totalwages - 14875.00));
                                                    terminate2++;
                                                    }
                                              else {
                                                    taxes2pay = (0.15 * totalwages);
                                                    terminate2++;
                                                    }
                                       }     
                                       else
                                              printf("Please type either Joint or Separate.");
                                    }
                               default :
                                       printf("Please enter either Single, Head of Household, or Married.");     
                        }
        printf("\n\nTotal taxes: $%d\n\n", taxes2pay);
      //  while(getchar() != '\n');                  //trying to eat up extra chars in the input buffer
        printf("Would you like to perform another calculation? (Y/N): ");
        scanf("%c", &terminate);
        }
        system("pause");
        return 0;
        
    }

  2. #2
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    975
    Your commented-out line about eating up extra characters is on the right track.

    It seems that you have an idea about what the problem is: when you say scanf("%d", ..), and the user types in a number, he also hits enter. %d won't read that enter (which comes through as a newline) and so it remains in the stream. scanf("%c", ..) will happily read it. Note also that scanf("%c", ..) will leave a newline hanging around, too, if the user actually has a chance to type something. It might leave more if he types more than one character and hits enter.

    The "best" fix is to read lines (say, with fgets(), which has its own problems but is not too bad) and parse them. If this is homework, though, you're probably stuck with the maddening scanf().

    The ugly-but-probably-works fix in this case is something like:
    Code:
    int c;
    while((c = getchar()) != EOF && c != '\n') ;
    Ugly because it will sit and wait if there's no newline (and EOF isn't signalled). Unfortunately, there's not a portable way to say "drain the input stream, but don't ask for input if there's nothing to drain." You would place the while loop (or a function call/macro invocation that does the same thing) after each input function that you expect to leave a newline or other junk. Not recommended in real code but for homework where you have to do things a certain way, you're sometimes stuck.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    4
    thanks cas, I had read alot about that probably being the issue but its just so hard to accept as messy as it is. I had to add a character-eating loop after each input function and that took care of it, aside from having to press enter twice when exiting. I hope to learn better ways of handling such problems!


    Thanks again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. geting input in C, problem in getting input
    By BujarM in forum C Programming
    Replies: 3
    Last Post: 04-17-2009, 09:38 PM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. Input statement problem
    By une in forum C Programming
    Replies: 3
    Last Post: 05-29-2007, 11:16 PM
  4. Problem with File Input & Pointers
    By jenna in forum C++ Programming
    Replies: 9
    Last Post: 12-04-2004, 10:34 PM
  5. Problem with text input
    By newbie in forum C++ Programming
    Replies: 2
    Last Post: 03-10-2002, 03:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21