Function Exiting

Fellow programmers,
I am requesting assistance on a small problem I am experiencing with this calculator program. I wanted the user to have the ability to input an unknown number of integers and then be able to see the solution. Once the solution is displayed, I would like the program to take the user back to the switch statement so the user could continue the program with other math functions like subtraction, multiplication, or division or exit. The trouble I receive is after the results are shown in the addition function; once the function displays the solution, the function will return to the switch statement (which is a good thing) and then go back into the addition function and crash. I know I have not written the other functions, because I am stuck at this junction. Thanks for taking your personal time to look at this obstacle for me. All recommendations are welcome. Thanks


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <iostream>     //library for cout
#include <math.h>       //library for math functions
#include <string>       //library for string functions

using namespace std;

//this enum function is for the witch statement
enum Arthmetic {ADD =1, SUB, MULT, DIV, EXIT};

//This function includes instructions to how to use the program
//Pre: Will have switch function
//Post: None
void WelcomeScreen ();

//This function will have some fun ocular art in the shape of a calculator.
//Pre: None.
//Post: Will display a calculator.
void WelcomeArt ();

//This function will add an unlimited number of intergers together
//Pre: Will declare int variables sum and numVal.
//Post: Will add numbers and provide the solution.
string Addition (int sum, int numVal);

//This function will subtract an unlimited number of integers together
//Pre: Will declare int variables sum and numVal.
//Post: Will subtract numbers and provide the solution.
int Subtraction (int sum);

//This function will multiply an unlimited number of integers together
//Pre: Will declare int variables sum and numVal.
//Post: Will multiply numbers and provide the solution.
int Multiplication (int sum);

//This function will divide an unlimited number of integers together
//Pre: Will declare int variables sum and numVal.
//Post: Will divide numbers and provide the solution.
int Division (int sum);

//This function will display the solution to the math problems
//Pre: none.
//Post: Will display the solution to the math problems.
void DisplayResult (int sum);

int main (){

    int sum;                //local variable
    int numVal;             //local variable

    //Function calls the welcome screen
    WelcomeScreen();

    return 0;
}

void WelcomeScreen() {

    int sum;
    int numVal;
    int choice;

    do {

        cout << "Please choose from the options below. \n"
             << "For ADDITION press [1] \n"
             << "For SUBTRACTION press [2] \n"
             << "For MULTIPLICATION press [3] \n"
             << "For DIVISION press [4] \n"
             << "To EXIT the program [5] \n";
        cin >> choice;

        switch (choice) {

            case ADD:
                Addition(sum, numVal);
                cout << endl;
                break;

            case SUB:
                Subtraction(sum);
                break;

            case MULT:
                Multiplication(sum);
                break;

            case DIV:
                Division(sum);
                break;

            case EXIT:
                cout << "See Ya, GUY!! \n";
                break;

            default:
                cout << "You have chosen incorrectly, please try again. \n";
                break;
        }

    } while (choice != EXIT);
}

string Addition (int sum, int numVal){

    char q;             //Local Variable
    sum = 0;            //Local variable that is initialized

    cout << "Enter a positive or negative integer and then press enter to continue "
         << "adding new integers or press 'q' for solution: \n";
    cin >> numVal;

    while (! (numVal == q)) {
        cin >> numVal;
        sum = sum + numVal;
    }
    // Function call to display the results of the integers
    DisplayResult (sum);

}

int Subtraction (int sum) {

//  ++++++Test Thing++++++
    cout << "SUB works! \n\n";
//  ++++++++++++++++++++++
}

int Multiplication (int sum){

//  ++++++Test Thing++++++
    cout << "Mult works! \n\n";
//  ++++++++++++++++++++++
}

int Division (int sum){

//  ++++++Test Thing++++++
    cout << "Div works! \n\n";
//  ++++++++++++++++++++++
}

void DisplayResult (int sum) {

    //  ++++++Test Thing++++++
        cout << sum << endl;
    //  ++++++++++++++++++++++

}
Hello john26999,

The main problem is in the "Addition" function.

I have not worked on fixing it yet, but did make comments that should help:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
string Addition(int sum, int numVal) // <--- There is no real need for the parameters.
                                     // Or you could pass "sum" by reference to use back in "WelcomeScreen".
				     // Or return "sum" back to "WelcomeScreen".
{

	char q{ 'q' };      // Local Variable. Originally uninitialized and contained garbage.
	sum = 0;            // Local variable that is initialized. / Local variable that is set to zero.
	//int numVal{}; // <--- Should be this. Not defined as a parameter.

	cout << "Enter a positive or negative integer and then press enter to continue "
		<< "adding new integers or press 'q' for solution: \n";
	cin >> numVal;

	while (!(numVal == q)) // <--- Compares an "int" to a "char". Does not work the way that you think.
	{
		cin >> numVal;
		sum = sum + numVal;
	}

	// Function call to display the results of the integers
	DisplayResult(sum); // <--- Should be done in "WelcomeScreen" or better in "main".

	return "?"; // <--- You promise to return something, but you did not.
}

The next problem is line 12. This is formatted input and "cin" is expecting to put a number into the variable "numVal". When you enter "q" this causes "cin" to fail and is unusable after that point, so every "cin" you come to is passed over because it no longer works.

One possible solution is to make "numVal" or something else a "std::string" for the input and along with a try/catch change the string into an "int" with "stoi()" from the string class. If this should fail the catch block will keep the program running.

In the function definition you are passing two variables by value, i.e., only a copy. These are better defined in the function as it is written.

As the comments suggest I would loose the second parameter and consider passing "sum" by reference to be used back in "WelcomeScreen" where it was first defined.

This way you could move the "DisplayResult" function back to "WelcomeScreen" in the switch where it belongs.

Another my VS2017 compiler had was the most of the variables are defined uninitialized and then used before they have received any usable value.

You have included the header file "math.h". First you should use "cmath" and second I do not see anything like "pow()" or "sqrt()" or anything else that comes from "cmath". "cmath" is not required for general (+, -, *, / or %) arithmetic .

You could also benefit from including the "cctype" header file and using "std::tolower()" or "std::toupper()" or when you enter a letter like "q" you should check for either case.

Hope that helps,

Andy
Andy,
Thanks for taking the time to point out the errors and your solution recommendations. I will input the suggestions you made. Thanks again.
Hello john26999,

Any time.

Andy
Topic archived. No new replies allowed.