First error: Missed a semicolon before you declared Op.
Second error: Char is the correct type not string.
Third error: Not sure about this one. Try moving your variable declarations into the beginning of main.
Actually, if you fix the semicolon error I think that will clear up that third error.
I'm just a beginner, too but look at some more program examples. You are making quite a few simple mistakes.
I'm at work otherwise I would plug your code into my compiler and fix it for you.
[edited by - Insanegull on January 3, 2003 3:26:07 PM]
[edited by - Insanegull on January 3, 2003 3:28:44 PM]
simple c++ math program not working
January 03, 2003 02:30 PM
#include <iostream>#include <string>using namespace std;int main(){ int answer; int num1; int num2; string op; cout << "pick a number" << endl; cin >> num1; cout << "pick 1 more" << endl; cin >> num2; cout << "what would you like me to do with them(*,/,+,-)" << endl; cin >> op; if (op == "*") { answer = num1 * num2; } else if (op == "-") { answer = num1 - num2; } else if (op == "+") { answer = num1 + num2; } else if (op == "/") { answer = num1 / num2; } else { // handle error } cout << answer << endl; return 0;}
Notes:
- .h files aren''t used anymore in C++.
- stdio.h is for C IO functions, such as printf and scanf.
- setting the namespace guards against compilers that don''t use std as the default namespace.
- = is the assignment operator, == is the quality operator.
- "num1 * num2 = answer", etc. should have been "answer = num1 * num2". Look in a book or on Google about l-values and r-values.
quote: Original post by Anonymous Poster
Notes:
- .h files aren't used anymore in C++.
- stdio.h is for C IO functions, such as printf and scanf.
- setting the namespace guards against compilers that don't use std as the default namespace.
- = is the assignment operator, == is the quality operator.
- "num1 * num2 = answer", etc. should have been "answer = num1 * num2". Look in a book or on Google about l-values and r-values.
1-i though you could do string in c++?
2-when did they stop using .h?
here is my updated code
//this program takes two numbers#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 ; cout << "pick 1 more\n"; cin >> num2 ; cout << "what would you like me to do with them(*,/,+,-)\n"; cin >> op;if (op == "*"){ answer = num1 * num2 ;}if (op == "-"){ answer = num1 - num2 ;}if (op == "+"){ answer = num1 + num2 ;}if (op == "/"){ answer = num1 / num2 ;}cout << answer;cout << endl;return 0;}
here is the error i get now,
--------------------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 tryed char, that did not work ether?
[edited by - headshot on January 3, 2003 4:39:41 PM]
i am god!
January 03, 2003 04:03 PM
The following will fix those three errors:
1) Change op to type char.
2) In your if statements, use single quotes instead of double quotes. with chars, you use single quotes. with strings, you use double quotes.
Hope this helps
1) Change op to type char.
2) In your if statements, use single quotes instead of double quotes. with chars, you use single quotes. with strings, you use double quotes.
Hope this helps
thank you so much! it works now :D
but just to clareify, can you do strings or not?
but just to clareify, can you do strings or not?
i am god!
January 03, 2003 04:29 PM
To compare strings you should use strcmp(). What you are doing is comparing the address of the strings.
YES. But as the AP implied you must #include <string> if you want to use them. Strings are not primitive data types in C/C++. The are handled as char* in C and with the string class in C++ (obviously C++ is completely backwards-compatible on this issue).
______________________________________________________________________________________The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ"So. Any n00bs need some pointers? I have a std::vector<n00b*> right here..." - ZahlmanMySite | Forum FAQ | File Formats______________________________________________________________________________________
oh, thanks, that make more sense now, god i was scard i would need to use a char array just to spell out a word, lol.
i am god!
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement