Error correcting user input in a 4 function calculator

I am trying to build a 4 function calculator that acknowledges and corrects input errors. If a user puts in words instead of numbers I want the program to recognize it, clear the cache, notify the user, and allow the user to try again.

The code compiles, it runs just fine and the code processes the error correction that I want in all other parts of the code (to test I input bob at each stage that the code asks for choices or values). The code does not error process when I ask "Is that correct?" in line 5-7. Please help! Thank you so much.

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
// addition function
int addition () {
  values (); 
  cout << "So what I have is " << input1 << " plus " << input2 << "." << endl;
  cout << "Is that correct? (1 if yes, 2 if no): ";
  cin >> choice;
  testmistake (); 
  if (choice == 1) {
    answer = input1 + input2; 
    cout << input1 << " + " << input2 << " = " << answer << endl; 
    repeat ();
  } else {
    cout << "Sorry about that, let's try again." << endl; 
    addition; 
  }
}

// values function
int values () {
  cout << "What is the first value you would like to use in your equation? : ";
  cin >> input1;
  while (input1 == 0) {
    mistake ();
    cout << "Sorry, your first value needs to be a number: ";
    cin >> input1;
  }
  cout << "What is the second value you would like to use in your equation? : ";
  cin >> input2; 
  while (input2 == 0) {
    mistake ();
    cout << "Sorry, your second value needs to be a number: ";
    cin >> input2;
  }
}

// check mistake function
int testmistake () {
  if (choice == 1) {
    return 0;
  }
  if (choice == 2) {
    return 0;
  }
  mistake ();
  cout << "Please enter an appropriate choice, 1 (yes) or 2 (no): ";
  cin >> choice;
  testmistake ();
}

// mistake function
int mistake () {
  cin.clear ();
  cin.ignore (10000, '\n');
}
Hi,

Can you see a problem with line 14 ?

Btw, some of your functions are not returning anything, maybe they should be void ? The compiler should have complained about that.

Are input1 and input2 global variables? If so that is bad. Pass them to the functions by reference, then their values will change in the scope of the function that calls them.
I know that line 14 calls its own function, which can be dangerous, but that is by design, the entire code might make it make more sense.
I am using code anywhere so that my students on chromebooks can code and compile in the cloud and the compiler didn't complain about anything. My few forreys into void have not worked very well for me, I am still learning a lot of this, so I don't know, maybe.
They are global variables, I am not too confident on passing variable values by reference yet, as I have only done it a couple of times.

Let me give you the entire program so you can see what is happening. Thanks for responding so quickly, as a teacher tasked with teaching things I am still learning this is very stressful and I appreciate it.



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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// libraries
#include <iostream>
#include <cmath> 
#include <cstring> 
using namespace std;

// outside functions
int prompt (); 
int prompt2 ();
int noobs (); 
int mistake ();
int testmistake ();
int values (); 
int addition ();
int subtraction ();
int multiplication ();
int division ();
int repeat ();

// create objects
double input1, input2, answer;
int choice = 0;

// main program
int main () {
  // prompt the user
  if (choice == 0) {
    prompt (); 
  } else {
    prompt2 (); 
  }
  
  // addition calculation
  if (choice == 1) {
    addition (); 
  }
  
  // subtraction calculation
  if (choice == 2) {
    subtraction ();
  }
  
  // multiplication calculation 
  if (choice == 3) {
    multiplication ();
  }
  
  // division calculation
   if (choice == 4) {
     division ();
   }
}

// prompt function
int prompt () {
  cout << "CCCC   A   L     CCCCC    +   " << endl;
  cout << "C     A A  L     C        +   " << endl;
  cout << "C    AAAAA L     C      +++++ " << endl;
  cout << "C    A   A L     C        +   " << endl;
  cout << "CCCC A   A LLLLL CCCCC    +   " << endl;
  cout << "______________________________" << endl;
  cout << "Welcome to Calc+, your friendly neighborhood calculator." << endl;
  cout << "We are able to calculate the basic 4 functions; add (1), subtract (2), multiply (3), and divide (4)." << endl;
  cout << "What would you like to do? (please enter a number equal to your chosen equation): ";
  cin >> choice; 
  while (choice < 1 || choice > 4) {
    noobs (); 
  }
}

