Hi, I'm a newbie learning C++ on my own
So far I've traveled to this if else nested loop topic
I've even created a program on my own
But it seems to have an error in it
It is executable, but it does not run as I intended it to
#include<iostream>
usingnamespace std;
int main()
{
cout << "Welcome to Math Solving Program" << endl;
cout << "Please choose your category:(1,2,3,4)" << endl;
cout << "1.Addition" << endl;
cout << "2.Subtraction" << endl;
cout << "3.Multiplication" << endl;
cout << "4.Division" << endl;
int input, a, b, total;
cin >> input;
if (input = 1)
{
cout << "Please enter your value A" << endl;
cin >> a;
cout << "Please enter your value B" << endl;
cin >> b;
total = a + b;
cout << "The answer of " << a << " add " << b << " is equal to " << total << endl;
}
elseif (input = 2)
{
cout << "Please enter your value A" << endl;
cin >> a;
cout << "Please enter your value B" << endl;
cin >> b;
total = a - b;
cout << "The answer of " << a << " subtract " << b << " is equal to " << total << endl;
}
elseif (input = 3)
{
cout << "Please enter your value A" << endl;
cin >> a;
cout << "Please enter your value B" << endl;
cin >> b;
total = a * b;
cout << "The answer of " << a << " multiply " << b << " is equal to " << total << endl;
}
elseif (input = 4)
{
cout << "Please enter your value A" << endl;
cin >> a;
cout << "Please enter your value B" << endl;
cin >> b;
total = a / b;
cout << "The answer of " << a << " divide " << b << " is equal to " << total << endl;
}
elseif (input != 1 && 2 && 3 && 4)
{
cout << "Please enter a valid selection" << endl;
}
system("pause");
}
The output of the program will only be the result from the first if statement
Anyone care to enlighten me? Thanks!
But it seems to have an error in it
It is executable, but it does not run as I intended it to
That is not a very helpful description of your problem. In what way is it not working as intended? What behaviour are you seeing? How does that differ from intended behaviour?
3) Some things I've noticed:
- you seem to be confusing the equality comparison operator == with the assignment operator =
- you're doing integer division, so the results you get will almost certainly not be the one you're expecting
>Repeater
Ohh! Thanks for pointing out the problem!
I'll try again, thanks!
[update]
Thanks for the tips, tried this one just now, it works elseif (input != 1 && 2 && 3 && 4)
---line---
>MikeyBoy
Sorry bout that, but the rest of the question was at the end of the code
Am a newbie, so I'm not so sure how the code tags function works
Sorry for the inconvenience, and yes, I've added it into code tags, thanks for the reminder
[update]
Thanks, I've just noticed that silly mistake too
Now it seems to be working fine
---line---
Thanks for the tips
Now I'll just try to make it go back to the main page every time after it executes
which probably takes another few more hours , lol