HELP with Basic else if statment
HI,
Im sure this will be easy for you guys, however it's being annoying me for quite a while now. I have only studyed a couple of hours in C++ in a induction sesision.
ok here is my code:
#include <iostream>
using namespace std;
int main()
{
char op;
int num1;
int num2;
int ans;
cout <<"Enter your first number: ";
cin >> num1;
cout <<"Enter your second number: ";
cin >> num2;
cout << "" << endl;
if (op == '+');
cout << "Add" <<endl;
{
ans = num1 + num2;
cout << "Answer is " << ans << endl;
cout << "" << endl;
}
else if (op == '-');
cout << "Subtract" <<endl;
{
ans = num1 - num2;
cout << "Answer is " << ans << endl;
cout << "" << endl;
}
else if (op == '*');
cout << "Times" <<endl;
{
ans = num1 * num2;
cout << "Answer is " << ans << endl;
cout << "" << endl;
}
else (op == '/');
cout << "Divide" <<endl;
{
ans = num1 / num2;
cout << "Answer is " << ans << endl;
cout << "" << endl;
}
system("PAUSE");
}
Right the problems, i carnt see anthing wrong with it however when i try to run it in Dev-C++ is says there is somthing wrong with my else if statments, however if i replace them with if startments it runs just not the way i want it to.
hope you can help i am very new to C++ so any help will be appreciated.
Re: HELP with Basic else if statment
First please use code tags when posting code. The code you posted is unformatted and very hard to read.
Quote:
when i try to run it in Dev-C++
This is the Visual C++ forum. Next time, post your question in the non-Visual C++ forum. "Dev-C++" is an IDE that runs the gcc compiler underneath the hood -- none of these deals with Microsoft Visual C++, which is the topic of this forum.
Quote:
Im sure this will be easy for you guys, however it's being annoying me for quite a while now. I have only studyed a couple of hours in C++ in a induction sesision.
Explain what this code does, because it doesn't look right:
Code:
if (op == '+'); // <-- what's this semicolon doing here?
cout << "Add" <<endl;
// What's this supposed to be?
{
ans = num1 + num2;
cout << "Answer is " << ans << endl;
cout << "" << endl;
}
Please read up on how to properly construct an if/else in C++, because whatever that is, it's incorrect. You do it several times in your code.
Quote:
i carnt see anthing wrong with it
If you take a look at the C++ book you're using, you can't see the difference between what you posted and what you see in your book?
Regards,
Paul McKenzie
Re: HELP with Basic else if statment
Since your code isn't formatted (with code tags), I didn't look at it in detail, but for one thing, you need to remove the ; at the end of your if and your else if lines.
Code:
if (op == '+'); // lose the semicolon
...
else if (op == '-'); // lose the semicolon
Hope that helps.
Re: HELP with Basic else if statment
Hello this should help you it is a reconstructed version of your project hope it helps
how do you put in code tags :L:L
Code:
#include <iostream>
using namespace std;
int main()
{
char op;
int num1;
int num2;
int ans;
cout <<"Enter your first number: ";
cin >> num1;
cout <<"Enter your second number: ";
cin >> num2;
cout <<"Pick your operation: ";
cin >> op;
cout << "" << endl;
if (op == '+'){
cout << "Add" <<endl;
ans = num1 + num2;
cout << "Answer is " << ans << endl;
cout << "" << endl;
}
else if (op == '-'){
cout << "Subtract" <<endl;
ans = num1 - num2;
cout << "Answer is " << ans << endl;
cout << "" << endl;
}
else if (op == '*'){
cout << "Times" <<endl;
ans = num1 * num2;
cout << "Answer is " << ans << endl;
cout << "" << endl;
}
else if (op == '/')
{
cout << "Divide" <<endl;
ans = num1 / num2;
cout << "Answer is " << ans << endl;
cout << "" << endl;
}
system("pause");
return 0;
}
Re: HELP with Basic else if statment
oh yeah dont use this for new line cout <<""<<;
it should be \n at the end