// prompt2 function 
int prompt2 () {
  cout << "What would you like to do?" << endl;
  cout << "Add (1), Subtract (2), Multiply (3), or Divide (4)" << endl;
  cout << "Please enter a number equal to your chosen equation: ";
  cin >> choice;
  while (choice < 1 || choice > 4) {
    noobs (); 
  }
}

// noobs function 
int noobs () {
  mistake ();
  cout << "ERROR: Please enter a value between 1 and 4, for our functions options;" << endl;
  cout << "Add is 1" << endl;
  cout << "Subtract is 2" << endl;
  cout << "Multiply is 3" << endl;
  cout << "Divide is 4" << endl;
  cout << "Please pick one of the above options: ";
  cin >> choice; 
}

// mistake function
int mistake () {
  cin.clear ();
  cin.ignore (10000, '\n');
}

// values function
int values () {
  cout << "What is the first value you would like to use in your equation? : ";
  cin >> input1;
  while (input1 == 0) {
    mistake ();
    cout << "Sorry, your first value needs to be a number: ";
    cin >> input1;
  }
  cout << "What is the second value you would like to use in your equation? : ";
  cin >> input2; 
  while (input2 == 0) {
    mistake ();
    cout << "Sorry, your second value needs to be a number: ";
    cin >> input2;
  }
}

// check mistake
int testmistake () {
  if (choice == 1) {
    return 0;
  }
  if (choice == 2) {
    return 0;
  }
  mistake ();
  cout << "Please enter an appropriate choice, 1 (yes) or 2 (no): ";
  cin >> choice;
  testmistake ();
}

// addition function
int addition () {
  values (); 
  cout << "So what I have is " << input1 << " plus " << input2 << "." << endl;
  cout << "Is that correct? (1 if yes, 2 if no): ";
  cin >> choice;
  testmistake (); 
  if (choice == 1) {
    answer = input1 + input2; 
    cout << input1 << " + " << input2 << " = " << answer << endl; 
    repeat ();
  } else {
    cout << "Sorry about that, let's try again." << endl; 
    addition; 
  }
}

// subtraction function
int subtraction () {
  values (); 
  cout << "So what I have is " << input1 << " minus " << input2 << "." << endl;
  cout << "Is that correct? (1 if yes, 0 if no): ";
  cin >> choice;
  while (choice < 0 || choice > 1) {
      mistake ();
      cout << "Please enter an appropriate choice, 1 (yes) or 0 (no): ";
      cin >> choice;
  }
  answer = input1 - input2; 
  cout << input1 << " - " << input2 << " = " << answer << endl; 
  repeat ();
}

// multiplication function
int multiplication () {
  values (); 
  cout << "So what I have is " << input1 << " multiplied by " << input2 << "." << endl;
  cout << "Is that correct? (1 if yes, 0 if no): ";
  cin >> choice;
  while (choice < 0 || choice > 1) {
      mistake ();
      cout << "Please enter an appropriate choice, 1 (yes) or 0 (no): ";
      cin >> choice;
  }
  answer = input1 * input2; 
  cout << input1 << " * " << input2 << " = " << answer << endl; 
  repeat ();
}

// division function 
int division () {
  values (); 
  cout << "So what I have is " << input1 << " divided by " << input2 << "." << endl;
  cout << "Is that correct? (1 if yes, 0 if no): ";
  cin >> choice;
  while (choice < 0 || choice > 1) {
      mistake ();
      cout << "Please enter an appropriate choice, 1 (yes) or 0 (no): ";
      cin >> choice;
  }
  answer = input1 / input2; 
  cout << input1 << " / " << input2 << " = " << answer << endl; 
  repeat ();
}

// repeat function? 
int repeat () {
  cout << "Would you like to do another math problem?" << endl;
  cout << "Press 1 for yes, or 0 for no: ";
  cin >> choice; 
  while (choice < 0 || choice > 1) {
    mistake ();
    cout << "Please enter an appropriate value:";
    cin >> choice;
  }
  if (choice == 1) {
    main (); 
  } else {
    return 0; 
  }
}
These are the compile errors / warnings:

