Thread: help with class

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    38

    help with class

    I posted last night on this program but am now running into a different problem, even though i think it is the same issue

    here is the code
    Code:
    /*
      Jessie Brown
      cop2334
    
    
    
    
    
    
      */
    
    #include "Student.h"
    //Prototypes -- function declarations.
      //-----------------------------------
    
       void Menu_Selection();
       void enter_students (int,int& total);
    
    
    
    
    
    int main()
    {
    
    
      Menu_Selection();
    
    
    
    
    
    
    
        // the following lines keep the console open
            cout << "\nPress <ENTER> to continue.\n";
            fflush(stdin);
            cin.get();
    
    
    	return 0;
    }
    
    
    /*------------------------------------------------------------------------------
            This section of code is the initial screen after employee signs in
                    to system.  From here said employee can choose what she must
                    do with each customer being assisted.
    ------------------------------------------------------------------------------*/
    
    
      void Menu_Selection()
    {
    
    
         Student student[STUDENT_LIMIT];
         int total=0, examnumber=0;
         int exam[EXAM_LIMIT];
         int studentID;
         int option;
    
         do
         {
              cout << endl
                   << "1. Add a new student to roster. \n \n"
                   << "2. Enter exam score for all students. \n \n"
                   << "3. Display student exam averages. \n \n"
                   << "4. Display course information for all students. \n \n"
                   << "5. Exit program: \n \n";
    
              cin >> option;
              cout << endl;
              
              switch (option)
               {
                    case 1:
                         enter_students (STUDENT_LIMIT,total);
                         cout << endl;
                         break;
                     case 2:
                         cout << "works 2";
                         cout << endl;
                         break;
                    case 3:
                        cout << "works 3";
                         cout << endl;
                         break;
                    case 4:
                        /*------------------------------------------------------------------------------
                        This function prints all acounts listed in the "bank array" as well as thier
                            corresponding balances.
                        ----------------------------------------------*/
    
                            for( int i = 0; i < total ; i++ )
                            {
                             Student studentTemp;
                             cout << studentTemp.getFullName() << endl;
                             cout << "ID: " << studentTemp.getID() << endl;
    
                            }
    
    
                         break;
                   case 5:
                        cout << "End of Program.\n";
                        break;
                    default:
                    cout << "Not a valid option. Please choose a valid option. \n\n";
               }
         }while (option != 5);
    
    
    }
    
    /*------------------------------------------------------------------------------
     This function allows a user to add an account with its beginning balance.
            accounts are to be sorted and no to account numbers are to be the same
    ------------------------------------------------------------------------------*/
    void enter_students (const int STUDENT_LIMIT, int& total)
    {
     string temp1;
     string temp2;
     Student studentTemp;
     int tempID;
    
    
     if(total >= STUDENT_LIMIT)
           {
            cout <<"Registration closed for this section.\n";
           }
          else if(total <STUDENT_LIMIT)
            {
             cout <<"Please enter students first name.\n";
             cin >> temp1;
             cout <<"Please enter students last name.\n";
             cin >> temp2;
             studentTemp.setName(temp1,temp2);
    
             cout << "Enter student ID.\n";
             cin >> tempID;
             studentTemp.setID(tempID);
    
             cout << studentTemp.getFullName() <<endl;
             cout << "ID: " << studentTemp.getID() << endl;
    
             total++;
             }
    
     }
    it compiles with no errors,
    option number one works
    option number 4 prints out -1
    I know it is how i am trying to use the class but just can not get it

    I am not even sure that it is storing the data in a new object each time i implement number 1

    any insight is helpful
    thanks
    jessie

  2. #2
    Registered User
    Join Date
    Nov 2006
    Posts
    6
    Your problem is that you are printing an empty value. When you declare studentTemp is case 4, it is initialized with no value. Also, the loop actually does nothing - you do not use i. enter_students does not do anything with the value it gets from the user.

    Solution:
    1. Move the declaration of Student student[n] to the global scope - that is, before main(), not within a function.
    2. In case 4, instead of using studentTemp, use student[i].
    3. In enter_students, instead of studentTemp, use student[total].

    Why:
    The thing is, variables with the same name in different functions share no special bond - to have a common value, they must be global variables, declared outside of functions. (not necessarily the best solution, but applicable in this case)
    Also, when you loop through a variable (in this case, i), you need to actually use that variable, otherwise you are doing the same thing over and over, which is often redundant.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    38
    That was a perfect explanation!!!!!
    now I have another question

    I just want someone to point me in the right direction,

    I need to be able to find a particular student, I will use the ID, I now how to code to find a particluar something in an array, but have tried several things for class and not working

    can someone explain the theory for me.

    thanks
    jessie

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    1. Get your working code for printing all students (suppose you already have it)
    2. Add the check whether the Id of the current student is what you want before printing
    3. Print only the student where the check is passed
    4. Change the printing action to any action you want

    Do it step-by-step compiling and running after each step is accomplished
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    38
    I have gotten farther along, it compiles with no shown errors, but does not run correctly
    here are the obvious problems i see
    case 2
    when a score is written, it is not storing it correctly, it does not record in each record for the students (i will include a copy of what i am seeing

    case 3 crashes the whole program, something is definetely not correct here! but i am not seeing it.

    If anyone has time to help me see what i am doing i so appreciate it.

    here is all the info

    main code
    Code:
    /*
      Jessie Brown
      cop2334
    
    
    
    
    
    
      */
    
    #include "Student.h"
    //Prototypes -- function declarations.
      //-----------------------------------
    
       void Menu_Selection();
       void enter_students (int,int& total);
       Student student[STUDENT_LIMIT];
       int find_student(int,int,int& total);
    
    
    
    int main()
    {
    
    
      Menu_Selection();
    
    
    
    
    
    
    
        // the following lines keep the console open
            cout << "\nPress <ENTER> to continue.\n";
            fflush(stdin);
            cin.get();
    
    
    	return 0;
    }
    
    
    /*------------------------------------------------------------------------------
            This section of code is the initial screen after employee signs in
                    to system.  From here said employee can choose what she must
                    do with each customer being assisted.
    ------------------------------------------------------------------------------*/
    
    
      void Menu_Selection()
    {
    
    
    
         int total=0;
         int studentID;
         int option;
         int examNumber;
         double score;
         do
         {
              cout << endl
                   << "1. Add a new student to roster. \n \n"
                   << "2. Enter exam score for all students. \n \n"
                   << "3. Display student exam averages. \n \n"
                   << "4. Display course information for all students. \n \n"
                   << "5. Exit program: \n \n";
    
              cin >> option;
              cout << endl;
    
              switch (option)
               {
                    case 1:
                         enter_students (STUDENT_LIMIT,total);
                         cout << endl;
                         break;
    
                     case 2:
                          /* this allows you to enter an exam number and score for
                          each stutdent in this class.  assumes that you know the
                          number, that you have put the papers in some sort of order
                          that matches the order of students....if you skip a number
                          or the score does not track your average will be off as
                          a -1 will be put in that exam number spot.*/
    
                          for( int i = 0; i < total ; i++ )
                            {
                             cout << "Enter Exam number and score.\n";
                             cin >> examNumber;
                             cin >> score;
                             student[i].setExamScore(examNumber, score);
                            }
                         break;
    
                    case 3:
                         /*this will find a particular student based on his ID number
                         and will then print the average of the scored he has taken
                         and that have been recorded, it will also list the test scores
                         individually so that one can see if there is one missing,
                         a missing one would be indicated by -1*/
    
    
                         int tempID;
                         cout << "Please enter the Student ID number\n";
                         cin >> tempID;
                          for( int i=0; i<EXAM_LIMIT; i++ )
    		          cout << student[tempID].getExamScore(i) << endl;
                              cout << "Avg = " << student[tempID].getExamAverage() << endl;
    
    
                         break;
    
                    case 4:
                         for( int i = 0; i < total ; i++ )
                            {
                             cout << student[i].getFullName() << endl;
                             cout << "ID: " << student[i].getID() << endl;
                             for(int i=0; i<EXAM_LIMIT; i++ )
                               {
    		             cout << student[i].getExamScore(i) << endl;
                               }
                             cout << "Avg = " << student[i].getExamAverage() << endl;
    
                            }
                         break;
                   case 5:
                        cout << "End of Program.\n";
                        break;
                    default:
                    cout << "Not a valid option. Please choose a valid option. \n\n";
               }
         }while (option != 5);
    
    
    }
    
    /*------------------------------------------------------------------------------
     This function allows a user to add student, assuming that each id number is
     original (like a social security number) id number is not checked for duplicity
     If class is full it will not allow you to register another student.
    ------------------------------------------------------------------------------*/
    void enter_students (const int STUDENT_LIMIT, int& total)
    {
     string temp1;
     string temp2;
    
     int tempID;
    
    
     if(total >= STUDENT_LIMIT)
           {
            cout <<"Registration closed for this section.\n";
           }
          else if(total <STUDENT_LIMIT)
            {
             cout <<"Please enter students first and last name.\n";
             cin >> temp1;
             cin >> temp2;
             student[total].setName(temp1,temp2);
    
             cout << "Enter student ID.\n";
             cin >> tempID;
             student[total].setID(tempID);
    
             cout <<  student[total].getFullName() <<endl;
             cout << "ID: " <<  student[total].getID() << endl;
    
    
    
             total++;
             }
    
     }
    class code (this can not be changed main must be made to work off of this)
    Code:
    #include "Student.h"
    
    /*
    	This is a constructor.
    	It assigns default values to a new Student.
    */
    Student::Student()
    {
    	ID = -1;
    	lastName = "";
    	firstName = "";
    	for( int i=0; i<EXAM_LIMIT; i++ ) exams[i] = -1;
    }
    
    /*
    	Returns full name formatted LAST, FIRST
    */
    string Student::getFullName()
    {
    	return lastName + ", " + firstName;
    }
    
    /*
    	Assigns the two parameters as first and last names.
    */
    void Student::setName( string theFirstName, string theLastName )
    {
    	firstName = theFirstName;
    	lastName = theLastName;
    }
    
    /*
    	Standard getter (accessor) for ID
    */
    int Student::getID()
    {
    	return ID;
    }
    
    /*
    	Standard setter (mutator) for ID
    */
    void Student::setID( int theID )
    {
    	ID = theID;
    }
    
    /*
    	Accesses the desired score.
    	If the exam number does not map to the array
    		it returns a -1.
    	Notice, this function does not test for a 
    		negative test score.
    */
    double Student::getExamScore( int examNumber )
    {
    	if( examNumber < 0 || examNumber > EXAM_LIMIT ) return -1;
    	return exams[examNumber];
    }
    
    /*
    	Sets the indicated exam to the score provided.
    	This over writes any score previously entered
    		without warning!!
    */
    void Student::setExamScore( int examNumber, double score )
    {
    	if( examNumber < 0 || examNumber > EXAM_LIMIT ) return;
    	exams[examNumber] = score;
    }
    
    /*
    	Returns the average of all positive test scores.
    	Negative scores are assumed to be invalid.
    	If the total of the scores is zero, return a zero.
    */
    double Student::getExamAverage()
    {
    	double total = 0;
    	int examCount = 0;
    	for( int i=0; i<EXAM_LIMIT; i++ )
    		if( exams[i] >= 0 ) 
    		{
    			total = total + exams[i];
    			examCount++;
    		}
    	if( examCount == 0 ) return 0;
    	else return total / examCount;
    }
    this is the header file (again this can not be changed it is a .h file)
    #include <iostream>
    #include <string>
    using namespace std;

    const int EXAM_LIMIT = 3;
    const int STUDENT_LIMIT = 10;

    class Student
    {
    public:
    Student();
    string getFullName();
    void setName( string theFirstName, string theLastName );
    int getID();
    void setID( int theID );
    double getExamScore( int examNumber );
    void setExamScore( int examNumber, double score );
    double getExamAverage();

    private:
    int ID;
    string firstName, lastName;
    double exams[EXAM_LIMIT];
    };
    this is a copy of what is happening in the console panel. hopefully you can see what i am trying to communicate that it is doing, thanks


    1. Add a new student to roster.

    2. Enter exam score for all students.

    3. Display student exam averages.

    4. Display course information for all students.

    5. Exit program:

    1

    Please enter students first and last name.
    Jessie Brown
    Enter student ID.
    262750619
    Brown, Jessie
    ID: 262750619


    1. Add a new student to roster.

    2. Enter exam score for all students.

    3. Display student exam averages.

    4. Display course information for all students.

    5. Exit program:

    1

    Please enter students first and last name.
    Bill Brown
    Enter student ID.
    259419917
    Brown, Bill
    ID: 259419917


    1. Add a new student to roster.

    2. Enter exam score for all students.

    3. Display student exam averages.

    4. Display course information for all students.

    5. Exit program:

    2

    Enter Exam number and score.
    0
    95
    Enter Exam number and score.
    0
    65

    1. Add a new student to roster.

    2. Enter exam score for all students.

    3. Display student exam averages.

    4. Display course information for all students.

    5. Exit program:

    4

    Brown, Jessie
    ID: 262750619
    95
    -1
    -1
    Avg = 95
    Brown, Bill
    ID: 259419917
    95
    -1
    -1
    Avg = 65

    1. Add a new student to roster.

    2. Enter exam score for all students.

    3. Display student exam averages.

    4. Display course information for all students.

    5. Exit program:

    2

    Enter Exam number and score.
    1
    85
    Enter Exam number and score.
    1 75

    1. Add a new student to roster.

    2. Enter exam score for all students.

    3. Display student exam averages.

    4. Display course information for all students.

    5. Exit program:

    4

    Brown, Jessie
    ID: 262750619
    95
    75
    -1
    Avg = 90
    Brown, Bill
    ID: 259419917
    95
    75
    -1
    Avg = 70

    1. Add a new student to roster.

    2. Enter exam score for all students.

    3. Display student exam averages.

    4. Display course information for all students.

    5. Exit program:

    2

    Enter Exam number and score.
    2
    85
    Enter Exam number and score.
    2 85

    1. Add a new student to roster.

    2. Enter exam score for all students.

    3. Display student exam averages.

    4. Display course information for all students.

    5. Exit program:

    4

    Brown, Jessie
    ID: 262750619
    95
    75
    -1
    Avg = 88.3333
    Brown, Bill
    ID: 259419917
    95
    75
    -1
    Avg = 75

    1. Add a new student to roster.

    2. Enter exam score for all students.

    3. Display student exam averages.

    4. Display course information for all students.

    5. Exit program:
    Thank you so very much.
    Jessie

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 09:10 AM
  2. Two conceptual questions
    By AntiScience in forum C++ Programming
    Replies: 3
    Last Post: 11-01-2007, 11:36 AM
  3. Defining derivated class problem
    By mikahell in forum C++ Programming
    Replies: 9
    Last Post: 08-22-2007, 02:46 PM
  4. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM