#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    16
    Rep 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:
    1. Add
    2. Subtract
    3. Multiply
    4. Divide
    5. Square
    0. Exit the program
    Your choice:

    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
    		   	<< "1.Add" << endl
    		 	<< "2.Subtract" << endl
    		 	<< "3.Multiply" << endl
    		 	<< "4.Divide" << endl
    		 	<< "5.Square" << endl
    		 	<< "0.Exit the program" << endl;
    		cout << "Your choice: ";
    		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. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,413
    Rep Power
    1871
    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

IMN logo majestic logo threadwatch logo seochat tools logo