sefisko
sefisko

Reputation: 35

How to reset number to 0 after running program once again

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!

Upvotes: 1

Views: 575

Answers (2)

Basant Khatiyan
Basant Khatiyan

Reputation: 204

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

Upvotes: 0

dbush
dbush

Reputation: 224207

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

Upvotes: 1

Related Questions