testing 2 strings for equality

This is a discussion on testing 2 strings for equality within the C++ Programming forums, part of the General Programming Boards category; here is my code. in void function lookupInfo, how to i test the 2 strings for equality? everything works except ...

  1. #1
    Unregistered
    Guest

    testing 2 strings for equality

    here is my code. in void function lookupInfo, how to i test the 2 strings for equality? everything works except the if statement.

    #include <iostream.h>
    #include <fstream.h>
    #include <ctype.h>
    #include <string.h>

    struct inputInfo
    {
    char name[25];
    short ext;
    };

    void enterInfo();
    void lookupInfo();

    void main()
    { char choice = ' ';
    cout<< "Enter E to enter info or L to lookup info or Q to quit: ";
    cin>> choice;
    cin.ignore (100,'\n');
    choice = toupper(choice);
    while( choice != 'Q')
    {
    if (choice == 'E')
    enterInfo();
    else
    if(choice == 'L')
    lookupInfo();

    cout<< "Enter E to enter info or L to lookup info or Q to quit: ";
    cin>> choice;
    cin.ignore (100,'\n');
    choice = toupper(choice);
    }
    }
    void enterInfo()
    {
    inputInfo record = {"",0};

    ofstream outFile;
    outFile.open ("T7Be08.dat",ios::app);
    if (!outFile.fail () )
    {
    cout<< "Enter name: ";
    cin.get (record.name,25,'\n');
    cin.ignore (100,'\n');

    cout<< "Enter extention number: ";
    cin>> record.ext;
    cin.ignore (100,'\n');

    outFile<< record.name << '#'<< record.ext << endl;

    outFile.close ();
    }
    else
    cout<< "File not opened"<< endl;
    }
    void lookupInfo()
    {
    char lookupName[25]= "";
    cout<< "Enter lookup name: ";
    cin.get (lookupName,25,'\n');
    cin.ignore (100,'\n');



    inputInfo record = {"",0};

    ifstream inFile;
    inFile.open ("T7Be08.dat",ios::in);
    if (!inFile.fail () )
    {
    inFile.get (record.name ,25,'#');
    inFile.ignore (1);
    inFile>> record.ext ;
    inFile.ignore (1);
    while (!inFile.eof () )
    {
    // if (stricmp(record.name,lookupName) == 0 ) how
    // do i test these 2 strings for equality?
    // {
    cout<< record.name << '\n';
    cout<< record.ext << '\n';
    inFile.get (record.name ,25,'#');
    inFile.ignore (1);
    inFile>> record.ext ;
    inFile.ignore (1) ;
    // }
    }
    }
    }

  2. #2
    Unregistered
    Guest
    stricmp(record.name,lookupName) == 0 will be a true statement if the cstyle string called record.name is the same as the cstyle string lookupName. stricmp is the case insensitive version of strcmp which is case sensitive. therefore if you wish to do something if the two strings are the same just use the above statement as the condition of an if statement as you have done and it should work.

  3. #3
    Registered User Engineer's Avatar
    Join Date
    Oct 2001
    Posts
    125
    You can use either strcmp(), strncmp() and memcmp() to compare the two string to equality.

    strcmp() (as you know) takes pointers to string1 and string2 as arguments.

    strncmp() and memcmp(): first two arguments are the same as strcmp(), and the third one is the length that you want to compare ( this will allow to compare the first 3 bytes, for example).

    strcmp() and strncmp() work only with strings, but memcmp() will compare the memory areas no matter what variable type is used.

    All three functions return:

    -1 if string1 is smaller than string2 (I say smaller because chars are really integer values, and this is how the comparison is done).

    0 if string1 is equal to string2

    1 if string1 is bigger than string2

    Hope this helps.
    1 rule of the Samurai Code: if you have nothing to say, don't say anything at all!

  4. #4
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    .
    Last edited by Troll_King; 12-01-2001 at 04:51 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 02:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 07:07 PM
  3. Hi everyone..Testing strings and if question
    By tinkerbell20 in forum C++ Programming
    Replies: 7
    Last Post: 06-21-2005, 06:23 AM
  4. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  5. testing for EOF with strings
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 04-26-2002, 11:57 AM

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