简体   繁体   中英

C skip a “while” loop?

I have a problem, I tried to write a program to show the whole sum from 1 to 22 and after that, to do 2 while loops. The first one is supposed to perform the sum of some numbers given by the user, as an example: you type 10, 30 and 40 then as you enter a 0 the program sums the first three numbers. Unfortunetly the first while loop is not working. It goes directly to the last while loop where it is supposed to type a decimal numbers like (10.20 30.50 40.55) and after you type 0 again it sum those numbers and add and multipli every entry with 1.19. So far the last loop is working properly, unfortunately the second loop does not, if I move printf and scanf over the while it let me write but just start writing w/o stopping the number I wrote . Thank You in advance!

Here is the code :

#include <stdio.h>

int main() 
{
    int sum = 0;                         
    int a;
    int b;
    double i;
    double sum1 = 0;
    for (a= 0; a <= 22; a++) {

        sum = sum + a; 
        printf("the sum from 1 till 22 : %i\n ", sum);
    }

    while (b != 0) {
        printf("type a number:");
        scanf("%i", &b);
        sum += b;
        printf("%i\n", b);

    }
    printf("the sum is : %i\n", sum);

    while(i !=0) {
        printf ("Type a decimal number:");
        scanf ("%lf",&i);                       
        sum1 += i*1.19;


        printf("%lf\n", i);

    }

    printf("The decimal summ is: %lf\n",sum1);
    return 0;
}

You don't initialise i to any value before entering the loop with

while(i != 0)

i might very well be zero at this point, so your loop won't be entered even once. Initialising i to a non-zero value should fix this particular problem. The same holds for the variable b .

You should turn on warnings in your compiler, so it can show you problems like this one.

The first time the condition of the second while is evaluated, b has undefined value, since it wasn't initialized. The same applies to the third while.

Whether or not both loops are executed is only a question of chance.

Initialize both variables with non-zero values to ensure both whiles are entering. Or use a do-while:

do {

    printf("type a number:");
    scanf("%i", &b);
    sum += b;
    printf("%i\n", b);

} while (b != 0);

Don't test b with while , test it after the user enters the number. Then you can use break to exit the loop.

while (1) {
    printf("type a number:");
    scanf("%i", &b);
    if (b == 0) {
        break;
    }
    sum += b;
    printf("%i\n", b);
}

while(1) {
    printf ("Type a decimal number:");
    scanf ("%lf",&i); 
    if (i == 0.0) {
        break;
    }                      
    sum1 += i*1.19;
    printf("%lf\n", i);
}

Your only issues are initialization: see edits in the code below. (it compiles and runs)
Did you get any compiler warnings for these? If not, you should change your settings so you do.

#include <stdio.h>

int main() 
{
   int sum = 0;                         
   int a;
   int b=-1;  //initialize (any non-zero value will work)
   double i;
   double sum1 = 0;
    for (a= 0; a <= 22; a++) {//a initialized in for(...) statement, (this is good)

    sum = sum + a; 
    printf("the sum from 1 till 22 : %i\n ", sum);
        }

    while (b != 0) { //b Needs to be initialized before using (done above)
        printf("type a number:");
        scanf("%i", &b);
        sum += b;
        printf("%i\n", b);

    }
    printf("the sum is : %i\n", sum);
    i=-1;                          //initialize i to any non-zero value
    while(i !=0) {  
     printf ("Type a decimal number:");
     scanf ("%lf",&i);                      
     sum1 += i*1.19;


      printf("%lf\n", i);

    }
     printf("The decimal summ is: %lf\n",sum1);

     getchar();
    return 0;
}

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