trouble with char* equality

This is a discussion on trouble with char* equality within the C++ Programming forums, part of the General Programming Boards category; Code: int main(int argv, char * argc[]){ // size array if (argv!=5){ cout<<"baduser"; exit 0; } if((argc[1]=="-I")and(argc[3]=="-o"){ char * inputfile ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    6

    trouble with char* equality

    Code:
    int main(int argv, char * argc[]){
    //	size		array
    
    	if (argv!=5){
    		cout<<"baduser";
    		exit 0;
    	}
    	if((argc[1]=="-I")and(argc[3]=="-o"){
    		char * inputfile = argc[2];
    		char * outputfile = argc[4];
    	}else
    		//bad user
    etc. etc.
    this is code from the notes i've taken from the TA.

    command line input is supposed to look something like this:

    program -I input.txt -o output.txt

    the if statement always passes to the else, how do i get it to equate the char* to what in my mind is a string? we havn't "learned" about strings so can't use that type. how do i get it to correctly check out that -I does infact equal -I?

    also is all the spacing with the *'s correct? the TA draws on the chalkboard and i'm not 100% sure on the spacing.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Compare C-strings with strcmp.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    6
    can't do it that way, we haven't been taught about the cstrings library :-(

    can't use what we don't "know".

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Well I guess then you'll have to write your own.

    Or check argv[n][0] for a '-' and then check argv[n][1].
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    6
    i also tried something like this:

    Code:
    int main(int argv, char * argc[]){
    //	size		array
    
    	char * char1 = "-I";
    	char * char2 = "-o";
    
    	if (argv!=5){
    		cout<<"baduser";
    		exit 0;
    	}
    	if((argc[1]==char1)and(argc[3]==char2){
    		char * inputfile = argc[2];
    		char * outputfile = argc[4];
    	}else
    		//bad user
    etc. etc.
    it threw a warning about converting a string literal to a char*, but when i ran it, it didn't change anything. why wouldn't this work? and how would i set something as a char* without having it be seen as a string literal?

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by pik_d
    i also tried something like this:

    [...]

    it threw a warning about converting a string literal to a char*, but when i ran it, it didn't change anything. why wouldn't this work? and how would i set something as a char* without having it be seen as a string literal?
    It threw a warning because you were doing wrong. Try to read my last post.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> why wouldn't this work?
    Because a string literal and a char* are both just character pointers, and when you compare pointers with == it compares the pointers themselves, not the strings they point to.

    >> how would i set something as a char* without having it be seen as a string literal.
    You would copy a string literal into the char*. But you would normally use strcpy to do that and you can't use cstring functions.

    You can do this check without using strings or string literals at all, just compare characters as Dave_Sinkula showed.

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    2,017
    You could do this. It's a bit of a hack, but if you can't use cstrings, then this is the next easiest thing.

    Code:
    int main(int argv, char * argc[]){
    //	size		array
    
    	if (argv!=5){
    		cout<<"baduser";
    		exit(0);//exit is a function.
    	}
    	if( String(argc[1]) == "-I" && String(argc[3]) == "-o" ){
    		char * inputfile = argc[2];
    		char * outputfile = argc[4];
    	}else
    		//bad user
    etc. etc.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> String
    Did you mean string? That would be best in a C++ program of course, but the OP said, "we havn't "learned" about strings so can't use that type".

    I just noticed another issue, your inputfile and outputfile pointers are local to the if block and will not be usable outside of it. Declare them before the if.

  10. #10
    Registered User
    Join Date
    Nov 2006
    Posts
    6
    actually, i ended up writing this:

    Code:
    int comp(char * char1, char * char2){
    	int i = 0;
    
    	while ((char1[i]!=(int)0)&&(char2[i]!=(int)0)){
    		if(char1[i]!=char2[i])
    			return 0;
    
    		i++;}
    
    	return 1;
    }
    seems to work.

    Quote Originally Posted by Daved
    >> String
    Did you mean string? That would be best in a C++ program of course, but the OP said, "we havn't "learned" about strings so can't use that type".

    I just noticed another issue, your inputfile and outputfile pointers are local to the if block and will not be usable outside of it. Declare them before the if.
    yeah, a friend of mine pointed that out. usually this TA knows what he's doing... but this code is messed up in so many ways...

  11. #11
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,452
    Code:
    int main(int argv, char * argc[])
    better use
    Code:
    int main(int argc, char * argv[])
    It is not mandatory, but general conventions. Think about others who will support your code
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  12. #12
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,452
    seems to work.
    But note that the original "strcmp" returns something like str2[i]-str1[i]. (0 if strings are equal)This gives a possibility to use the same function when sorting strings, not only just to check if they are equal.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  13. #13
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Quote Originally Posted by pik_d
    why wouldn't this work?
    Because pointer comparison is comparing memory addresses.

    char * str, str2;
    if (str == str2) cout << "Yay";

    The second line says "If str points to the same memory location as str2 then print". It isn't comparing what is at those addresses, it's comparing if the addresses themselves are the same.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble with assignment in C
    By mohanlon in forum C Programming
    Replies: 17
    Last Post: 06-23-2009, 10:44 AM
  2. Replies: 6
    Last Post: 01-03-2007, 02:02 PM
  3. Is it so trouble?
    By Yumin in forum Tech Board
    Replies: 4
    Last Post: 01-30-2006, 03:10 PM
  4. trouble scanning in... and link listing
    By panfilero in forum C Programming
    Replies: 14
    Last Post: 11-21-2005, 11:58 AM
  5. C++ program trouble
    By senrab in forum C++ Programming
    Replies: 7
    Last Post: 04-29-2003, 11:55 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21