In function 'int prompt()': 69:1: warning: no return statement in function returning non-void [-Wreturn-type] In function 'int prompt2()': 80:1: warning: no return statement in function returning non-void [-Wreturn-type] In function 'int noobs()': 92:1: warning: no return statement in function returning non-void [-Wreturn-type] In function 'int mistake()': 98:1: warning: no return statement in function returning non-void [-Wreturn-type] In function 'int values()': 116:1: warning: no return statement in function returning non-void [-Wreturn-type] In function 'int addition()': 145:13: warning: statement is a reference, not call, to function 'addition' [-Waddress] 145:13: warning: statement has no effect [-Wunused-value] 147:1: warning: no return statement in function returning non-void [-Wreturn-type] In function 'int subtraction()': 163:1: warning: no return statement in function returning non-void [-Wreturn-type] In function 'int multiplication()': 179:1: warning: no return statement in function returning non-void [-Wreturn-type] In function 'int division()': 195:1: warning: no return statement in function returning non-void [-Wreturn-type] In function 'int repeat()': 208:11: warning: ISO C++ forbids taking address of function '::main' [-Wpedantic] In function 'int testmistake()': 130:1: warning: control reaches end of non-void function [-Wreturn-type] In function 'int repeat()': 212:1: warning: control reaches end of non-void function [-Wreturn-type]


I am using code anywhere so that my students on chromebooks can code and compile in the cloud and the compiler didn't complain about anything. My few forreys into void have not worked very well for me, I am still learning a lot of this, so I don't know, maybe.


Really? You are teaching, but you don't know much yourself?

My comment about line 14 in your OP, wasn't about a recursive function call, rather that it wasn't a function call at all because it was missing the parentheses. Recursive function calls are not appropriate here, consider using loops instead.

You can't call main like you do on line 208.

There is lots of repetition in your code.
Really? You are teaching, but you don't know much yourself?


Welcome to public schools in rural Idaho where they will have you fill a need and take classes at the same time because nobody else will come there and do it for the pay :D

My comment about line 14 in your OP, wasn't about a recursive function call, rather that it wasn't a function call at all because it was missing the parentheses. Recursive function calls are not appropriate here, consider using loops instead.


Ahh fixed that, why isn't a recursive function appropriate here? would a do-while be better? I've looked into them but have never used them.

You can't call main like you do on line 208.


Why not? it is working when I run through the program. And I realize how ignorant that sounds, just because something works doesn't mean it is right, but it is doing exactly what I want it to, and the call function is working the right way.

There is lots of repetition in your code.


I am trying to cut down on repetition, and have used the outside functions to do so, where am I still having a problem? I also realize that this is a more complicated question.

Thank you so much for your help!
I am also still wondering about my original post/topic/question.
How do I fix my error correcting so that when I input "bob" instead of an integer it will clear the cache and allow me to retry the input. It only happens in the one location and nowhere else.
Welcome to public schools in rural Idaho
God bless rural Idaho.

Please, let us know if the following code gives you the same problem.
I tried to stick to your code as much as I could. If you fancy it, you can use it for hints.

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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include <iostream>


// outside functions
int prompt ();
int prompt2 ();
int noobs ();
void values ();
void addition ();
void subtraction ();
void multiplication ();
void division ();
int repeat ();



int main ()
{
    int again { 1 };
    while(again) {
        // prompt the user
        int choice {};
        if (choice == 0) {
            choice = prompt ();
        } else {
            choice = prompt2 ();
        }

        switch(choice) {
        case 1:
            addition ();
            break;
        case 2:
            subtraction ();
            break;
        case 3:
            multiplication ();
            break;
        case 4:
            division ();
            break;
        default:
            break;
        }
        again = repeat ();                      // can return only 0 or 1
    }
}


// Display initial menu
int prompt ()
{
    std::cout << "CCCC   A   L     CCCCC    +   \n"
                 "C     A A  L     C        +   \n"
                 "C    AAAAA L     C      +++++ \n"
                 "C    A   A L     C        +   \n"
                 "CCCC A   A LLLLL CCCCC    +   \n"
                 "______________________________\n"
                 "Welcome to Calc+, your friendly neighborhood calculator.\n"
                 "We are able to calculate the basic 4 functions add (1), "
                 "subtract (2), multiply (3), and divide (4).\n"
                 "What would you like to do? "
                 "(please enter a number equal to your chosen equation): ";
    int choice;
    std::cin >> choice;
    while (choice < 1 || 4 < choice) {
        choice = noobs ();
    }
    return choice;
}


// Display second menu
int prompt2 ()
{
    std::cout << "What would you like to do?\n"
                 "Add (1), Subtract (2), Multiply (3), or Divide (4)\n"
                 "Please enter a number equal to your chosen equation: ";
    int choice;
    std::cin >> choice;
    while (choice < 1 || choice > 4) {
        choice = noobs ();
    }
    return choice;
}


