Do While Loop的轻微问题

时间:2016-10-02 23:36:24

标签: c loops while-loop

所以我正在写一个基本程序,要求用户输入一个数字,循环将继续,直到他们输入一定的数字。 (25)。之后程序会将他们输入的所有数字相加。问题是当我输入退出号码时,循环没有退出,我不知道为什么。

double userNum = 0;
double sum = 0;

do {
    printf("Please enter a number you would like to add [Enter 25 to exit at any time]:\n");
    scanf("%f", &userNum);
    sum = sum + userNum;
} while (userNum != 25); 

printf("The sum of all the numbers you entered:%f\n", sum);

此外,我不确定总和是否会正确计算,因为我从未能够退出循环。

3 个答案:

答案 0 :(得分:1)

考虑使用fgets进行输入并使用sscanf解析值。使用此方法,您可以输入done或exit来终止循环而不是25.扫描双精度的格式为%lf

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

int main( void)
{
    char input[99] = "";
    double userNum = 0;
    double sum = 0;
    while ( 1) {
        printf("Please enter a number you would like to add [Enter 25 to exit at any time]:\n");
        if ( ( fgets ( input, sizeof ( input) , stdin))) {
            if ( strcmp ( input, "25\n") == 0) {//could use exit, done ... instead of 25
                break;
            }
            if ( ( sscanf(input, "%lf", &userNum)) == 1) {//sscanf successful
                sum = sum + userNum;
            }
        }
        else {
            break;//fgets failed
        }
    }
    printf("The sum of all the numbers you entered:%f\n", sum);

    return 0;
}

答案 1 :(得分:0)

你使用了错误的数据类型,改为使用整数:

int userNum = 0;
int sum = 0;
do {
    printf("Please enter a number you would like to add [Enter 25 to exit at any time]:\n");
    scanf("%d", &userNum);
    sum = sum + userNum;
} while (userNum != 25);
printf("The sum of all the numbers you entered:%d\n", sum);

答案 2 :(得分:0)

你想要使用一个哨兵控制的循环(25是你的哨兵)。以下是我要写的内容:

    #include <stdio.h>


    int main()
    {
        double userNum = 0;
        double sum = 0;

        while ( userNum != 25 ) { //sentinel controlled loop

        puts("Please enter a number you would like to add [Enter 25 to exit                at any time]:"); // puts automatically inputs a newline character
        scanf("%lf", &userNum); // read user input as a double and assign it to userNum
        sum = sum + userNum; // keep running tally of the sum of the numbers

        if ( userNum == 25 ) { // Subtract the "25" that the user entered to exit the program
        sum = sum - 25;
        } // exit the if
      } // exit while loop

        printf("The sum of all the numbers you entered:%lf\n", sum);

     } // exit main

或者,你可以坚持做......同时:

    // Using the do...while repetition statement
    #include <stdio.h>

    // function main begins program execution
    int main( void )
    {
       double userNum = 0; // initialize user input
       double sum = 0; //initialize sum as a DOUBLE

       do {                                               
        puts("Please enter a number you would like to add [Enter 25 to exit at any time]:"); // puts automatically inputs a newline character
        scanf("%lf", &userNum); // read user input as a double and assign it to userNum
        sum = sum + userNum; // keep running tally of the sum of the numbers


        if ( userNum == 25 ) { // Subtract the "25" that the user entered to exit the program
        sum = sum - 25;
        } // end if statement
       } while ( userNum != 25 ); // end do...while   

       printf("The sum of all the numbers you entered:%lf\n", sum);

    } // end function main