Problem: Functions

This is a discussion on Problem: Functions within the C Programming forums, part of the General Programming Boards category; You're supposed to mind read him. BTW, this code never executes: Code: else printf("Invalid input!\tEnter selection (c, d, w, or ...

  1. #16
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    You're supposed to mind read him.

    BTW, this code never executes:
    Code:
        else
          printf("Invalid input!\tEnter selection (c, d, w, or q): ");
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  2. #17
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    It is supposed to show you that you alwais get TWO characters. First time the desired one and an additional 0x0a for the newline that's still in the buffer from the previous call to scanf.
    Kurt

  3. #18
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    So, you should clear the keyboard buffer:
    Code:
    int c;
    while((c = getchar()) != '\n' && c != EOF);
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #19
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by dwks
    You're supposed to mind read him.
    No mind reading required.
    I was just tired to repeat again that scanf leaves newlines in the buffer.
    Kurt

  5. #20
    Registered User
    Join Date
    Nov 2005
    Posts
    13
    Still don't get it. Maybe you can give me a link to a tutorial where I can read about this stuff in more details. Thanks

  6. #21
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    In your case there would be a simple solution
    Code:
    while((trans = getTrans()) != 'q')  {    
        if (trans == 'c' || trans == 'C'){
          updateCheque(&balance, &cheque, &cheques, &bounced);
          transMenu();
        }
        else if(trans == 'd' || trans == 'D'){
          updateDeposit(&balance, &deposit, &deposits);
          transMenu();
        }
        else if(trans == 'w' || trans == 'W'){
          updateWithdrawal(&balance, &withdrawal, &withdrawals);  
          transMenu();
        }
        else if(trans == '\n'){
          // just ignore newlines
        }
        else
          printf("Invalid selection, enter 'c', 'd', 'w', or 'q': ");          
     }
    You could just as well put dwks code after each scanf in the program.
    Kurt

  7. #22
    Registered User
    Join Date
    Nov 2005
    Posts
    13
    Zuk
    Thanks, it works.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with functions
    By saliya in forum C Programming
    Replies: 1
    Last Post: 11-05-2007, 09:36 PM
  2. Problem with system(), execl(), etc. functions
    By nickkrym in forum C Programming
    Replies: 5
    Last Post: 05-12-2007, 08:04 AM
  3. Problem with pointers and functions
    By Kheila in forum C++ Programming
    Replies: 5
    Last Post: 10-13-2005, 12:40 PM
  4. Problem with functions
    By lizardking3 in forum C++ Programming
    Replies: 4
    Last Post: 09-22-2003, 04:34 PM
  5. problem with functions and "parse errors"
    By bart in forum C++ Programming
    Replies: 3
    Last Post: 08-27-2001, 08:52 AM

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