【问题标题】:Slight issue with Do While LoopDo While 循环的小问题
【发布时间】:2016-10-02 23:36:23
【问题描述】:

所以我正在编写一个基本程序,要求用户输入一个数字,然后循环将继续,直到他们输入某个数字。 (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);

我也不确定总和是否会正确计算,因为我一直无法退出循环。

【问题讨论】:

  • 启用警告的良好编译器将报告scanf("%f", &userNum); 的问题节省时间!获得更好的编译器或确保您的警告全部启用。

标签: c loops while-loop


【解决方案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;
}

【讨论】:

    【解决方案2】:

    您使用了错误的数据类型,请改用整数:

    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);
    

    【讨论】:

      【解决方案3】:

      您想要使用哨兵控制循环(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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-27
        • 2013-09-26
        • 2016-01-03
        相关资源
        最近更新 更多