Thread: strcmp(), reading students' record and search for a particular name

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    254

    strcmp(), reading students' record and search for a particular name

    Hi

    I'm getting this error for the blue line in the code below: error: cannot convert 'bool' to 'const char*' for argument '1' to 'int strcmp(const char*, const char*)'.

    How do I fix it? Please help me. Thanks.

    Code:
    // read students' record and search for data of a particular student.cpp
    // use structure and display all data about the searched student
    
    #include <iostream>
    #include <cstdlib>
    #include <cstring>
    
    using namespace std;
    
    const int C = 3;
    
    struct Student {int rollno; char firstname[20]; char lastname[20]; string sex; float gpa;};
    
    Student stud[C];
    
    int main()
    {
            int i; char searchname[30];
    
            for (i=0; i<C; i++)
            {
                    cout << "\n\nEnter student #" << (i+1) << "\'s details below\n\n";
                    cout << "enter roll no.: "; cin >> stud[i].rollno;
                    cout << "enter first name: "; cin.get(stud[i].firstname, 20);
                    cout << "enter last name: "; cin.get(stud[i].lastname, 20);
                    cout << "enter sex: "; cin >> stud[i].sex;
                    cout << "enter GPA: "; cin >> stud[i].gpa;
            }
    
            cout << "enter the name to be searched for: "; cin.get(searchname, 30);
            // name to be searched for could be, say, Jack Dawson
    
            for (i=0; i<C; i++)
            {
                    if (strcmp( (stud[i].firstname && stud[i].lastname), searchname ) == 0)
                    {
                            cout << "Data for the searched student is shown below\n";
                            cout << "roll no.: " << stud[i].rollno << endl;
                            cout << "first name: " << stud[i].firstname << endl;
                            cout << "last name: " << stud[i].lastname << endl;
                            cout << "sex: " << stud[i].sex << endl;
                            cout << "GPA: " << stud[i].gpa << endl;
                            break;
                     }
    
                     else
                     {
                             cout << "Sorry, no student exists with such name" << endl;
                             break;
                     }
            }
    
            system("pause");
            return 0;
    }
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > if (strcmp( (stud[i].firstname && stud[i].lastname), searchname ) == 0)
    Compare each one separately
    strcmp(a,b)==0 && strcmp(c,d)==0
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Quote Originally Posted by Salem View Post
    > if (strcmp( (stud[i].firstname && stud[i].lastname), searchname ) == 0)
    Compare each one separately
    strcmp(a,b)==0 && strcmp(c,d)==0
    Thank you, Salem.

    Can I do that? I have entered the student's first and last names separately but the name I want to search for is entered in one turn. Do you get what I'm trying to say? Please let me know if there is a way to fix it. Thanks.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Why not split the search term back up?

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    120
    I would join the 2 strings into one string before checking:

    Code:
    char Temp[ 300 ] = "" //Pretty big array
    for (i=0; i<C; i++)
            {
                    Temp[0] = '\0';
                    strcat( Temp, stud[i].firstname );
                    strcat( Temp, " " );
                    strcat( Temp, stud[i].lastname );
                    if (strcmp( Temp, searchname ) == 0)
                    {
                            cout << "Data for the searched student is shown below\n";
                            cout << "roll no.: " << stud[i].rollno << endl;
                            cout << "first name: " << stud[i].firstname << endl;
                            cout << "last name: " << stud[i].lastname << endl;
                            cout << "sex: " << stud[i].sex << endl;
                            cout << "GPA: " << stud[i].gpa << endl;
                            break;
                     }
    
                     else
                     {
                             cout << "Sorry, no student exists with such name" << endl;
                             break;
                     }
            }
    edit: sory about the mistake ^^'
    Last edited by shiroaisu; 06-13-2011 at 08:51 AM.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I would not. That code is a ticking time bomb.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    120
    Quote Originally Posted by Elysia View Post
    I would not. That code is a ticking time bomb.
    My mistake -.-, i fixed it now

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The note about a ticking time bomb is because you are using strcat without even bothering to check that you aren't overrunning your buffer. You see the additional complexity (and security issues!), which is why I would not use the approach.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    120
    Quote Originally Posted by Elysia View Post
    The note about a ticking time bomb is because you are using strcat without even bothering to check that you aren't overrunning your buffer. You see the additional complexity (and security issues!), which is why I would not use the approach.
    indeed, but i just gave the example as a solution for a simple program like this. Really the first time i saw your post i thought you were infact refering to buffer overrunning, but then i saw i was just adding and adding endlessly to one buffer and i thought u were refering to that instead.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    That's just how scary that kind of code is. Bugs sneak in so easily and they are a huge security risk.
    That's really why you shouldn't use such approaches--even for educational purposes--when there are much better alternatives.
    This whole C-style C++ is what gave the language its bad name. It's such a shame, because it's such a wonderful--and safe--language.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    120
    Yeah you are right, after all, the best way of practicing is doing it right from the start.

    But using an std::string would get rid of that problem no??

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, it would get rid of the problem.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading a record from a File
    By David101 in forum C Programming
    Replies: 2
    Last Post: 12-14-2004, 06:42 PM
  2. linear search for structure (record) array
    By jereland in forum C Programming
    Replies: 3
    Last Post: 04-21-2004, 07:31 AM
  3. reading a customer record file
    By abbycat131 in forum C++ Programming
    Replies: 1
    Last Post: 01-29-2003, 09:15 PM
  4. Reading Master Boot Record
    By Lord Siddle in forum C Programming
    Replies: 2
    Last Post: 09-15-2002, 08:23 PM
  5. Any Computeach International Students/ex students??
    By stevey in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 03-26-2002, 04:12 PM