C skip while loop?

I have a problem, I tried to write a program to show the whole sum from 1 to 22 and after that do 2 loops. The first is to do the sum of some user-supplied numbers, as an example: you enter 10, 30 and 40, and then when you enter 0, the program adds the first three numbers. Unfortunately, the first while loop does not work. It goes directly to the last while loop, where it is supposed to enter decimal numbers such as (10.20 30.50 40.55), and after you type 0 again, it sums those numbers and adds and multiplies each entry since 1.19. While the last loop is working correctly, unfortunately the second loop does not work if I move printf and scanf while it allows me to write, but just start writing without 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;
}

      

+3


source to share


4 answers


Do not test b

with while

, test it after user input 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);
}

      

+1


source


You don't initialize i

to any value before entering the loop with

while(i != 0)

      



i

can be very null at this point, so your loop won't be injected even once. Initializing i

to non-zero should fix this particular problem. The same goes for the variable b

.

You have to include warnings in your compiler so that it can show you problems like this one.

+5


source


The first time the condition of the second while is evaluated b

is undefined because it has not been initialized. The same applies to the third.

Whether both loops are executed is just a matter of chance.

Initialize both variables with non-null values ​​to ensure that both whiles are input. Or use do-while:

do {

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

} while (b != 0);

      

+2


source


The only issues are initialization: see code changes below. (it compiles and runs)
Have you received any compiler warnings? If not, you must change your settings in order for you to do this.

#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;
}

      

0


source







All Articles