Return Error

This is a discussion on Return Error within the C++ Programming forums, part of the General Programming Boards category; Code: #include <cstdlib> #include <iostream> using namespace std; struct Student { string id; string fname; string sname; float cpa; }; ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    20

    Return Error

    Code:
    #include <cstdlib>
    #include <iostream>
    
    
    using namespace std;
    
    
    struct Student
    {
        string id;
        string fname;
        string sname;
        float cpa;
    };
    
    
    void inputStudent(Student s)
    {
        cout<<"Enter id"<<endl;
        cin>>s.id;
        cout<<"Enter First Name"<<endl;
        cin>>s.fname;
        cout<<"Enter Surname"<<endl;
        cin>>s.sname;
        cout<<"Enter cpa"<<endl;
        cin>>s.cpa;
        cin.ignore();
    }
    
    
    void displayStudent(Student s)
    {
        cout<<"ID :"<<s.id;
        cout<<"\tSurname :"<<s.sname;
        cout<<"\tFirstname :"<<s.fname;
        cout<<"\tCPA: "<<s.cpa<<endl;
    }
    
    
    int main()
    {
        float totalStudent;
    
    
        const int MAXStudent = 2;
    
    
    
    
        Student s1[MAXStudent];
        cout<<"\nProcessing details of Student"<<endl;
        for(int i=0; i<MAXStudent; i++)
        {
            cout<<"Details for student"<<i+1<<endl;
            inputStudent(s1[i]);
        }
    
    
        for(int i=0; i<MAXStudent; i++)
        {
            cout<<"Details for student"<<i+1<<endl;
            displayStudent(s1[i]);
        }
        return EXIT_SUCCESS;
    }
    When the program displays Student details, the fields are blank. In theory I think it is correctly written. Any idea on this? Thanks.

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    inputStudent is modifying a local copy of it's parameter. You need to receive a reference (this is C++ by the way).
    Code:
    void inputStudent(Student &s)
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,794
    Quote Originally Posted by oogabooga
    this is C++ by the way
    Indeed. Thread moved to C++ Programming forum.
    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

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    20
    Okay.
    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error in return variable.
    By jericjones45 in forum C++ Programming
    Replies: 2
    Last Post: 02-26-2009, 08:22 PM
  2. error when return 0; in int main()w
    By elninio in forum C++ Programming
    Replies: 19
    Last Post: 09-18-2008, 02:32 AM
  3. return error
    By warney_out in forum C Programming
    Replies: 2
    Last Post: 12-06-2004, 04:21 AM
  4. Return Return Error
    By javacvb in forum C++ Programming
    Replies: 8
    Last Post: 12-16-2003, 03:17 PM
  5. atoi return error
    By rotis23 in forum C Programming
    Replies: 5
    Last Post: 10-30-2002, 01:08 PM

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