Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


[Help] Starting C++ (Revise this code please)

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
GaNoN69
Master Cheater
Reputation: 0

Joined: 23 Jun 2007
Posts: 362
Location: Guh?

PostPosted: Sat Jun 21, 2008 3:39 pm    Post subject: [Help] Starting C++ (Revise this code please) Reply with quote

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 Wink

#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
View user's profile Send private message
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8587
Location: 127.0.0.1

PostPosted: Sat Jun 21, 2008 3:51 pm    Post subject: Reply with quote

Please use the code tags when making your post that contains code.
Code:
[code][/code]

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
oib111
I post too much
Reputation: 0

Joined: 02 Apr 2007
Posts: 2947
Location: you wanna know why?

PostPosted: Sat Jun 21, 2008 4:17 pm    Post subject: Reply with quote

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
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
Symbol
I'm a spammer
Reputation: 0

Joined: 18 Apr 2007
Posts: 5094
Location: Israel.

PostPosted: Sat Jun 21, 2008 4:24 pm    Post subject: Reply with quote

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
View user's profile Send private message
Noz3001
I'm a spammer
Reputation: 26

Joined: 29 May 2006
Posts: 6220
Location: /dev/null

PostPosted: Sat Jun 21, 2008 5:16 pm    Post subject: Reply with quote

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
View user's profile Send private message MSN Messenger
GaNoN69
Master Cheater
Reputation: 0

Joined: 23 Jun 2007
Posts: 362
Location: Guh?

PostPosted: Sat Jun 21, 2008 5:51 pm    Post subject: Reply with quote

Ty all , i was considering case but my knowledge is rly limited atm Razz
_________________
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites