add, multiply, subtract, divide

Hey guys, My assignment is to do simple add, subtract , multiply and divide. The user gets to choose what they want to do by choosing from the menu. I have done everything, but i am having trouble with option 6. In this option, the user is able to change the values again if they want to. Can someone help me out, how i can fix that option. The program compiles, but when i decide option 6, it does not loop back to the start of the program.


#include <stdlib.h>
#include <iostream>
#include <iomanip>

using namespace std;
//functions
float a1 (float num1, float num2)
{
float add;
add = num1 + num2;
return add;
}
float a2 (float num1, float num2)
{
float subtract;
subtract = num1 - num2;
return subtract;
}
float a3 (float num1, float num2)
{
float multiply;
multiply = num1 * num2;
return multiply;

}
float a4 (float num1, float num2)
{
float divide;
divide = num1 / num2;
return divide;
}
float a5 (float num1, float num2)
{
cout <<num1<< "+" <<num2<< "=" << a1 (num1,num2)<<"\n";
cout <<num1<< "-" <<num2<< "=" << a2(num1,num2)<<"\n";
cout <<num1<< "*" <<num2<< "=" << a3(num1,num2)<<"\n";
cout <<num1<< "/" <<num2<< "=" << a4(num1,num2)<<"\n";
}

int main()
{
char changes;
int x=1;
char again ='y';
float num1, num2;
char ans;
while(again == 'y')
if (x == 1)
{
cout << "Please enter the first number: \n";
cin >> num1;
cout << "Please enter the second number: \n";
cin >> num2;
if (num2 == 0)
{
cout << "Cannot devide by zero\n";
cout << "defaulting num2 to 1\n";
num2 = 1;
}

cout << "1-Add:"<< num1 << "+" <<num2 << endl
<< "2-Subtract:" << num1 << "-" <<num2 << endl
<< "3-Multiplication:" << num1 << "*" <<num2 << endl
<< "4-Divide:" << num1 << "/" <<num2 << endl
<< "5-All:" <<("+,-,*,/") <<endl
<< "6-Change Values" <<endl
<< "7-Quit" <<endl;

cout<<"Now enter the corresponding number for the operation you desire to do(1 - 7)\n";
cin>>ans;

if (ans=='1')
{
cout<< "The addition:" << num1 << " + " << num2 << " = " << a1(num1, num2) << "\n";
}
else if (ans=='2')
{
cout << "The subtraction: " << num1 <<"-"<< num2 <<"=" << a2(num1, num2) << "\n";
}
else if (ans == '3')
{
cout <<"The multiplication: " << num1 <<"*"<< num2 <<"=" << a3(num1, num2) << "\n";
}
else if (ans =='4')
{
cout << "The division: " << num1 <<"/"<< num2 <<"=" << a4(num1, num2) << "\n";
}
else if(ans=='5')
{
cout <<"ALL\n";
a5 (num1,num2);
}
else if (ans=='6')
{
cout << "do you want to make value changes(y/n)?\n";
cin >>changes;
if (changes =='y')
{
x = 1;
}
else
{
x = 0;
}
}
else if(ans=='7')
{
exit (0);
}
else
{
cout <<"Invalid choice\n";
}
cout << "Would you like to make another selection(y/n)?\n";
cin >>again;
}
}
Hi hello321!

Please always use code tags (the <> icon on the right of the text box), it makes it easier for people to read your code.

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
#include <stdlib.h>
#include <iostream>
#include <iomanip>

using namespace std;

//functions
float a1 (float num1, float num2)
{
	float add;
	add = num1 + num2;
	
	return add;
}

float a2 (float num1, float num2)
{
	float subtract;
	subtract = num1 - num2;
	
	return subtract;
}

float a3 (float num1, float num2)
{
	float multiply;
	multiply = num1 * num2;
	
	return multiply;
}

float a4 (float num1, float num2)
{
	float divide;
	divide = num1 / num2;
	
	return divide;
}

float a5 (float num1, float num2)
{
	cout <<num1<< "+" <<num2<< "=" << a1 (num1,num2)<<"\n";
	cout <<num1<< "-" <<num2<< "=" << a2(num1,num2)<<"\n";
	cout <<num1<< "*" <<num2<< "=" << a3(num1,num2)<<"\n";
	cout <<num1<< "/" <<num2<< "=" << a4(num1,num2)<<"\n";
}



int main()
{
	char changes;
	int x = 1;
	char again ='y';
	float num1, num2;
	char ans;
	
	while(again == 'y')
	{//missing bracket here, your while loop were only applying to your following if statement
		if (x == 1)
		{
			cout << "Please enter the first number: \n";
			cin >> num1;
			cout << "Please enter the second number: \n";
			cin >> num2;
			
			if (num2 == 0)
			{
				cout << "Cannot devide by zero\n";
				cout << "defaulting num2 to 1\n";
				num2 = 1;
			}
		}//missing closing bracket for if statement
		
		
	
		cout << "1-Add: "<< num1 << " + " <<num2 << endl
		<< "2-Subtract: " << num1 << " - " <<num2 << endl
		<< "3-Multiplication: " << num1 << " * " <<num2 << endl
		<< "4-Divide: " << num1 << " / " <<num2 << endl
		<< "5-All: " << " +,-,*,/ " <<endl
		<< "6-Change Values" <<endl
		<< "7-Quit" <<endl;
	
		cout << "Now enter the corresponding number for the operation you desire to do(1 - 7)\n";
		cin >> ans;
	
		if (ans == '1')
		{
			cout<< "The addition:" << num1 << " + " << num2 << " = " << a1(num1, num2) << "\n";
		}
		else if (ans == '2')
		{
			cout << "The subtraction: " << num1 <<"-"<< num2 <<"=" << a2(num1, num2) << "\n";
		}
		else if (ans == '3')
		{
			cout <<"The multiplication: " << num1 <<"*"<< num2 <<"=" << a3(num1, num2) << "\n";
		}
		else if (ans == '4')
		{
			cout << "The division: " << num1 <<"/"<< num2 <<"=" << a4(num1, num2) << "\n";
		}
		else if(ans == '5')
		{
			cout <<"ALL\n";
			a5 (num1,num2);
		}
		else if (ans == '6')
		{
			cout << "do you want to make value changes(y/n)?\n";
			cin >>changes;
			
			if (changes =='y')
			{
				x = 1;
			}
			else
			{
				x = 0;
			}
		}
		else if(ans == '7')
		{
			break;
		}
		else
		{
			cout <<"Invalid choice\n";
			cout << "Would you like to make another selection(y/n)?\n";
			cin >> again;
		}
	}
	
	return 0;
}


