简体   繁体   English

我的C do while循环不会执行

[英]My C do while loop will not execute

My program assignment is to write a looping program that calculates USD to Euros. 我的程序分配是编写一个循环程序,该程序将USD转换为Euros。 My code looks like this 我的代码看起来像这样

#include <stdio.h>


int main(void)

{
    double USD, euro;
    char again;

    do
    {
    printf("Please enter the amount of USD you want to convert to Euros> ");
    scanf_s("%lf", &USD);

    euro=USD * 0.73209;

    printf("%4.2f USD equals %4.2f Euros.", USD, euro);
    printf("Do you want to convert another amount (y/n)?");
    scanf_s("%c", &again);
    }

    while (again == 'y' || again == 'Y');


    return 0;
}

And when I run the program, it executes, allows my to enter a USD value, gives me the correct Euro value, then when prompt for y/n it just exits. 当我运行该程序时,它将执行,允许我输入美元值,为我提供正确的欧元值,然后在提示输入y / n时退出。

As other folks have explained, your problem is due to \\n remaining in STDIN. 正如其他人所解释的那样,您的问题是由于\\n保留在STDIN中。

In order to skip it, just replace 为了跳过它,只需替换

scanf_s("%c", &again);

with

scanf_s(" %c", &again);

This is part of scanf 's functionality: 这是scanf功能的一部分:

White-space characters: blank (' '); 空格字符:空白(''); tab ('\\t'); 标签('\\ t'); or newline ('\\n'). 或换行符('\\ n')。 A white-space character causes scanf to read, but not store, all consecutive white-space characters in the input up to the next non–white-space character. 空格字符会使scanf读取但不存储输入中的所有连续的空格字符,直到下一个非空格字符。 One white-space character in the format matches any number (including 0) and combination of white-space characters in the input. 格式中的一个空格字符与输入中的任何数字(包括0)和空格字符组合匹配。

http://msdn.microsoft.com/en-us/library/kwwtf9ch.aspx http://msdn.microsoft.com/en-us/library/kwwtf9ch.aspx

For your first scanf , 对于您的第一次scanf

scanf(" %lf", &USD);

may also help. 可能也有帮助。

This is because that there are stray \\r and or \\n left in your input stream (stdin), which is read by scanf and gets out of the do while, replace your scanf_s line with 这是因为在输入流(stdin)中剩余了杂散\\ r和/ n,它们由scanf读取并且无法执行,请用以下命令替换scanf_s行:

while((again = getchar()) != '\n' && again != EOF);

if you want to use scanf_s then you can do something like 如果您想使用scanf_s,则可以执行以下操作

do {scanf_s("%c", &again);}while(again == '\n' || again == '\r');

For more information about this check http://c-faq.com/stdio/gets_flush2.html 有关此信息的更多信息,请访问http://c-faq.com/stdio/gets_flush2.html

I think you must use strcmp() here. 我认为您必须在这里使用strcmp()。

while ( strcmp(again, 'y') == 0 || strcmp(again, 'Y') == 0 )

I'm about two weeks into learning C so sorry if it doesn't work! 我要学习C大约有两个星期,所以对不起,如果抱歉!

Try this. 尝试这个。

#include <stdio.h>


int main(void)

{
    double USD, euro;
    char again;

    do
    {
        printf("Please enter the amount of USD you want to convert to Euros> ");
        scanf_s("%lf", &USD);
        getchar();
        euro=USD * 0.73209;

        printf("%4.2f USD equals %4.2f Euros.", USD, euro);
        printf("Do you want to convert another amount (y/n)?");
        again = getchar();
    }
    while (again == 'y' || again == 'Y');
    return 0;
}

It is happening because scanf does not takes whitespace characters. 这是因为scanf不需要空格字符。 like newline, space etc. when you were pressing enter 'USD' was taking the value and 'again' was taking the newline character. 例如换行符,空格等。当您按Enter键时,“ USD”取值,而“再次”取换行符。 Hope this helps. 希望这可以帮助。 :) :)

stdin (standard input) has one more character you are missing, right after you ask for the USD amount, that character don't simple goes way, as a matter of fact it's waiting for you to pull it out, and that's what you do with the last scanf . stdin (标准输入)在您要求美元金额后,又缺少一个字符,该字符并非简单易行,事实上,它正在等待您将其取出,这就是您要做的最后一个scanf This happens because you told scanf (the first one) to read just a float (double) number and nothing else. 发生这种情况是因为您告诉scanf (第一个)仅读取一个float (双float )而没有其他内容。

To see what's going on, you could check the value of the variable again as indiv said. 要查看发生了什么,您可以按照indiv所述again检查变量的值。 Although it's preferable to print the ASCII value instead of the character itself, because you may not be able to really know for sure if it is an space, new line, tab or any other character of this kind. 尽管最好打印ASCII值而不是字符本身,因为您可能无法真正确定它是空格,换行,制表符还是任何其他此类字符。

instead try this: 而是尝试这样:

printf("ASCII: %d, CHAR: %c\n", again, again);

Put that line right after the last scanf . 将该行放在最后一个scanf

Either way, your problem is to find a way to discard that last character. 无论哪种方式,您的问题都是找到一种丢弃最后一个字符的方法。 One solution could be reading an string from stdin and then using atof() to convert that string into a decimal number. 一种解决方案是从stdin读取一个字符串,然后使用atof()将字符串转换为十进制数。

change 更改

scanf_s("%c", &again);

to

scanf_s("%c", &again, 1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM