Thread: searching for a student using functions, two errors

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

    searching for a student using functions, two errors

    Hi

    For the code below I'm getting these errors:
    In function 'int main()':|
    29|error: no match for 'operator=' in 'stud[3] = read()'|
    14|note: candidates are: Student& Student:: operator=(const Student&)|

    What's the reason for these errors? Please help me. Thank you.


    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;
    int i;
    
    ////////////////////////////////////////////////////////////////////////
    struct Student {int rollno; char firstname[20]; char lastname[20]; string sex; float gpa;};
    ////////////////////////////////////////////////////////////////////////
    
    Student stud[C];
    
    void read();
    void prnt(Student dummy[]);
    void find(char dummy[]);
    
    int main()
    {
            char choice;
            char searchname[30];
    
    
            stud[C] = read();
    
            cout << "do you want to print the data: "; cin >> choice;
    
            if (choice == 'y')
            {
                    prnt(stud);
            }
    
            cout << "enter the name of the student to be searched for: ";
            cin.get(searchname, 30);
    
            find(searchname);
    
            system("pause");
            return 0;
    
    }
    
    //----------------------------------------------------------------
    // read() definition
    void read()
    {
    
            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;
            }
    
    
    }
    
    //----------------------------------------------------------------
    //prnt() definition
    void prnt(Student dummy[])
    {
            for (i=0; i<C; i++)
            {
                    cout << "\n\nstudent #" << (i+1) << "\'s details below\n\n";
                    cout << "\nroll no.: " << dummy[i].rollno;
                    cout << "\nfirst name: " << dummy[i].firstname;
                    cout << "\nlast name: " << dummy[i].lastname;
                    cout << "\nsex: " << dummy[i].sex;
                    cout << "\nGPA: " << dummy[i].gpa;
            }
    
    }
    
    //------------------------------------------------------------------
    //find() definition
    void find(char dummy[])
    {
            for (i=0; i<C; i++)
            {
                    char temp_str[30] = {0};
                    strcat(temp_str, stud[i].firstname);
                    strcat(temp_str, " ");
                    strcat(temp_str, stud[i].lastname);
    
    
                    if ( strcmp(temp_str, dummy ) == 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 if ( i == (C-1) )
                     {
                             cout << "Sorry, no student exists with such name" << endl;
                             break;
                     }
            }
    
    
    }
    
    //----------------------------------------------------------------------
    Last edited by jackson6612; 06-16-2011 at 07:04 PM.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    Dude, will you please get a book? You can't just throw stuff at your compiler and expect it to work. Or at least remember all the help you've already received? You've been told before how return values work. You've been told before how the size of an array an indexing an array works.

    Soma

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    I have book but it doesn't mean one can learn everything from a book.

    As soon as I have entered the details for student #2 the program stops working. Please have a look on the attached screenshot. Thanks.
    New code is:
    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 = 2;
    int i;
    
    ////////////////////////////////////////////////////////////////////////
    struct Student {int rollno; char firstname[20]; char lastname[20]; string sex; float gpa;};
    ////////////////////////////////////////////////////////////////////////
    
    Student stud[C];
    
    Student read();
    void prnt(Student dummy[]);
    void find(char dummy[]);
    
    int main()
    {
            char choice;
            char searchname[30];
    
    
            stud[C] = read();
    
            cout << "do you want to print the data: "; cin >> choice;
    
            if (choice == 'y')
            {
                    prnt(stud);
            }
    
            cout << "enter the name of the student to be searched for: ";
            cin.get(searchname, 30);
    
            find(searchname);
    
            system("pause");
            return 0;
    
    }
    
    //----------------------------------------------------------------
    // read() definition
    Student read()
    {
    
            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;
                    cin.ignore();
                    cout << "enter first name: "; cin.getline(stud[i].firstname, 20);
                    cout << "enter last name: "; cin.getline(stud[i].lastname, 20);
                    cout << "enter sex: "; cin >> stud[i].sex;
                    cout << "enter GPA: "; cin >> stud[i].gpa;
            }
    
            return stud[C];
    
    
    }
    
    //----------------------------------------------------------------
    //prnt() definition
    void prnt(Student dummy[])
    {
            for (i=0; i<C; i++)
            {
                    cout << "\n\nstudent #" << (i+1) << "\'s details below\n\n";
                    cout << "\nroll no.: " << dummy[i].rollno;
                    cout << "\nfirst name: " << dummy[i].firstname;
                    cout << "\nlast name: " << dummy[i].lastname;
                    cout << "\nsex: " << dummy[i].sex;
                    cout << "\nGPA: " << dummy[i].gpa;
            }
    
    }
    
    //------------------------------------------------------------------
    //find() definition
    void find(char dummy[])
    {
            for (i=0; i<C; i++)
            {
                    char temp_str[30] = {0};
                    strcat(temp_str, stud[i].firstname);
                    strcat(temp_str, " ");
                    strcat(temp_str, stud[i].lastname);
    
    
                    if ( strcmp(temp_str, dummy ) == 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 if ( i == (C-1) )
                     {
                             cout << "Sorry, no student exists with such name" << endl;
                             break;
                     }
            }
    
    
    }
    
    //----------------------------------------------------------------------
    Attached Images Attached Images searching for a student using functions, two errors-whatswrong-jpg 
    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
    Code:
    stud[C] = read();
    This element of your array DOES NOT EXIST. You have declared an array of length 2. That means you get to use stud[0] and stud[1]. That is all you get. You do not get a [2], or a [3], or an [18].

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Sorry, tabstop. I don't get it. I'm returning an array of struct type. Both arrays have same size. Then, what's the issue? I know I'm missing many things here.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by jackson6612
    I have book but it doesn't mean one can learn everything from a book.
    What is the book that you are using?

    Quote Originally Posted by jackson6612
    I'm returning an array of struct type. Both arrays have same size. Then, what's the issue?
    How many elements are there in the array named stud?

    Oh, and why is stud a global variable?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by jackson6612 View Post
    Sorry, tabstop. I don't get it. I'm returning an array of struct type. Both arrays have same size. Then, what's the issue? I know I'm missing many things here.
    You've made some ludicrous statements in the past, but I think this is the most ludicrous yet. Of course you are NOT returning an array. Of course there are NOT two arrays in the first place. Read your book, or some tutorials, on arrays. You can only deal with ONE SLOT AT A TIME, not the whole array at once. Do not ever ever ever ever ever ever ever ever ever ever ever ever ever ever ever attempt to assign all the slots of an array at once.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. errors with virtual functions
    By vril in forum C++ Programming
    Replies: 6
    Last Post: 12-21-2010, 10:34 AM
  2. Functions, have errors...NEED HELP FAST
    By alkamenes in forum C++ Programming
    Replies: 6
    Last Post: 11-02-2009, 03:00 PM
  3. File Searching API functions
    By @nthony in forum Windows Programming
    Replies: 2
    Last Post: 01-13-2007, 06:22 PM
  4. 3x syntax errors :S, Help a student finalise an assignment
    By Chickenhawk in forum C Programming
    Replies: 14
    Last Post: 07-27-2006, 05:14 AM
  5. Built in Searching functions
    By MethodMan in forum C Programming
    Replies: 6
    Last Post: 03-07-2003, 09:08 AM