Michael Rice
Michael Rice

Reputation: 27

Switch menu calculator will not display arithmetic

Sorry if the title is confusing, I did not know how to word my issue very well. I understand some of my program is not finished as of right now, I'm currently only working on the keyboard input sections.

Basically, I have to create a program that allows the user to either input a file with set integers or have the user enter two integers of their own. This user is also asked which arithmetic they would like performed on their integers. I created a menu and sub menus using a switch statement that would allow the user to easily navigate to their destination.

My problem is, when I try to use the input through keyboard option, my program fails to display the calculated variable. I can fully navigate to the option and even input my integers but when the program displays the final answer it states: "The total is: menu" and then it kicks me back to the main menu.

My specific example: User selects (2) for keyboard input. User selects (1) for addition arithmetic User enters an integer (1) User enters another integer (2) Program displays "The total is: Menu" Program loops back to main menu.

Here is my code:

#include "complx.h"
#include <iostream> using namespace std;

ifstream infile ("in.dat");

int main() {    
    int choiceOne, choiceOneSubMenu, choiceTwoSubMenu, digitOne, digitTwo, digitTotal;  
    bool menu = true;   
    do{         
        cout <<  "Menu \n";         
        cout << "=========== \n";

        cout << "(1) Input from a file \n";         
        cout << "(2) Input from the keyboard \n";       
        cout << "(3) Exit the program \n";

        cout << "Enter a numerical selection:  \n";         
        cin >> choiceOne;
        switch (choiceOne)      {           
            case 1:
                cout << "You chose input from a file \n";
                cout << "=============== \n";

                cout << "Which arithmetic would you like applied? \n";

                cout << "(1) Addition + \n";
                cout << "(2) Subtraction - \n";
                cout << "(3) Multiplication * \n";
                cout << "(4) Division / \n";

                cout << "Enter a numerical selection: \n";
                cin >> choiceOneSubMenu;

                switch (choiceOneSubMenu) {
                    case 1:
                        break;
                    case 2:
                        break;
                    case 3:
                        break;
                    case 4:
                        break;
                    }           
                break;
            case 2:
                cout << "You chose input from the keyboard \n";
                cout << "=============== \n";

                cout << "Which arithmetic would you like applied? \n";

                cout << "(1) Addition + \n";
                cout << "(2) Subtraction - \n";
                cout << "(3) Multiplication * \n";
                cout << "(4) Division / \n";

                cout << "Enter a numerical selection: \n";
                cin >> choiceTwoSubMenu;

                switch (choiceTwoSubMenu)
                {
                    case 1:
                        cout << "You chose addition \n";
                        cout << "=============== \n";

                        cout << "Enter your first integer: \n";
                        cin >> digitOne;
                        cout << "Enter your second integer: \n";
                        cin >> digitTwo;
                        digitTotal = (digitOne + digitTwo);

                        cout << "The total is: " + digitTotal;
                        break;
                    case 2:
                        cout << "You chose subtraction \n";
                        cout << "=============== \n";

                        cout << "Enter your first integer: \n";
                        cin >> digitOne;
                        cout << "Enter your second integer: \n";
                        cin >> digitTwo;
                        digitTotal = (digitOne - digitTwo);

                        cout << "The total is: " + digitTotal;
                        break;

                    case 3:
                        cout << "You chose multiplication \n";
                        cout << "=============== \n";

                        cout << "Enter your first integer: \n";
                        cin >> digitOne;
                        cout << "Enter your second integer: \n";
                        cin >> digitTwo;
                        digitTotal = (digitOne * digitTwo);

                        cout << "The total is: " + digitTotal;
                        break;

                    case 4:
                        cout << "You chose division \n";
                        cout << "=============== \n";

                        cout << "Enter your first integer: \n";
                        cin >> digitOne;
                        cout << "Enter your second integer: \n";
                        cin >> digitTwo;
                        digitTotal = (digitOne / digitTwo);

                        cout << "The total is: " + digitTotal;
                        break;
                }
                break;

            case 3:
                cout << "You have chosen to exit";      
        }
    }while(choiceOne!=3);
}

Upvotes: 0

Views: 147

Answers (1)

Taheri
Taheri

Reputation: 324

cout << "The total is: " + digitTotal;

you can't simply convert digit to string and concatenate it using + operator.

Edit: you can do it like this:

cout << "The total is: " << itoa(digitTotal);

or these

Upvotes: 1

Related Questions