Alright, so I'm a bit lost as to what to do next with this and am looking for some direction. The assignment sheet is as follows:

CSC 265 – Fall 2009 – Program 1

Due: Thursday October 8, 10 minutes before class starts

Write a header file, implementation file, and main program in which you create a Student class, and then use your class in a menu-driven program. The menu should contain:
1. Create New Student
2. Delete Student (by ID number)
3. Display All students
4. Search for Student (by ID number)
5. Quit

Users will be able to enter in students first and last name, address, and current GPA (ex. 3.8). The ID number will be randomly assigned with a value between 1 and 9999.

Basically, after you create the Student class, you will use it in a main program that works with an array of Students. Make the array size 30, and make sure all input/output is neat and fully error checked.

What to submit:
Submit source code via email. If you are absent, it is still your responsibility to submit on time. Put group members’ last names in the subject line.

Keep in mind the following:
• Name your source files student.h, student.cpp, and main.cpp.
• With invalid data, your program should respond appropriately.
• Each function prototype should have a description, preconditions, and postconditions.
• Test your code when you think you are finished. Try to break it. After you are done trying to break it, I will then try.

Good Luck! Remember, questions are encouraged.

What I have thus far:

student.h

#ifndef STUDENT_H
#define STUDENT_H
using namespace std;


class Student
{
  public:
    // CONSTRUCTOR
    Student();
    
    // MODIFICATION MEMBER FUNCTIONS
    void setID (const int);
    void setAddress (const string);
    void setFName (const string);
    void setLName (const string);
    void setGPA (const float);
    

    // CONSTANT MEMBER FUNCTIONS
    float  getGPA (void) const;
    string getFName (void) const;
    string getLName (void) const;
    string getAddress (void) const;
    int getID (void) const;
    void display (void) const;

  private:
          int  ID;
          float GPA; 
          string Address;
          string FName;
          string LName;
          
};

#endif

student.cpp

#include <iostream>
#include <string>
#include "student.h"
using namespace std; 


// CONSTRUCTOR
Student::Student()
{
  ID = 0;
  GPA = 0;
  Address = "";
  FName = "";
  LName = "";
}

// MODIFICATION MEMBER FUNCTIONS
void Student::setID (const int value)
{  
     for(int index=0; index<20; index++)
     {
             value = (rand()%10)+1;
     } 
     if(value>0 && value<9999)
             ID = value;
}

void Student::setAddress (const string value)
{
     cout << "Please enter new student's address: ";
     cin >> value;
     if(value != "")
             Address = value;
     else 
          cout << "Please enter a valid address!";
}

void Student::setFName (const string value)
{
     cout << "Please enter new student's first name: ";
     cin >> value;
     if(value != "")
              FName = value;
     else
         cout << "Please enter student's first name!";
}

void Student::setLName (const string value)
{
     cout << "Please enter new student's last name: ";
     cin >> value;
     if(value != "")
              LName = value;
     else
         cout << "Please enter student's last name!";
}

void Student::setGPA(const int value)
{
     cout << "Please enter new student's GPA (ex: 3.8): ";
     cin >> value;
     if(value > 0 && value < 4)
              pages = value;
     else
         cout << "Please enter a valid GPA for the student!";
}

     


// CONSTANT MEMBER FUNCTIONS

int Student::getGPA(void) const
{
  return GPA;
}

int Student::getID(void) const
{
  return ID;
}

string Student::getFName(void) const
{
  return FName;
}

string Student::getLName(void) const
{
  return LName;
}

string Student::getAddress(void) const
{
  return Address;
}


void Student::display(void) const
{
    cout << "ID: " << ID << endl; 
    cout << "Name: " << FName << " " << LName << endl;
    cout << "Address: " << Address << endl;
    cout << "GPA: " << GPA << endl;
}

main.cpp

#include <iostream>
#include <string>
#include "student.h"

using namespace std;