// noobs function
int noobs ()
{
    std::cout << "ERROR: Please enter a value between 1 and 4, "
                 "for our functions options;\n"
                 "Add is 1\n"
                 "Subtract is 2\n"
                 "Multiply is 3\n"
                 "Divide is 4\n"
                 "Please pick one of the above options: ";
    int choice;
    std::cin >> choice;
    return choice;
}


// values function
void values (double & first, double & second)
{
    std::cout << "What is the first value you would like to use in your equation? ";
    std::cin >> first;
    while (first == 0.0) {
        std::cout << "Sorry, your first value needs to be a number: ";
        std::cin >> first;
    }
    std::cout << "What is the second value you would like to use in your equation? ";
    std::cin >> second;
    while (second == 0.0) {
        std::cout << "Sorry, your second value needs to be a number: ";
        std::cin >> second;
    }
}


// addition function
void addition ()
{
    double first {};
    double second {};
    int choice {};
    while(choice != 1) {
        values(first, second);
        std::cout << "So what I have is " << first << " plus " << second
                  << ".\nIs that correct? (1 if yes, 2 if no): ";
        std::cin >> choice;
        if(choice == 2) {
            std::cout << "Sorry about that, let's try again.\n";
            continue;
        }
        std::cout << first << " + " << second << " = " << first + second << '\n';
    }
}


// subtraction function
void subtraction ()
{
    double first {};
    double second {};
    int choice {};
    while ( choice != 1) {
        values (first, second);
        std::cout << "So what I have is " << first << " minus " << second
                  << ".\nIs that correct? (1 if yes, 0 if no): ";
        std::cin >> choice;
        while (choice < 0 || choice > 1) {
            std::cout << "Please enter an appropriate choice, 1 (yes) or 0 (no): ";
            std::cin >> choice;
        }
        if(choice == 0) { continue; }
        std::cout << first << " - " << second << " = " << first - second << '\n';
    }
}


// multiplication function
void multiplication ()
{
    double factor1;
    double factor2;
    values (factor1, factor2);
    int choice {};
    while ( choice != 1) {
        std::cout << "So what I have is " << factor1 << " multiplied by " << factor2
                  << ".\nIs that correct? (1 if yes, 0 if no): ";
        std::cin >> choice;
        while (choice < 0 || choice > 1) {
            std::cout << "Please enter an appropriate choice, 1 (yes) or 0 (no): ";
            std::cin >> choice;
        }
        if(choice == 0) { continue; }
        std::cout << factor1 << " * " << factor2 << " = " << factor1 * factor2 << '\n';
    }
}


// division function
void division ()
{
    double dividend;
    double divisor;
    values (dividend, divisor);
    int choice {};
    while ( choice != 1) {
        std::cout << "So what I have is " << dividend << " divided by " << divisor
                  << ".\nIs that correct? (1 if yes, 0 if no): ";
        std::cin >> choice;
        while (choice < 0 || choice > 1) {
            std::cout << "Please enter an appropriate choice, 1 (yes) or 0 (no): ";
            std::cin >> choice;
        }
        if(choice == 0) { continue; }
        std::cout << dividend << " / " << divisor << " = " << dividend / divisor << '\n';
    }
}


// Ask the user if they want another calculation
int repeat ()
{
    std::cout << "Would you like to do another math problem?\n"
                 "Press 1 for yes, or 0 for no: ";
    int choice;
    std::cin >> choice;
    while (choice < 0 || choice > 1) {
        std::cout << "Please enter an appropriate value:";
        std::cin >> choice;
    }
    return choice;
}

Ahh fixed that, why isn't a recursive function appropriate here?


Recursive functions need to have a base case in order to exit the function. Although one could organise it like that, it's better to do loops, because it's clearer.

Why not? it is working when I run through the program.


One shouldn't call main, because:

It's prohibited in the C++ standard;
It can lead to a stackoverflow (each time it's called, it creates a new stack frame);
It leads to spaghetti code.

would a do-while be better?


My opinion on do loops:

Just because they always execute at least once, shouldn't be the sole reason for using them. All 3 types of loops can be converted from one to another, so it's easy to write a do loop as a while loop. In Kernighan & Ritchie (the inventors of the C language), they don't like them because they are more error prone. The example they use for when a do loop might be required, is when something must be written even when the target is empty, like a C string must be null terminated even if otherwise empty.

Topic archived. No new replies allowed.