0

I am getting crazy here attempting to do something I thought it would be easy... :-) Hopefully you guys can help me out.

This a C function written for a Linux Ubuntu...

All I need is to show a confirmation message and I expect the user to hit ENTER to continue.

    #include <stdio.h>
    #include <time.h>

    void setDeposit(int account, int amount)
    {
        printf("You have successfully transfered %d EUR to the account number %d\nPlease press ENTER to continue\n", account, amount);
        getchar();

    }

The application "ignores" the getchar() and simply moves on.

EDITED

Including the entire program as requested in the comments:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "rbs-accmgr.c"
#include "rbs-graphics.c"
#include "rbs-struct.c"

//Main application function
int main()
{
    //Creating the application control instance
    application a = {1, 0, 0};

    //Populating the customer information with the login interface information
    customer c = {1234, "John", "Deer", 0};

    //Clearing the terminal window
    system("clear");

    //Keeps the application running as long as the user doesn't hit option 9 (Quit)
    while (a.status == 1)
    {
        //Displaying the graphic objects
        displayBanner();
        displayMenu();
        scanf("%d",&a.selectedOption);
        displayBanner();

        switch(a.selectedOption)
        {
            //Deposit
            case 1:
                printf("\nHow much would you like to deposit?\n");
                scanf("%d",&a.operationAmount);
                setDeposit(c.account, a.operationAmount);
                break;

            //Wrong option
            default:
                a.status = 0;
                break;
        }

        //Making sure all variables are zeroed
        a.operationAmount = 0;
    }

    return 0;

}

What am I doing wrong here?

Thanks!

  • 3
    whats the error you are getting, or the problem you are facing?? – Haris Oct 08 '14 at 10:03
  • Sorry guys. I have updated the question. –  Oct 08 '14 at 10:04
  • 7
    Works for me - there must be additional code which causes this. Are you reading additional input before (e.g. through `scanf()`)? – Andreas Fester Oct 08 '14 at 10:06
  • Show the `main()` function of your code where you call this `setDeposit()` function. – iqstatic Oct 08 '14 at 10:08
  • I would suggest you post the entire programm if its not to big, so that we can rebuild the error. – Rizier123 Oct 08 '14 at 10:11
  • Entire program posted! –  Oct 08 '14 at 10:14
  • tl;dr : If you value your sanity avoid `scanf()` and `fscanf()`! They eat whitespace even crossing line boundaries. `sscanf()` can be useful, though. – PM 2Ring Oct 08 '14 at 12:37
  • And **_please_** don't use `#include` to inject other .c source files into your code. That's ugly and evil. Write your .c files so that the compiler can translate each of them independently. _Only_ `#include` proper .h header files which should only contain non-executable code, eg macro definitions, function prototypes and perhaps other types of declaration (there are exceptions to this rule but you don't need to worry about them at this stage). – PM 2Ring Oct 08 '14 at 12:49

1 Answers1

1

Please try the one below and let me know. I think that getChar is used by that scanf you wrote. A while reading a separate char can rule that out. I wrote \r in case you use other operating systems.

void setDeposit(int account, int amount)
{
    printf("You have successfully transfered %d EUR to the account number %d\nPlease press ENTER to continue\n", account, amount);

    char myChar = 0;
    while (myChar != '\n' && myChar  != '\r') { 
        myChar = getchar(); 
    }
}

Detailed information about the case can be found within this thread.

Community
  • 1
  • 1
İsmet Alkan
  • 5,361
  • 3
  • 41
  • 64