[plz help]confusion about comparison between two strings to me

This is a discussion on [plz help]confusion about comparison between two strings to me within the C++ Programming forums, part of the General Programming Boards category; Dear guys~~ i met a problem which is spoiling me long time... i havent figure it out~ i am coding ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    23

    [plz help]confusion about comparison between two strings to me

    Dear guys~~
    i met a problem which is spoiling me long time... i havent figure it out~
    i am coding in linux...the following code shows the comparison of two strings... the size and the content are same but why it doesnt report "same"... plz help me...

    Code:
    bool FindRecord()
    {
        char Seeker[MaxCallNumber];
        int SeekrPos = 0;
        cout << "Enter Call Namber: ";
        cin >> Seeker;
        
        
        
            fstream iofile("ass1.dat", ios::in | ios::out | ios::binary);
            if (!iofile)
            {
                cerr << "open error!" << endl;
                abort();    
            }
            
            while ((strcmp((Database[SeekrPos].CallNumber),Seeker) != 0) && (SeekrPos < NumBooks))
            {
                	cout << "Database[SeekrPos]:" << Database[SeekrPos].CallNumber << endl;
    		cout << sizeof(Database[SeekrPos].CallNumber) << endl;
    		cout << "Seeker: " << Seeker << endl;
    		cout << sizeof(Seeker) << endl;
    		SeekrPos++;
            }
            
            if (strcmp((Database[SeekrPos].CallNumber),Seeker) == 0)
            {
            
            cout << "found!!!"<< endl;
    	iofile.seekg(SeekrPos*sizeof(Database[SeekrPos]),ios::beg);
            iofile.read((char*)&Database[SeekrPos],sizeof(Database[SeekrPos]));
            cout << "CallNumber: " << Database[SeekrPos].CallNumber << endl;
            cout << "Title: " << Database[SeekrPos].Title << endl;
            cout << "Author: " << Database[SeekrPos].Author << endl; 
            cout << "BookStatus: " << Database[SeekrPos].BookStatus << endl; 
            cout << "DueDate: " << Database[SeekrPos].DueDate << endl; 
            cout << "BorrowersName: " << Database[SeekrPos].BorrowersName << endl;
            }
    	else
    	{
    		cout << "couldn't find!!!";
    	}
    }
    the konsole says that:

    Enter command (or m for menu): f
    Enter Call Namber: 001.645.2
    Database[SeekrPos]:001.645.2
    15
    Seeker: 001.645.2
    15
    Database[SeekrPos]:001.721.3
    15
    Seeker: 001.645.2
    15
    Database[SeekrPos]:001.384.2
    15
    Seeker: 001.645.2
    15
    Database[SeekrPos]:001.098.5
    15
    Seeker: 001.645.2
    15
    Database[SeekrPos]:001.491.8
    15
    Seeker: 001.645.2
    15
    Database[SeekrPos]:001.967.2
    15
    Seeker: 001.645.2
    15
    Database[SeekrPos]:001.077.4
    15
    Seeker: 001.645.2
    15
    Database[SeekrPos]:001.947.2
    15
    Seeker: 001.645.2
    15
    Database[SeekrPos]:001.997.1
    15
    Seeker: 001.645.2
    15
    Database[SeekrPos]:001.095.3
    15
    Seeker: 001.645.2
    15
    Database[SeekrPos]:001.823.8
    15
    Seeker: 001.645.2
    15
    Database[SeekrPos]:001.812.9
    15
    Seeker: 001.645.2
    15
    Database[SeekrPos]:005.920.4
    15
    Seeker: 001.645.2
    15
    couldn't find!!!
    ========================

    thx a lot!!!!

  2. #2
    Registered User
    Join Date
    Aug 2007
    Posts
    23
    if i do it in DEV in windows,,, it works alright...

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Can you output the Database[SeekrPos] and Seeker values with something around it to make sure there is no trailing whitespace or anything:
    Code:
    cout << "Seeker-->" << Seeker << "<--" << endl;
    You can also output each letter (and maybe its ASCII value) for each string you pass to strcmp.

  4. #4
    Registered User
    Join Date
    Aug 2007
    Posts
    23
    do i need to use this to reset all element in the array?
    Code:
    memset(ch,0x00,sizeof(ch));
    btw why the size shows they are same?

  5. #5
    Registered User
    Join Date
    Aug 2007
    Posts
    23
    i wonder why when the size and elements are same , but it reports "not same"

  6. #6
    Registered User
    Join Date
    Aug 2007
    Posts
    23
    it still has the same problem...
    Last edited by kyaky; 08-14-2007 at 02:05 PM.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    sizeof returns the size of the array, but strcmp uses the length of the strings. These can be different. The string length is found by looking for the first null character (which is the character with the value 0x00). So even if the two arrays have a size of 15, it is possible that one has a string with 9 characters before the terminating null, and the other has a string of 10 characters (or some other number).

    My guess is that by initializing the arrays to null characters, you no longer have garbage characters filling out the array after the actual string is loaded.

  8. #8
    Registered User
    Join Date
    Aug 2007
    Posts
    23
    sry, i saw wrong... it still has the same problem... ................................ -. - OZN

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    What was the output when you added the --> <--?

  10. #10
    Registered User
    Join Date
    Aug 2007
    Posts
    23
    output:
    Code:
    Enter command (or m for menu): f
    Enter Call Namber: 001.645.2
    <--Database[SeekrPos]001.645.2
    15
    Seeker-->001.645.2<--Seeker
    15
    <--Database[SeekrPos]001.721.3
    15
    Seeker-->001.645.2<--Seeker
    15
    <--Database[SeekrPos]001.384.2
    15
    Seeker-->001.645.2<--Seeker
    15
    <--Database[SeekrPos]001.098.5
    15
    Seeker-->001.645.2<--Seeker
    15
    <--Database[SeekrPos]001.491.8
    15
    Seeker-->001.645.2<--Seeker
    15
    <--Database[SeekrPos]001.967.2
    15
    Seeker-->001.645.2<--Seeker
    15
    <--Database[SeekrPos]001.077.4
    15
    Seeker-->001.645.2<--Seeker
    15
    <--Database[SeekrPos]001.947.2
    15
    Seeker-->001.645.2<--Seeker
    15
    <--Database[SeekrPos]001.997.1
    15
    Seeker-->001.645.2<--Seeker
    15
    <--Database[SeekrPos]001.095.3
    15
    Seeker-->001.645.2<--Seeker
    15
    <--Database[SeekrPos]001.823.8
    15
    Seeker-->001.645.2<--Seeker
    15
    <--Database[SeekrPos]001.812.9
    15
    Seeker-->001.645.2<--Seeker
    15
    <--Database[SeekrPos]005.920.4
    15
    Seeker-->001.645.2<--Seeker
    15
    couldn't find!!!

  11. #11
    Registered User
    Join Date
    Aug 2007
    Posts
    23
    it has no space between <-- --> and "text"

  12. #12
    Registered User
    Join Date
    Aug 2007
    Posts
    23
    wait... thats strange...~~~ why it is like that "<--Database[SeekrPos]001.823.8"

    OMG

  13. #13
    Registered User
    Join Date
    Aug 2007
    Posts
    23
    the position is totally strange!!!

  14. #14
    Registered User
    Join Date
    Aug 2007
    Posts
    23
    if i change it to
    Code:
    cout << "11111Database[SeekrPos]-->" << Database[SeekrPos].CallNumber << "<--Database[SeekrPos]"<< endl;
    the output is :
    Code:
    Enter command (or m for menu): f
    Enter Call Namber: 001.645.2
    <--Database[SeekrPos]s]-->001.645.2
    15
    Seeker-->001.645.2<--Seeker
    15
    something wrong with output buffer or something else?

  15. #15
    Registered User
    Join Date
    Aug 2007
    Posts
    23
    where the hell is "s]" from.......... i am nearly crazy... 5:30am...

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 11:43 AM
  2. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  3. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  4. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 07:37 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 12:23 PM

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