1

I have written a simple program to exchange currency and able to buy a beer.

But there something in program and I don't know why, it auto skips the third input data -> end program.

Here my code :

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int ex_rate_into_vnd = 20000; //! Exchange Rate
    int beer = 7000; //! Local price of a beer
    float in_c = 0; //! Input amount of money
    float out_c = 2; //! Amount of currency to exchange !
    float choice; //! Switch mode
    char buy; //! Deal or not

    //! Introduction
    printf ("||---------------------------------------------------||\n");
    printf ("||         Currency Exchange Machine  beta           ||\n");
    printf ("||---------------------------------------------------||\n");

    printf ("Please choose your option:\n");
    printf("\t 1.Exchange VND to dollar\n");
    printf("\t 2.Exchange Dollar to VND\n");

    do
    {
        printf("Your choice: ",choice);
        scanf("%f",&choice);
    } while( choice != 1 && choice != 2);

    printf ("Please enter amount of money:");
    scanf("%f",&in_c);

    if (choice == 1 )
        {
            out_c = in_c / ex_rate_into_vnd;
            printf ("Your amount of money: %.2f",out_c);
        }
    else
        {
           out_c = in_c * ex_rate_into_vnd;
           printf ("Your amount of money: %.0f",out_c);
        }
//! End of Exchanging

    printf ("\nWould you like to buy a beer (y/n) ?",buy);
    scanf("%c", &buy);

    if (buy == 'y')
        {
        if (out_c >= 7000)
            {
                out_c = out_c - 7000;
                printf("Transactions success !\n");
                printf("Your amount: %2.f",out_c);
            }
        }
    printf ("\nWhy Stop ?");


    return 0;
}
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Lê Hòa
  • 45
  • 1
  • 2
  • 6
  • why do you add the argument to printf when asking for the input? – Leeor Sep 10 '13 at 14:19
  • There are several posts which can answer your question . Check out my answer on a similar question :- http://stackoverflow.com/questions/17614761/calling-scanf-after-another-string-input-function-creates-phantom-input/17615082#17615082 – 0decimal0 Sep 10 '13 at 14:41

7 Answers7

2

You have at least one \n between the latest float entry and the char you want to read. You need to get rid of that first.

See also all answers in getchar after scanf category

Community
  • 1
  • 1
Mihai Maruseac
  • 20,967
  • 7
  • 57
  • 109
2

Change

scanf("%c", &buy);

to

scanf(" %c", &buy);
//     ^space

Because the newline character is still in the input buffer after you enter a number and press ENTER in the second scanf.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
  • That's not quite a good solution. – Mihai Maruseac Sep 10 '13 at 14:20
  • @MihaiMaruseac I agree this is just a quick and dirty way to fix the problem, however, I don't think using `getchar` with `scanf` is any better. The better way is to avoid using `scanf` at all, and use `fgets` and `sscanf` instead. – Yu Hao Sep 10 '13 at 14:26
  • Your tweak will also not work if the user inputs a space before the actual input. That's why it's worse. I agree with you that `fgets` + `sscanf` is the way to go everytime but just look at how many people use the `scanf` approach. – Mihai Maruseac Sep 10 '13 at 14:28
2

Instead of scanf("%c", &buy);

1.use space before %c

scanf(" %c",&buy); //space before %c  
       ^ 

this skips reading of white space (including newlines).

2.or Use getchar(); before scanf("%c", &buy); statement

getchar(); //this hold the newline 
scanf("%c", &buy);

3.or use two times getchar();

getchar();
buy=getchar();     
//here getchar returns int , it would be better if you declare buy with integer type.

In GCC usage of fflush(stdin); is discouaraged. please avoid using it.

Gangadhar
  • 10,248
  • 3
  • 31
  • 50
0

I was wondering why you made 'choice' a float, not an int Also, consider using a switch-case that way, you won't have to do the whole do-while loop. Also, in the line printf ("\nWould you like to buy a beer (y/n) ?",buy); Why did u add that? Here is what I would have done :

printf("Your choice?\n>");
scanf("%d", &choice);

switch(choice)
{
     case 1 :
    {
         out_c = in_c / ex_rate_into_vnd;
            printf ("Your amount of money: %.2f",out_c);


    }

     case 2:
    {

    out_c = in_c * ex_rate_into_vnd;
           printf ("Your amount of money: %.0f",out_c);

    }

    default :
    printf("\nThere has been an error\n"):
    reloadprogram();   /* Reloadprogram() is simply to make this go back to the asking thing :) */

}

}

EDIT: also, where it says if( <somevariable> >= 7000), change 7000 to beer, so that way, if u change beer, you won't have to change this :)

ABanerjee
  • 97
  • 1
  • 1
  • 8
0

Put a fflush(stdin) to clear the input before the last scanf

0

The program doesn't skip the third input data, it just scans the newline you press after the second input. To fix this, type scanf("%*c%c", &buy); instead of scanf("%c", &buy);. This little %*c scans and ignores the the character read from the input.

Rontogiannis Aristofanis
  • 8,883
  • 8
  • 41
  • 58
-1

you can remove the buy variable from the printf call, it's unneeded

printf ("\nWould you like to buy a beer (y/n) ?",buy);

And replace char buy by char buy[2]; . because a sting is always terminated by /0.

You can also add a memset (buy, 0, sizeof(buy)), to be sure that memory is reset before you start to use it.

Mali
  • 2,990
  • 18
  • 18
  • `buy` is not a string, it's a character. Null-termination doesn't apply. And the `memset` is not needed. – interjay Sep 10 '13 at 14:32