0

I have a project in my C++ class - we're supposed to make a "simple student management system" comprised of a class for the student attribute variables, and a main function with a branch statement that lets the user input names and IDs for the students. I know that I need to use an array to make "slots" for the students, and that my branch statements need to let the user input values into those slots, but I'm not exactly sure how to do it. Here is the code that I have so far.

#include <string>
#include <iostream>
#include <utility>

using std::string;
using std::cin;
using std::cout;
using std::endl;

struct Student {
private:
    int id;
    string name;
    int birthday;
public:
    Student()
    {
        id = 0;
        birthday = 0;
    }
    Student(int id, string name, int birthday)
    {
        //set your parameters to the class variables
        this->id = id;
        this->name = name;
        this->birthday = birthday;
    }
    void setID(int id)
    {
        this->id = id;
    }
    int getID() {
        return id;
    }
    void setName(string name)
    {
        this->name = name;
    }
    string getName()
    {
        return name;
    }
    void setBirthday(int birthday)
    {
        this->birthday = birthday;
    }
    int getBirthday()
    {
        return birthday;
    }
    void output() {
        cout << id << name << birthday << endl;
    }
};

int main() {
    Student arr[50];
    cout << "Student Management System" << endl;
    cout << "Press 'a' to add a student" << endl;
    char a = 1;
    int y = 1;
    while (a == 'a') {
        switch (y)
        {
            cout << "Input Student ID:";
            cin >> id;
        }
    }
}

What I'm focusing on most is the fourth line from the bottom. I was told that I need to use my setters, so I said that I want what my user inputs to be treated as the value of the ID variable that I set in the class. However, when I wrote this out, I was given an error. Could someone tell me what the issue is?

  • 2
    What are you really asking about? How to "delete" elements from an array? How to read input into objects of your class? How to use basic statements like your `switch`? Why `a = 1` leads to `a == 'a'` being false? Please take some time to [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask). And you definitely need to [find a good beginners book or two](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) to read. – Some programmer dude Oct 03 '17 at 14:37
  • BTW, you can eliminate the `this->` notation by giving different names to parameters and data members. Saves typing and possible typos. – Thomas Matthews Oct 03 '17 at 15:31

2 Answers2

0

You should try to get your switch statement working correctly. To use classes setters, you can store the user input to a temporary variable then from your one student you can call the member function. i.e. in your case:
arr[index].setID(tempInputVariable);

taras
  • 6,566
  • 10
  • 39
  • 50
Chris
  • 1
  • 1
0

There is no id in your main function or as a global variable.

I suggest you overload operator >> to have your structure extract its members from the data stream:

struct Student
{
 //...
  public:
    friend std::istream& operator>>(std::istream& input, Student& s);
};

std::istream& operator>>(std::istream& input, Student& s)
{
  input >> s.id;
  input >> s.name;
  input >> s.birthday;
  return input;
}

Although the above code doesn't use setters, it is the preferred method for inputting data.

The overload can be modified to use setters (kind of overkill):

std::istream& operator>>(std::istream& input, Student& s)
{
  int id;
  input >> id;
  s.setID(id);

  std::string name;
  input >> name;
  s.setName(name);

  int birthday;
  input >> birthday;
  s.setBirthday(birthday);
  return input;
}

If you don't like the overload, you can perform the steps in your main function:

//...
Student new_student;
//...
{
  int student_id;
  std::cout << "Input Student ID:";
  std::cin >> student_id;
  new_student.setID(student_id);

  std::string student_name;
  std::cout << "Input Student Name: ";
  std::cin >> student_name;
  new_student.setName(student_name);

  int student_birthday;
  std::cout << "Input Student Birthday: ";
  std::cin >> student_birthday;
  new_student.setBirthday(student_birthday);
}

Edit 1: The Database
You'll probably need to store or remember the students. This is easy using the first method above:

std::vector<Student> database;
Student s;
std::cout << "Enter student information (ID, Name and birthday, separated by spaces:\n";
std::cin >> s;
// Append to the database
database.push_back(s);

If you haven't learned std::vector you can try an array:

const size_t MAXIMUM_STUDENTS = 16;
Student database[MAXIMUM_STUDENTS];
size_t database_index = 0;
//...
Student s;
std::cout << "Enter student information (ID, Name and birthday, separated by spaces:\n";
std::cin >> s;
// Append to database
database[database_index] = s;
++database_index;
Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154