我的程序跳过获取输入数据?

时间:2013-09-10 14:16:29

标签: c scanf

我写了一个简单的程序来交换货币并且能够买到啤酒。

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

这是我的代码:

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

7 个答案:

答案 0 :(得分:2)

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

另见all answers in getchar after scanf category

答案 1 :(得分:2)

更改

scanf("%c", &buy);

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

因为输入数字后输入缓冲区仍然在输入缓冲区中,并在第二个scanf中按ENTER键。

答案 2 :(得分:2)

而不是scanf("%c", &buy);

1.在%c之前使用空格

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

这会跳过空白区域(包括换行符)。

2.或使用getchar();在scanf之前(“%c”,&amp; 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);。请避免使用它。

答案 3 :(得分:0)

我想知道为什么你让'选择'成为一个浮点数,而不是一个int 另外,请考虑使用开关盒 这样,您就不必进行整个do-while循环。 另外,在行 printf(“\ n你想买啤酒(是/否)?”, 购买 ); 你为什么加了? 这就是我要做的事情:

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更改为啤酒,这样,如果你改变啤酒,你将不必改变它:)

答案 4 :(得分:0)

使用fflush(stdin)清除最后一次scanf之前的输入

答案 5 :(得分:0)

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

答案 6 :(得分:-1)

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

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

char buy替换为char buy[2];。因为/0总是终止刺痛。

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

相关问题