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>
usingnamespace 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";
}
elseif (ans == '2')
{
cout << "The subtraction: " << num1 <<"-"<< num2 <<"=" << a2(num1, num2) << "\n";
}
elseif (ans == '3')
{
cout <<"The multiplication: " << num1 <<"*"<< num2 <<"=" << a3(num1, num2) << "\n";
}
elseif (ans == '4')
{
cout << "The division: " << num1 <<"/"<< num2 <<"=" << a4(num1, num2) << "\n";
}
elseif(ans == '5')
{
cout <<"ALL\n";
a5 (num1,num2);
}
elseif (ans == '6')
{
cout << "do you want to make value changes(y/n)?\n";
cin >>changes;
if (changes =='y')
{
x = 1;
}
else
{
x = 0;
}
}
elseif(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
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
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.