string compare problem

This is a discussion on string compare problem within the C++ Programming forums, part of the General Programming Boards category; Code: int main() { string s1 = "", song = "blablabla.mp3", outputsong( song, 0, song.length()-4 ); int j = 0, ...

  1. #1
    Registered User gandalf_bar's Avatar
    Join Date
    Oct 2003
    Posts
    92

    Unhappy string compare problem

    Code:
    int main() {
    	string s1 = "", song = "blablabla.mp3", outputsong( song, 0, song.length()-4 );
    	int j = 0, i = 0;
    	while(song[j++]!='.');
    	while(song[j]) {
    		s1[i++] = song[j++];
    	}
    
    	if(s1=="mp3") {                             //Please, check this line!!!!
    		outputsong += ".ogg";
    	}
    	else if(s1=="ogg") {
    		outputsong += ".mp3";
    	}
    	else {
    	}
    
            cout << song << endl << outputsong << endl;
    
    	return 0;
    }
    What make me confuse is the output of the program is:
    blablabla.mp3
    blablabla
    What I expected from this program is:
    blablabla.mp3
    blablabla.ogg

    When I check this program with gdb:
    After this statement executed:
    Code:
    while(song[j]) {
    		s1[i++] = song[j++];
    }
    (gdb) p s1
    $18 = {static npos = 4294967295,
    _M_dataplus = {<allocator<char>> = {<No data fields>},
    _M_p = 0x804a4bc "mp3"}, static _S_empty_rep_storage = {0, 0, 1, 0}}

    But why if(s1=="mp3") statement does not give true condition????
    A man asked, "Who are you?"
    Buddha answered, "I am awaked."

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    place a the line:


    cout << s1 << endl;

    between the end of the second loop, and the line:

    if(s1 == "mp3")

    to see what s1 is. I suspect s1 may be .mp3 and not just mp3 so the if statement is never identified. I also see no way for s1 to ever be ogg based on this code since the value of s1 is dependent on song and the value of song never changes in the code.

  3. #3
    Registered User gandalf_bar's Avatar
    Join Date
    Oct 2003
    Posts
    92
    Surprising!!!!!
    The output is:

    blablabla.mp3
    blablabla

    So it means s1 = nothing. How come?
    Here's my code again:

    Code:
    int main() {
    	string song = "blablabla.mp3", outputsong( song, 0, song.length()-4 );
    	string s1;
    	int j = 0, i = 0, type;
    	while(song[j++]!='.');
    while(song[j]) {
    s1[i++] = song[j++];
    }
    
    	cout << s1 << endl;
    
    	if(s1=="mp3") {
    		outputsong += ".ogg";
    	}
    	else if(s1=="ogg") {
    		outputsong += ".mp3";
    	}
    	else {
    	}
    
            cout << song << endl << outputsong << endl;
    
    	return 0;
    }
    I found solution already with this code: string s1( song, song.length()-3, 3 );

    But I am just curios. How come?
    A man asked, "Who are you?"
    Buddha answered, "I am awaked."

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by gandalf_bar
    Surprising!!!!!
    The output is:

    blablabla.mp3
    blablabla

    So it means s1 = nothing. How come?
    Here's my code again:


    I found solution already with this code: string s1( song, song.length()-3, 3 );

    But I am just curios. How come?
    This won't work with c++ strings:

    Code:
    while(song[j]) {
      s1[i++] = song[j++];
    }
    Use

    Code:
    s1 += song[j++];
    Dave

  5. #5
    Registered User gandalf_bar's Avatar
    Join Date
    Oct 2003
    Posts
    92
    Thank you!!!! It worked.
    A man asked, "Who are you?"
    Buddha answered, "I am awaked."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 03:13 PM
  2. string problem
    By INeedSleep in forum C++ Programming
    Replies: 24
    Last Post: 11-08-2007, 10:52 PM
  3. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  4. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  5. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM

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