Advertisement

simple c++ math program not working

Started by January 03, 2003 01:58 PM
17 comments, last by headshot 21 years, 10 months ago
i am new to c++, and i made a progam to lerrn the math oporaters and stuff, basic calculatter and yet it wont work? here is the code(btw, how do you put it in a text box like ppl do here?) keep in mind that this code was for my eyes only, and not written for the world to see. /*this program takes two numbers and does ether *,/,,+, depending on what the guy says to do*/ #include <iostream.h> #include <stdio.h> int answer//answer int num1//1st number int num2//2nd number string op//what to do with them int main() { cout << "pick a number\n"; cin >> num1 \n; cout << "pick 1 more\n"; cin >> num2 \n; cout << "what would you like me to do with them(*,/,+,-)\n"; cin >> op; if (op = *) { num1 * num2 = answer; } if (op = -) { num1 - num2 = answer; } if (op = +) { num1 + num2 = answer; } if (op = /) { num1 / num2 = answer; } cout << answer; cout << endl; return 0; } any ideas?
i am god!
Can't use \n for cin statements. Only output statements.

Also - you need to operators inside single quotes in your if statements.


edit: oops - double quotes.
[edited by - Insanegull on January 3, 2003 3:05:40 PM]

[edited by - Insanegull on January 3, 2003 3:06:28 PM]

[edited by - Insanegull on January 3, 2003 3:07:11 PM]
Advertisement
A single = is for assignment. Comparison uses ==. Also, you are trying to compare a string and a operator. You will need to enclose in " ".

Halo7
whay didn´t you use the command of "switch" insist of writing
if (op=/)
i don´t know if is correct but try it
ok,, thanks, now i get 2 errors that, when i click on them, say it is this line?


int num1//1st number

?

c:\documents and settings\reilly\desktop\math.cpp(6) : error C2144: syntax error : missing '';'' before type ''int''


c:\documents and settings\reilly\desktop\math.cpp(6) : fatal error C1004: unexpected end of file found


those are the two errors i get?
i am god!
int answer//answer
int num1//1st number
int num2//2nd number
string op//what to do with them

Aren''t you missing something in between the variable decleration and the comment? The answer is in the compiler error.


Also, why don''t you move those variable declerations into main?
Advertisement
quote: Original post by OSN
whay didn´t you use the command of "switch" insist of writing
if (op=/)
i don´t know if is correct but try it




don''t know, if statments have always worked fine for me.
i am god!
well, i put ; in front of all the coments, but now i get this(is my code that full of errors?)


Deleting intermediate files and output files for project ''math - Win32 Debug''.
--------------------Configuration: math - Win32 Debug--------------------
Compiling...
math.cpp
c:\documents and settings\reilly\desktop\math.cpp(8) : error C2146: syntax error : missing '';'' before identifier ''op''
c:\documents and settings\reilly\desktop\math.cpp(8) : error C2501: ''string'' : missing storage-class or type specifiers
c:\documents and settings\reilly\desktop\math.cpp(8) : fatal error C1004: unexpected end of file found
Error executing cl.exe.

math.exe - 3 error(s), 0 warning(s)
i am god!
quote: Original post by Insanegull



Also, why don''t you move those variable declerations into main?


y, i see what your saying, but wont they work where they are now?
i am god!
quote: Original post by Insanegull



Also, why don''t you move those variable declerations into main?


y, i see what your saying, but wont they work where they are now?
i am god!

This topic is closed to new replies.

Advertisement