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