Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
January 21st, 2013, 12:44 PM
 blobman23
Registered User

Join Date: Nov 2012
Posts: 16
Time spent in forums: 2 h 41 m 15 sec
Reputation Power: 0
I need help with an assignment.

Here is my assignment:

Your task for this assignment is to write a simple calculator that can add, subtract, divide, multiply, and square numbers inputted by the user.

Your program should first ask the user for the first number. The program should then loop, showing the user a menu like this:

Running Total: 0
Please choose one of the following options:
2. Subtract
3. Multiply
4. Divide
5. Square
0. Exit the program

The first line of the menu should display the running total, the result of all previous operations.

Your program should take an integer as input. If the user chooses 0, the program should exit.

If the user chooses 1, 2, 3, or 4, the program should ask for a new number and then perform that operation.

If the user chooses 5, the program should multiply the running total by itself and display the new running total and menu.

******************************************

I am having trouble trying to figure out how to do the running total, and also how to do the squaring function so that it will not look to prompt the second number and will just square the running total.

******************************************

Here is my code so far:

Code:
```#include <iostream>
using namespace std;

// Program starts in main()
int main()
{
// Declare Variables:
double num1;
double num2;
int action;
double result;
double runningTotal = 0;

do {
// Get the fisrt number, second number, and operator.
cout << "Running Total is: " << runningTotal << endl;
cout << "Enter Your fisrt number: ";
cin >> num1;

cout << "Please choose one of the following options:" << endl
<< "2.Subtract" << endl
<< "3.Multiply" << endl
<< "4.Divide" << endl
<< "5.Square" << endl
<< "0.Exit the program" << endl;
cin >> action;

cout << "Enter your second number: ";
cin >> num2;

// Calculations

if (action == 1)
{
result = num1 + num2;
}
if (action == 2)
{
result = num1 - num2;
}
if (action == 3)
{
result = num1 * num2;
}
if (action == 4)
{
result = num1 / num2;
}
if (action == 5)
{
result = runningTotal * runningTotal;
}
cout << "Your total is: " << result << endl;
} while (action != 0);

}```

#2
January 21st, 2013, 01:35 PM
 salem
Contributed User

Join Date: Jun 2005
Posts: 4,260
Time spent in forums: 2 Months 4 Weeks 1 Day 15 h 3 m 57 sec
Reputation Power: 1827
I think you need to delete these lines
Code:
```		cout << "Enter your second number: ";
cin >> num2;```

And change the calculations to be something like
Code:
```		// Calculations
if (action == 1)
{
runningTotal = runningTotal + num1;
}```
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper

 Viewing: Dev Shed Forums > Programming Languages > C Programming > I need help with an assignment.