![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Feb 2005
Location: Singapore
Posts: 13
Rep Power: 0
![]() |
Help: Comparing a pointer and a string.
Okay, I have completely no idea what when wrong, and I also have no idea am I coding in the right way. :o So I'd need someone to help me in this:
This program (currently) takes its program arguments and compares the 1st argument if it's a number, or an operator (+, -, *, /, % or ^). If it's a number, currType will have the value of 1; if it's one of the operator, it'll have the value of 2; all other cases currType will have a value of 3. The problem comes when I attempt to compare the argument with a string. It's kind of difficult to explain, but the code should explain what I want to do. (Line 9 is the problem.) #include <iostream>
using namespace std;
int main(int argc, char *argv[]) {
int currType = 0; //0 = null; 1 = num; 2 = operator; 3 = error
if(atol(argv[1]) != 0) {
currType = 1;
}
else if(argv[1] == "+") {
currType = 2;
}
else {
currType = 3;
}
cout << currType << "::" << argv[1]; //Debugging statement
} |
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: Sep 2004
Location: Ontario, Canada
Posts: 550
Rep Power: 4
![]() |
Correct me if I'm wrong, but you can't compare strings using == thats only for single characters, no?
__________________
Johnny was a chemist's son but Johnny is no more, for what Johnny thought was H2O was H2SO4 |
|
|
|
|
|
#3 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
Well, on your first if-statement, I think what you mean to do is
if(argc!=0) . On your else if statement, you can't compare strings using the equals sign, like Benoit said. You need to use the strcmp() function in <string.h>.
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower |
|
|
|
|
|
#4 | |
|
Newbie
Join Date: Feb 2005
Location: Singapore
Posts: 13
Rep Power: 0
![]() |
Quote:
![]() |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|