Problem with Program

I have to write c++ program using a do while or while loop that will allow a user to input two integers, calculate and print the sum of the integers, then ask the user if he/she wishes to perform the process again.

Variables to be used.
choice (used to hold the user’s response to whether or not they wish to add two more numbers.
num1 (used to hold the integer value of the first number entered.
num2 (used to hold the integer value of the second number entered.
total (used to store the sum of num1 and num2.


I wrote a program but i don't know how to post it in here.

How can i ask the user to perform the process again?
what you would want to do is make a while(true) loop. It'll loop, ask for 2 numbers, and pop out the answer. Then write continue=cout<<"Would you like to do this process again? Yes or No:"<<endl; and make sure you define continue as a bool at the top of the program. Then write an if statement that breaks our of the while loop if the answer was no. You won't need an else because if the answer is yes then the while loop will reiterate.
#include <iostream>

using namespace std;

int main()

{
int choice, num1, num2, sum;
char choice = 'y';
sum=num1+num2;

while (choice== 'y')
{
cout << "Enter first number: ";
cin >> num1;
cout << "Enter second number: ";
cin >> num2;
cout << "Their sum is: ";
cout << "Do you want to do perform this task again? (y or n):" <<endl;
cin >> choice;
if (choice != 'y')
break;
}
return 0;

}

I keep getting an error and i can't figure it out.
Last edited on
you declare the var choice twice. And num1, num2, sum seem not to be initialized.
modify line 8 to int num1 = 0, num2 = 0, sum = 0;
still some logic error exists. hoping this will help you:)
Last edited on
Topic archived. No new replies allowed.