int main(void)
{
  Student students[30];
  int term = 0;
  int searchID = 0;
  
  do
  {
      for (int j=0; j<30; j++)
      {
        cout << "MENU" << endl;
        cout << "1. Create New Student" << endl;
        cout << "2. Delete Student (by ID Number)" << endl;
        cout << "3. Display All Students" << endl;
        cout << "4. Search for Student (by ID Number)" << endl;
        cout << "5. Quit" << endl;
        cin >> menuChoice;
        if (menuChoice == 1)
           {
                       students[j].setID();
                       students[j].setFName();
                       students[j].setLName();
                       students[j].setAddress();
                       students[j].setGPA();
           }
        else if (menuChoice == 2)
           {
                       students[j].setID(0);
                       students[j].setFName("");
                       students[j].setLName("");
                       students[j].setAddress("");
                       students[j].setGPA(0);
           }
        else if (menuChoice == 3)
           {
                       for (int k=0; k<j; k++)
                           {
                                students[k].display();
                           }
           }
           
        else if (menuChoice == 4)
           {
                       cout << "Which student are you looking for? (Please enter their ID Number) ";
                       cin >> searchID;
                       students[searchID].display();
           }
        else if (menuChoice = 5)
           {
                       term = 1;
           }
        else
            cout << "Please enter in a correct menu choice: ";
      }
  }while(term == 0);
        
  system("pause");
  return 0;
}

Any help will be greatly appreciated!!! It is definitely possible that I could be doing something entirely wrong. It's just been so long since I've taken a programming course.

Recommended Answers

All 5 Replies

Get rid of the for-loop that starts on line 15. There's no need for it. Your do-while loop should be able to handle any exiting. There is no need for a for-loop and what if I want to quit before I enter an option 30 times? If the array of students was size 1000, would I be forced to enter an option 1000 times before I was allowed to quit? Your do-while loop needs to handle all of this. Having a nested for-loop in there can only be bad.

That said, you need some way of knowing how many students you have. right now you do it with your for-loop variable, which I am suggesting you take out, so you need to replace it with something else. My recommendation is to have a variable called numStudents . When you add a student, you increment that variable. When you delete a student, you decrement that variable. When you search or display, you don't need to change it at all. Right now, if you add two students, then delete 1, j is always incremented, so you end up with a value of supposedly having 3 students instead of the real value of having only 1 student. That's going to really screw things up when you try to display 3 students if you only are supposed to have one.

So get rid of the for loop, get rid of the j variable, replace it with numStudents, and make sure it accurately reflects the actual number of students in the array at all times.

How would I go about adding/deleting/finding students in the array then? I guess that is where my root problem is.

How would I go about adding/deleting/finding students in the array then? I guess that is where my root problem is.

The way you do it now is incorrect:

void Student::setLName (const string value)
{
     cout << "Please enter new student's last name: ";
     cin >> value;
     if(value != "")
              LName = value;
     else
         cout << "Please enter student's last name!";
}

If you don't use the parameter passed to a function (and you don't - value is immediately overwritten), don't pass it.

void Student::setLName ()
{
     string value;
     cout << "Please enter new student's last name: ";
     cin >> value;
     if(value != "")
              LName = value;
     else
         cout << "Please enter student's last name!";
}

There are other problems with your function. How do I enter the name you've told me to re-enter when the function immediately ends afterwards?

And if you don't change it to the way I have it above, this won't work:

if (menuChoice == 1)
           {
                       students[j].setID();
                       students[j].setFName();
                       students[j].setLName();
                       students[j].setAddress();
                       students[j].setGPA();
           }

Line 5 - Where's the parameter?

But if you DO do it the way I've changed it, then THIS won't work:

else if (menuChoice == 2)
           {
                       students[j].setID(0);
                       students[j].setFName("");
                       students[j].setLName("");
                       students[j].setAddress("");
                       students[j].setGPA(0);
           }

My guess is that the function is supposed to have a parameter but your cin statements happen in main, not setLName (the same would be true of the other set functions).

student.h

#ifndef STUDENT_H
#define STUDENT_H
using namespace std;


class Student
{
  public:
    // CONSTRUCTOR
    Student();
    
    // MODIFICATION MEMBER FUNCTIONS
    void setID (const int);
    void setAddress (const string);
    void setFName (const string);
    void setLName (const string);
    void setGPA (const float);
    void deleteStudent(const string, const string, const string, const float, const int);
    

    // CONSTANT MEMBER FUNCTIONS
    float  getGPA (void) const;
    string getFName (void) const;
    string getLName (void) const;
    string getAddress (void) const;
    int getID (void) const;
    void display (void) const;

  private:
          int  ID;
          float GPA; 
          string Address;
          string FName;
          string LName;
          
};

#endif

student.cpp

#include <iostream>
#include <string>
#include <cstdlib>
#include "student.h"
using namespace std; 


// CONSTRUCTOR
Student::Student()
{
  ID = 0;
  GPA = 0;
  Address = "";
  FName = "";
  LName = "";
}

// MODIFICATION MEMBER FUNCTIONS
void Student::setID (const int value)
{
          //   value = (rand()%9999)+1; 
             ID = value;
}

