简体   繁体   中英

My C do while loop will not execute

My program assignment is to write a looping program that calculates USD to 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.

As other folks have explained, your problem is due to \\n remaining in STDIN.

In order to skip it, just replace

scanf_s("%c", &again);

with

scanf_s(" %c", &again);

This is part of scanf 's functionality:

White-space characters: blank (' '); tab ('\\t'); or newline ('\\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. One white-space character in the format matches any number (including 0) and combination of white-space characters in the input.

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

For your first 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

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

if you want to use scanf_s then you can do something like

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

For more information about this check http://c-faq.com/stdio/gets_flush2.html

I think you must use strcmp() here.

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

I'm about two weeks into learning C so sorry if it doesn't work!

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. like newline, space etc. when you were pressing enter 'USD' was taking the value and 'again' was taking the newline character. 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 . This happens because you told scanf (the first one) to read just a float (double) number and nothing else.

To see what's going on, you could check the value of the variable again as indiv said. 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.

instead try this:

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

Put that line right after the last 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.

change

scanf_s("%c", &again);

to

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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