So my program is running just fine and doing what it should beside one little thing. That thing is when I run the formula on the for loop. What it does is when I put in 5 and select the for loop operation it gives me the right answer. But when the program asks if I want to try again and I do the 5 again in the for loop it adds those two together so it gives me 30. How do I go about clearing that variable? My formula case works just fine doesnt add or anything when I do it over.

#include <iostream>
using namespace std;

int num;
int total = 0;
char c;
int counter;

int main(){

    do{
        cout <<"\n---------------------------------------" << endl;
        cout <<"Enter a Number (Natural/Whole/Integer)" << endl;
        cout <<"---------------------------------------" << endl;
        cin >> num;

        cout << "\nHow would you like to calculate the sum?\n" << endl;
        cout << "1. For-Loop" << endl;
        cout << "2. Formula" << endl;

        cout << "\nSelect an operation: ";
        cin >> c;
        cin.ignore();

        switch(c){

        case '1':

            for (counter = 1; counter <= num; counter++){

                total += counter;
            }

            cout <<"Your sum using For-Loop is: "<< total;
            break;

        case '2':

            total = num * (num + 1) / 2;
            cout <<"Your sum using formula is: " << total << endl;
            break;


        }   

        cout << "\nWould you like to try again? (y/n): ";
        cin >> c;

    }

    while(c == 'y' || c == 'Y');

    return 0;
}

Dani AI

Generated

Short version for : your running total keeps growing because the accumulator lives across repeats. As pointed out, the fix is to reinitialize the accumulator each time you run the sum — but there are a few cleaner, safer ways to do that and some extra pitfalls to watch for.

Why it happens and what to do

  • In your code total is defined outside the repeated block, so total += … adds onto the previous value on every retry. Make the accumulator local to the calculation (declare it inside the loop or inside the case that does the for‑loop) so it is recreated and starts at zero each time. That avoids manual clears.
  • You already have a formula option; that’s O(1) instead of O(n). Either approach is fine, but be mindful of integer overflow: for 32‑bit signed int the sum n(n+1)/2 overflows when n > 65535. If larger n are possible, use a 64‑bit integer (long long/int64_t) and validate num is non‑negative.

Other practical improvements

  • Don’t reuse the same char for the menu choice and the “try again” answer; separate variables make the code clearer and avoid accidental reuse.
  • Avoid system("cls") — it’s platform specific and a security risk; printing a few newlines is portable and safer.
  • Add basic input validation (check for cin failure, clear and ignore bad input, or use getline + parsing) so stray characters don’t break the flow.
  • Prefer small helper functions (e.g., sumTo(n)) and keep variables with the smallest useful scope. That makes the code easier to test and reason about.

Quick checklist

  1. Move the accumulator into the calculation scope. 2) Validate num (non‑negative). 3) Use 64‑bit types if inputs can be large. 4) Use separate variables for menu/retry and add input checks. Following these will stop the “5 then 5 → 30” behavior and make the program more robust.

Recommended Answers

All 3 Replies

First off. I suggest adding a

system("cls");

at the end of the do-while loop. Otherwise it'll just clutter the screen if it's run multiple times.
The problem with you code is the brackets. You're missing a few brackets and others aren't correctly placed. If you can't figure it out feel free to PM me and I'll help you out
Have fun :D

The only thing you'll need to do here is to reset total to zero at the beginning of your loop. That action alone will fix your code.

You're missing a few brackets and others aren't correctly placed.

There are no missing brackets here, and the brackets aren't placed incorrectly at all. What he's using is an indentation style known as K&R styling, which is a fairly common way of writing C, C++, and even Java code.

Oh you're right about resetting total to 0. I totally missed that haha.
And I know about the different ways of indenting, it just looked to me like there are some missing. Plus when I ran it after adding what I thought needed to be fixed it worked fine. - besides it not resetting haha

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.