简体   繁体   中英

C Programming help - Option for user to exit program

this is a follow on question from one I asked recently:

C Programming help - providing user with option to exit a program

I now have a new problem. I can get the program to exit if a user enters any letter which is great, but now if a number is entered nothing happens. The while loop doesn't seem to run..

Can you please have a look at my code and see if you can spot what's wrong, thanks. Also, ideally i'd like xterm's window to close if the user wishes to exit. I'd be greatful if anyone could show me how to do this. Anyway here's the code:

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

int main()
{
     float number;
     float sum = 0;


     printf ("Please enter number or enter any letter to exit:\n");
     scanf ("%f", &number);

     // if user ENTERS a letter, program will terminate

     if(1!=scanf ("%f", &number))
     {
        getchar();
        printf ("Exiting the program...\n");
        exit(0);
     }


     while (1)
     {
        sum += number;
        printf ("Sum: %.2f\n", sum);
        printf ("Please enter number or enter any letter to exit:\n");
        scanf ("%f", &number);

        // if user ENTERS a letter, program will terminate

             if(1!=scanf ("%f", &number))
             {
                getchar();
                printf ("Exiting the program...\n");
                exit(0);
             }

     }

    return 0;
}

As @BrianCain commented: you are calling scanf a second time in your if statement. If you entered a letter for the first scanf , it forces the second to immediately fail; if you entered a number for the first, then the second is waiting for you to enter another number.

I removed two of your scanf functions and it seems to fix the problem

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

int main()
{
    float number;
    float sum = 0;


    printf ("Please enter number or enter any letter to exit:\n");


    // if user ENTERS a letter, program will terminate

    if(1!=scanf ("%f", &number))
    {
        getchar();
        printf ("Exiting the program...\n");
        exit(0);
    }


     while (1)
     {
        sum += number;
        printf ("Sum: %.2f\n", sum);
        printf ("Please enter number or enter any letter to exit:\n");


        // if user ENTERS a letter, program will terminate

        if(1!=scanf ("%f", &number))
        {
            getchar();
            printf ("Exiting the program...\n");
            exit(0);
        }
    }
    return 0;
}

The scanf in your if erased the value of the previous one you did.

While the previous two answers are essentially correct, the simple (and clear) version would be:

if(number == 1)
    break;

Place this right after your original scanf() and get rid of the other if(..) ..; condition entirely. This will test to see if the number '1' was entered, and if so will break the infinite loop, allowing the program to continue down to the return 0; statement.

It will also deal with what could be confusing style, which is having an exit condition (the final return 0; ) that's essentially impossible to get to, or should be.

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