1

我编写了一个简单的程序来兑换货币并能够购买啤酒。

但是程序中有一些东西,我不知道为什么,它会自动跳过第三个输入数据-> 结束程序。

这是我的代码:

#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;
}
4

7 回答 7

2

代替scanf("%c", &buy);

1.使用 %c 之前的空间

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

这会跳过读取空白(包括换行符)。

2.或使用getchar();在 scanf("%c", &buy); 陈述

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

3.或者使用两次getchar();

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

在 GCC 中fflush(stdin);不鼓励使用 of。请避免使用它。

于 2013-09-10T14:34:59.890 回答
2

\n在最新的浮动条目和char要阅读的条目之间至少有一个。你需要先摆脱它。

另请参阅类别中的所有答案getcharscanf

于 2013-09-10T14:20:08.657 回答
2

改变

scanf("%c", &buy);

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

因为在输入数字并在第二个中按 ENTER 后,换行符仍在输入缓冲区中scanf

于 2013-09-10T14:20:16.357 回答
0

我想知道为什么您将“选择”设为浮点数,而不是整数另外,考虑使用这种方式的开关盒,您不必执行整个 do-while 循环。另外,在 printf ("\n你想买啤酒 (y/n) 吗?", buy ); 你为什么加那个?这是我会做的:

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 :) */

}

}

编辑:另外,它说if( <somevariable> >= 7000),将 7000 更改为啤酒,这样,如果您更改啤酒,您将不必更改此 :)

于 2013-09-10T14:24:52.187 回答
0

在最后一次 scanf 之前放一个 fflush(stdin) 来清除输入

于 2013-09-10T14:25:03.907 回答
0

该程序不会跳过第三个输入数据,它只会扫描您在第二个输入后按下的换行符。要解决此问题,请键入scanf("%*c%c", &buy);而不是scanf("%c", &buy);. 这个小%*c扫描并忽略从输入中读取的字符。

于 2013-09-10T14:28:03.590 回答
-1

您可以buy从 printf 调用中删除变量,它是不需要的

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

并替换char buychar buy[2];. 因为刺痛总是由/0.

您还可以添加memset (buy, 0, sizeof(buy)), 以确保在开始使用之前重置内存。

于 2013-09-10T14:26:21.693 回答