You were missing a couple of brackets, and I think this is why your program wasn't doing what you wanted.

A couple of things to node
_Your main doesn't have a return statement
_Your output on console is crammed and difficult to read
_If you know how switches work, you might as well use one here, it would make your life easier :) (https://en.cppreference.com/w/cpp/language/switch)
_Your function float a5(floas num1, float num2) could be void since its return value isn't used and it merely couts on console
_You could check if num2 is equal to zero within the divide function, so that you can still perform other operations with 0
_Instead of using if(x == 1) to determine whether the user enters those numbers again, you could use a bool
_Naming your function here would be a good idea, so that you get an idea of what they do when you call them, a1, a2, a3 isn't very intuitive

Hope this helps you,

Regards,

Hugo.
Hey hugo,
Thank you so much for helping! Once i fixed the division part so it changes 0 to 1 only for that selection, I ran the program. The only issue I am getting is that, once the selection has been made from the menu(i e addition), It still re runs the program all over again, instead of asking the user if they want to make another selection and letting the user decide. How can i fix this issue ?
thank you
How can i fix this issue ?

If you show us your updated code, we'll be able to give advice.
Last edited on
You ask if user wants to restart only when the input is wrong, you need to do in regardless.
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
#include <stdlib.h>
#include <iostream>
#include <iomanip>

using namespace std;

//functions
float a1 (float num1, float num2)
{
	float add;
	add = num1 + num2;
	
	return add;
}

float a2 (float num1, float num2)
{
	float subtract;
	subtract = num1 - num2;
	
	return subtract;
}

float a3 (float num1, float num2)
{
	float multiply;
	multiply = num1 * num2;
	
	return multiply;
}

float a4 (float num1, float num2)
{
	float divide;
	divide = num1 / num2;
	
	return divide;
}

float a5 (float num1, float num2)
{
	cout <<num1<< "+" <<num2<< "=" << a1 (num1,num2)<<"\n";
	cout <<num1<< "-" <<num2<< "=" << a2(num1,num2)<<"\n";
	cout <<num1<< "*" <<num2<< "=" << a3(num1,num2)<<"\n";
	cout <<num1<< "/" <<num2<< "=" << a4(num1,num2)<<"\n";
}



int main()
{
	char changes;
	int x = 1;
	char again ='y';
	float num1, num2;
	char ans;
	
	while(again == 'y')
	{//missing bracket here, your while loop were only applying to your following if statement
		if (x == 1)
		{
			cout << "Please enter the first number: \n";
			cin >> num1;
			cout << "Please enter the second number: \n";
			cin >> num2;
			
			if (num2 == 0)
			{
				cout << "Cannot devide by zero\n";
				cout << "defaulting num2 to 1\n";
				num2 = 1;
			}
		}//missing closing bracket for if statement
		
		
	
		cout << "1-Add: "<< num1 << " + " <<num2 << endl
		<< "2-Subtract: " << num1 << " - " <<num2 << endl
		<< "3-Multiplication: " << num1 << " * " <<num2 << endl
		<< "4-Divide: " << num1 << " / " <<num2 << endl
		<< "5-All: " << " +,-,*,/ " <<endl
		<< "6-Change Values" <<endl
		<< "7-Quit" <<endl;
	
		cout << "Now enter the corresponding number for the operation you desire to do(1 - 7)\n";
		cin >> ans;
	
		if (ans == '1')
		{
			cout<< "The addition:" << num1 << " + " << num2 << " = " << a1(num1, num2) << "\n";
		}
		else if (ans == '2')
		{
			cout << "The subtraction: " << num1 <<"-"<< num2 <<"=" << a2(num1, num2) << "\n";
		}
		else if (ans == '3')
		{
			cout <<"The multiplication: " << num1 <<"*"<< num2 <<"=" << a3(num1, num2) << "\n";
		}
		else if (ans == '4')
		{
			cout << "The division: " << num1 <<"/"<< num2 <<"=" << a4(num1, num2) << "\n";
		}
		else if(ans == '5')
		{
			cout <<"ALL\n";
			a5 (num1,num2);
		}
		else if (ans == '6')
		{
			cout << "do you want to make value changes(y/n)?\n";
			cin >>changes;
			
			if (changes =='y')
			{
				x = 1;
			}
			else
			{
				x = 0;
			}
		}
		else if(ans == '7')
		{
			break;
		}
		else
		{
			cout <<"Invalid choice\n";
		}
		
		cout << "Would you like to make another selection(y/n)?\n";
		cin >> again;
	}
	
	return 0;
}
It sounds like you want the code at lines 62-72 to run at line 116. As a first try, just copy the code there. Once it works, try to make the code a function and call it from lines 116 and 62.
Topic archived. No new replies allowed.