Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 22nd, 2005, 2:13 AM   #1
ian
Newbie
 
Join Date: Feb 2005
Location: Singapore
Posts: 13
Rep Power: 0 ian is on a distinguished road
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
}
ian is offline   Reply With Quote
Old Mar 22nd, 2005, 6:07 AM   #2
Benoit
Expert Programmer
 
Benoit's Avatar
 
Join Date: Sep 2004
Location: Ontario, Canada
Posts: 550
Rep Power: 4 Benoit is on a distinguished road
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
Benoit is offline   Reply With Quote
Old Mar 22nd, 2005, 6:22 AM   #3
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
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>.
__________________
&quot;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.&quot; - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Old Mar 23rd, 2005, 6:38 AM   #4
ian
Newbie
 
Join Date: Feb 2005
Location: Singapore
Posts: 13
Rep Power: 0 ian is on a distinguished road
Quote:
Originally Posted by Mjordan2nd
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>.
Done! Thanks for your help. By the way, the first if statement was supposed to check if the arugment was a number. The program would run into an error if there were no arguments, but I've added another if statement to test for arguments. All should go well now, thanks again for you help!
ian is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:00 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC