2016-10-02 8 views
0

私は基本的なプログラムを書いているので、ユーザーに数字を入力するように求めています。 (25)。その後、プログラムは入力したすべての数値を加算します。問題は、出口番号を入力するとループが終了せず、理由がわからないということです。Do While Loopの問題軽微

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

また、ループを終了できなかったため、合計が正しく計算されないことがあります。あなたは間違ったデータ型を使用している

+0

"、&userNum);'時間を節約!より良いコンパイラを入手するか、警告が有効になっていることを確認してください。 – chux

答えて

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

は、入力用のfgetsを使用して検討し、sscanf関数で値を解析。これで完了または終了して、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; 
} 
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 

OR、...ながら:有効な警告との良好なコンパイラは `のscanf("%fの問題が報告されています

// 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