Home > database >  How to reset number to 0 after running program once again
How to reset number to 0 after running program once again

Time:01-02

I'm making program which converts sum of the numbers entered by user to binary, octal and hexadecimal. I would like to give a user an option to run program once again but with different numbers. Here is the problem - each time user repeats numbers, sum of these numbers are added to the previous entry. How do I stop incrementing? Is there any way to reset sum to 0? Thank you so much for your help!

CodePudding user response:

Is there any way to reset sum to 0?

Yes, set sum to 0 right before the inner loop.

sum = 0;
while(1)
{
    printf("Enter number: ");
    scanf("%d", &value);
    sum  = value;
    if(value == 0)break;
}

Also, rather then using while (1) with a break condition at the end, use a do..while loop.

sum = 0;
do {
    printf("Enter number: ");
    scanf("%d", &value);
    sum  = value;
} while (value != 0);

CodePudding user response:

You would simply need to reset your sum variable to 0 if the user wants to reuse the number system with other numbers -

printf("\nEnter numbers once again? (y/n)\n");
scanf(" %c", &j);
printf("\n\nENTERING ONCE AGAIN\n");

if(j == 'n'||j == 'N')break;
***else sum = 0***
  • Related