Home > Net >  How do i make a code return to the start in C
How do i make a code return to the start in C

Time:09-23

I'm currently trying to make a number pseudo calculator of sorts that is supposed to add any number of integers until a negative is given then sum the amount of integers added, the biggest, smallest, average and also count the numbers of integers given. I have managed to make all of that, now im supposed to loop it without using a infinite loop, i have tried several hours but i just cant figure it out. I tried making the code return to the start but that doesn't seem to work. I'm wondering if it's possible to make the code return to the start or if another way to make it loop exist.

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

int main(void)
{
    int number = 0;
    int big = -1;
    int small = -1;
    float average = 0;
    int sum = 0;
    int counter = 0;

    do
    {
    

printf("Enter a number: ");
        scanf_s("%d", &number);

        if (number < 0)
            break;

        sum  = number;

        if (big == -1 || number > big)
        {
            big = number;
        }

        if (small == -1 || number < small)
        {
            small = number;
        }

        counter  ;

        average = (float) sum / counter;


        printf("sum =%d, big = %d, small = %d, counter = %d", sum, big, small, counter);
        printf("average = %.2f", average);

    } while (number >= 0);

    int yes = 1;
    int no = 0;
    

    printf("Would you like to run the program again (1 for yes, 0 for no)?: ");
    scanf_s("%d, %d", &yes, &no);

    if (yes == 1 || no != 1)
    {
        goto main;
    }
    else


    return 0;
}

CodePudding user response:

The two simplest mechanisms are:

int main(void) {
    start:
    /* code here */
    goto start;
    /* ... */
}

and

int main(void) {
    int yes = 1; int no = 0;
    while( yes == 1 || no == 0 ){
        /* ... */
    }
    /* ... */
}

Note that using two flags (yes and no) is a bit odd. You probably would rather do something like:

char response = 'y';
while( response == 'y' || response == 'Y' ){
    ...
    if( scanf(" %c", &response) != 1 ){
        break;
    }
}

CodePudding user response:

For starters main is not a label So this goto statement

goto main;

does not make a sense. And you should not use the goto statement in your programs.

These statements

average = (float) sum / counter;

printf("sum =%d, big = %d, small = %d, counter = %d", sum, big, small, counter);
printf("average = %.2f", average);

must be moved after the do-while loop.

In this if statement

    if (big == -1 || number > big)
    {
        big = number;
    }

the expression big == -1 is redundant because it is evident that any non-negative number is greater than -1.

And you need to enter a value only to one variable in this call of scanf_s

scanf_s("%d, %d", &yes, &no);

That is the call will look like

scanf_s("%d", &yes);

As for your question when you need to use one more outer do-while or while loop.

For example

int yes;

do
{
    do 
    {
        //...
    } while ( number >= 0 );

    average = (float) sum / counter;
    printf("sum =%d, big = %d, small = %d, counter = %d", sum, big, small, counter);
    printf("average = %.2f", average);

    printf("Would you like to run the program again (1 for yes, 0 for no)?: "); 
} while ( scanf_s("%d", &yes ) == 1 && yes == 1 );

Also you need to check whether the user at once entered a negative number. In this case for example counter will be equal to 0. In this case you should issue a message that the user did not enter numbers instead of outputting sum, big, small, counter and average.

  • Related