calculator-like program not displaying correctly?

Hello,
I'm having issues figuring out why my program won't come out the way I want it to. It needs to 1) let the user pick 1-4 from the menu, and if the input is not 1-4, display message and end program. 2) Once the user picks a number 1-4, The user inputs one number when prompted, and enters another number on the next prompt. Both prompts need to be able to tell if the input is a number or not, and if one input is not a number, the program displays a message and ends. 3) When both inputs are numbers, proceed with the problem and display it and the answer, pause, close. In the division section, it needs to recognize the second input is zero and display that you cannot divide with zero, and close. The program cannot break.

Here's my issues:
-When asked for the first number input for the problem, if input isn't a number, it first displays "Enter second number: " then displays "That is not a number." and closes. I can't have it display "Enter second number"
-When the second number prompted isn't a number, it takes the input as a zero and puts it into the equation.
-When dividing and both inputs are numbers, it displays the answer and also displays the error answer that it cannot divide by zero.

I feel that this is an organization error, but I'm not fully sure. I've been moving everything around for a while now.

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
#include "stdafx.h"
#include "string"
#include "iostream"
#include "iomanip"
using namespace std;


int main()
{
	int choice;
	int Anum1;
	int Anum2;
	int Snum1;
	int Snum2;
	int Mnum1;
	int Mnum2;
	int Dnum1;
	int Dnum2;
	int subAns;
	int addAns;
	int multAns;
	int divAns;

	cout << "What would you like to do?\n\n";
	cout << "1.       Add two numbers \n2.       Subtract 2 numbers \n3.       Multiply 2 nunbers \n4.       Divide 2 numbers\n";
	cout << "\nPlease enter your selection: ";
	cin >> choice;

	if (choice == 1, 2, 3, 4)
	{
		//======================================================================================
		//choice1
		if (choice == 1)
		{
			cout << "Enter the first number: ";
			cin >> Anum1;
			if (Anum1 >= 0)
			{
				cout << "Enter the second number: ";
				cin >> Anum2;

				addAns = Anum1 + Anum2;

				if (Anum2 >= 0)
				{
					cout << "\nYou chose to perform the following operation:\n" << Anum1 << " + " << Anum2 << " = " << addAns << "\n\n";
					cout << "Thanks for using the program. \nGoodbye\n\n";
				}
				else
				{
					cout << "That is not a number.\n\n";
				}
			}
			else
			{
				cout << "That is not a number.\n\n";
			}

		}
		//======================================================================================
		//choice2
		if (choice == 2)
		{
			cout << "Enter the first number: ";
			cin >> Snum1;
			if (Snum1 >= 0)
			{
				cout << "Enter the second number: ";
				cin >> Snum2;

				subAns = Snum1 - Snum2;

				if (Snum2 >= 0)
				{
					cout << "\nYou chose to perform the following operation:\n" << Snum1 << " - " << Snum2 << " = " << subAns << "\n\n";
					cout << "Thanks for using the program. \nGoodbye\n\n";
				}
				else
				{
					cout << "That is not a number.\n\n";
				}
			}
			else
			{
				cout << "That is not a number.\n\n";
			}

		}
		//======================================================================================
		//choice3
		if (choice == 3)
		{
			cout << "Enter the first number: ";
			cin >> Mnum1;
			if (Mnum1 >= 0)
			{
				cout << "Enter the second number: ";
				cin >> Mnum2;

				multAns = Mnum1 * Mnum2;

				if (Mnum2 >= 0)
				{
					cout << "\nYou chose to perform the following operation:\n" << Mnum1 << " x " << Mnum2 << " = " << multAns << "\n\n";
					cout << "Thanks for using the program. \nGoodbye\n\n";
				}
				else
				{
					cout << "That is not a number.\n\n";
				}
			}
			else
			{
				cout << "That is not a number.\n\n";
			}

		}
		//======================================================================================
		//choice4
		if (choice == 4)
		{
			cout << "Enter the first number: ";
			cin >> Dnum1;
			if (Dnum1 >= 0)
			{
				cout << "Enter the second number: ";
				cin >> Dnum2;

				if (Dnum2 > 0)
				{
					divAns = Dnum1 / Dnum2;
					cout << "\nYou chose to perform the following operation:\n" << Dnum1 << " / " << Dnum2 << " = " << divAns << "\n\n";
					cout << "Thanks for using the program. \nGoodbye\n\n";
				}
				if (Dnum2 == 0)
				{
					cout << "\nYou are trying to break my program. You cannot divide by 0.\n\n";
				}
				else
				{
					cout << "That is not a number.\n\n";
				}

			}
			else
			{
				cout << "That is not a number.\n\n";
			}

		}

	}
	else 
	{
		cout << "\nYou broke my program. You need to enter a number between 1 and 4.\n\n";
	}

	system("pause");
    return 0;
}
Last edited on
This is incorrect, and unnecessary.
 
if (choice == 1, 2, 3, 4)

The final else really should be part of that inner if statement, then you can loose the whole outer one.

Both prompts need to be able to tell if the input is a number or not
Have a look at strtol. It returns a long from a string, but also tells where the parser stopped, so you can tell if it wasn't a number indirectly. https://en.cppreference.com/w/cpp/string/byte/strtol
Last edited on
Yes, your problem is in organization.
Have you done functions?

Try to complete this
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
#include <iostream>
#include <climits>
using std::cout;
using std::cin;
using std::endl;

void displayMenu();
int getNumber();

// if you are comfortable with functions you can do these
void doAddition(int num1, int num2);
void doSubtraction(int num1, int num2);
void doMultiplication(int num1, int num2);
void doDivision(int num1, int num2);

int main()
{
    displayMenu();
    int choice = getNumber();
    if(choice >= 1 && choice <= 4) // valid choice
    {
        int num1 = getNumber();
        int num2 = getNumber();
        
        switch(choice)
        {
            case 1:
            // do addition stuff
            // doAddition(num1, num2);
            break;
            case 2:
            // do subtraction
            break;
            case 3:
            // do multiplication stuff
            break;
            case 4:
            // do division stuff
            break;
    
        }
        
    }else{ // invalid choice
        // do stuff
    }

  return 0;
}

void displayMenu()
{
    cout << "What would you like to do?\n\n";
	cout << "1. Add two numbers\n"
        <<"2. Subtract 2 numbers\n"
        <<"3. Multiply 2 nunbers\n"
        <<"4. Divide 2 numbers\n"
        << endl;
}

// credit goes to 
// https://stackoverflow.com/questions/18567483/c-checking-for-an-integer?answertab=votes#tab-top
int getNumber()
{
    int number;
    while(1) { // <<< loop "forever"
    cout << "Please enter an integer: ";
    cin >> number;

    if (cin.good()) // if int break the loop
    {
        break;
    }
    else
    {
        cout << "Enter an integer greater than 0.";
        cin.clear();
        cin.ignore(INT_MAX, '\n'); // NB: preferred method for flushing cin
    }
    }
    return number;
}
Last edited on
I removed
1
2
3
4
5
6
7
8
int Anum1;
int Anum2;
int Snum1;
int Snum2;
int Mnum1;
int Mnum2;
int Dnum1;
int Dnum2;
,
this
 
if (choice == 1, 2, 3, 4)
,
and moved cout << "\nYou broke my program. You need to enter a number between 1 and 4.\n\n"; to the top and made it just an if statement. Works perfectly now.
Last edited on
your code format hurts my eye.
Topic archived. No new replies allowed.