void Student::setAddress (const string value)
{
    // cout << "Please enter new student's address: ";
    // cin >> value;
     while(value == "")
     {
          cout << "Please enter a valid address! ";
          cin >> value;
     }
     Address = value;
}

void Student::setFName (const string value)
{
     //cout << "Please enter new student's first name: ";
     //cin >> value;
     while(value == "")
     {
          cout << "Please enter a student's first name! ";
          cin >> value;
     }
     FName = value;
}

void Student::setLName (const string value)
{
    // cout << "Please enter new student's last name: ";
     //cin >> value;
     while(value == "")
     {
          cout << "Please enter a student's last name! ";
          cin >> value;
     }
     LName = value;
}

void Student:: deleteStudent(const string dFName, const string dLName, const string dAddress, const float dGPA, const int dID)
{
     FName = dFName;
     LName = dLName;
     Address = dAddress;
     GPA = dGPA;
     ID = dID;
}
void Student::setGPA(const float value)
{
     //cout << "Please enter new student's GPA (ex: 3.8): ";
     //cin >> value;
     while(value<0 && value > 4)
     {
                 cout << "Please enter a valid GPA for the student (0 - 4.0): ";
                 cin >> value;
     }
     GPA = value;
}

     


// CONSTANT MEMBER FUNCTIONS

float Student::getGPA(void) const
{
  return GPA;
}

int Student::getID(void) const
{
  return ID;
}

string Student::getFName(void) const
{
  return FName;
}

string Student::getLName(void) const
{
  return LName;
}

string Student::getAddress(void) const
{
  return Address;
}


void Student::display(void) const
{
    cout << "ID: " << ID << endl; 
    cout << "Name: " << FName << " " << LName << endl;
    cout << "Address: " << Address << endl;
    cout << "GPA: " << GPA << endl;
}

main.cpp

#include <iostream>
#include <string>
#include <cstdlib>
#include "student.h"

using namespace std;

int main(void)
{
  Student students[30];
  int term = 0;
  int searchID = 0;
  int numStudents = 0;
  int menuChoice = 0;
  string mFName = "";
  string mLName = "";
  string mAddress = "";
  int mID = 0;
  float mGPA = 0;
  
  do
  {
        cout << "MENU" << endl;
        cout << "1. Create New Student" << endl;
        cout << "2. Delete Student (by ID Number)" << endl;
        cout << "3. Display All Students" << endl;
        cout << "4. Search for Student (by ID Number)" << endl;
        cout << "5. Quit" << endl;
        cin >> menuChoice;
        if (menuChoice == 1)
           {
                       cout << "Please enter new student's first name: ";
                       cin >> mFName;
                       cout << "Please enter new student's last name: "; 
                       cin >> mLName;
                       cout << "Please enter new student's address: ";
                       cin >> mAddress;
                       cout << "Please enter new student's GPA: ";
                       cin >> mGPA;
                       mID = (rand()%9999)+1; 
                       students[numStudents].setID(mID);
                       students[numStudents].setFName(mFName);
                       students[numStudents].setLName(mLName);
                       students[numStudents].setAddress(mAddress);
                       students[numStudents].setGPA(mGPA);
                       numStudents++;
           }
        else if (menuChoice == 2)
           {
                       students[numStudents].deleteStudent("", "", "", 0, 0);
                      /*
                       students[numStudents].setFName("");
                       students[numStudents].setLName("");
                       students[numStudents].setAddress("");
                       students[numStudents].setGPA(0);
                       */
                       numStudents--;
           }
        else if (menuChoice == 3)
           {
                       for (int k=0; k<=numStudents; k++)
                           {
                                students[k].display();
                           }
           }
           
        else if (menuChoice == 4)
           {
                       cout << "Which student are you looking for? (Please enter their ID Number) ";
                       cin >> searchID;
                       students[searchID].display();
           }
        else if (menuChoice = 5)
           {
                       term = 1;
           }
        else
            cout << "Please enter in a correct menu choice: ";
  }while(term == 0);
        
  system("pause");
  return 0;
}

Updated my code to address (I think) previous issues brought up. I'm having another error messages.

line 32 : no match for 'operator>>' in 'std::cin >> value'

I get this error in every function that is using a string. Thoughts?

commented: As for me doing assignments that are connected to programming languages is an occupation that is hard for completing! som writing assignments are assi +0

I guess I can't edit my own post, but I figured out the problem above. I had to remove the constant since I'm changing the value.

Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.