| View previous topic :: View next topic |
| Author |
Message |
GaNoN69 Master Cheater
Reputation: 0
Joined: 23 Jun 2007 Posts: 362 Location: Guh?
|
Posted: Sat Jun 21, 2008 3:39 pm Post subject: [Help] Starting C++ (Revise this code please) |
|
|
Hello, i started today to learn c++ and i tried by myself to make a "calculator", the problem appears on the "IF" that checks if the symbol entered is correct or not. Im getting allways that is incorrect and i dont know why, i have tried many things. Im sure is something simple, if someone can help me i will be very pleased. Ty.
Note* It compiles correctly please someone compile it or check it and help me ty.*
And yeah im Spanish, Spain is in europe
#include <iostream>
#include <string>
using namespace std;
int main()
{
int a,b,r;
char symbol;
string symbolReturn,palabra1,palabra2;
entravalor1:
cout << "Entra un valor" <<"\n "; //Enter a Value
cin >> a;
entrasymbol:
cout << "Suma(+), Resta(-), Multiplicacion(*) o Division(/)?" <<"\n "; //Type of Operation
getchar();
cin.get();
if(symbol!='+'&'-'&'*'&'/');
{
cout << "Symbolo Erroneo!" << endl;
cin.get();
goto entrasymbol;
}
entravalor2:
cout << "Entra el segundo valor que operara con el primero" <<"\n "; //Enter the Second Value
cin >> b;
cin.get();
//Condicion 1
condicion:
if(symbol='+')
{
symbolReturn = "sumado";
palabra1="suma";
palabra2="mas";
r=a+b;
}
else if(symbol='-')
{
symbolReturn = "restado";
palabra1="resta";
palabra2="menos";
r=a-b;
}
else if(symbol='*')
{
symbolReturn= "Multiplicado";
palabra1="multiplicacion";
palabra2="por";
r=a*b;
}
else if(symbol='/')
{
symbolReturn = "Dividido";
palabra1="division";
palabra2="dividido entre";
r=a/b;
}
//Parte Final
cout << "Usted ha " <<symbolReturn <<" los numero " <<a <<" " <<palabra2 <<" " <<b;
cout <<"El resultado de dicha " <<palabra1 <<"es el numero " <<r;
cin.get();
return 0;
}
_________________
|
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Sat Jun 21, 2008 3:51 pm Post subject: |
|
|
Please use the code tags when making your post that contains code.
_________________
- Retired. |
|
| Back to top |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Sat Jun 21, 2008 4:17 pm Post subject: |
|
|
Your problem is that you're using the bit operator AND (&) instead of the logical operator AND (&&). Just change it to:
| Code: |
if(symbol != "+" && "-" && "*" && "/") {
//code here
}
|
_________________
| 8D wrote: |
cigs dont make people high, which weed does, which causes them to do bad stuff. like killing |
|
|
| Back to top |
|
 |
Symbol I'm a spammer
Reputation: 0
Joined: 18 Apr 2007 Posts: 5094 Location: Israel.
|
Posted: Sat Jun 21, 2008 4:24 pm Post subject: |
|
|
He also used 1 equal sign (=) instead of 2, = is for assignment.
And oib, this if statement is also wrong, he should've wrote:
| Code: | | if (symbol != '+' && symbol != '-' && symbol != '/' && symbol != '*') |
|
|
| Back to top |
|
 |
Noz3001 I'm a spammer
Reputation: 26
Joined: 29 May 2006 Posts: 6220 Location: /dev/null
|
Posted: Sat Jun 21, 2008 5:16 pm Post subject: |
|
|
instead of using all them if's you could use a switch statement.
| Code: |
switch(symbol)
{
case '*':
// Do multiply
break;
case '+':
// Do addition
break;
case '-':
// Do subtraction
break;
default:
cout << "Spanish for invalid symbol" << endl;
break;
} |
Oh, and goto's are supposed to be bad practice but i suppose it' OK to use them in such a small program.
|
|
| Back to top |
|
 |
GaNoN69 Master Cheater
Reputation: 0
Joined: 23 Jun 2007 Posts: 362 Location: Guh?
|
Posted: Sat Jun 21, 2008 5:51 pm Post subject: |
|
|
Ty all , i was considering case but my knowledge is rly limited atm
_________________
|
|
| Back to top |
|
 |
|