0

I'm creating a c++ program to enter student information in a structure in c++. So when I enter the students it's getting the data and redirects me to the main menu. When I select an option to see that that data I have entered it's not displaying but when I call that same data to be displayed soon after entering it, it's displaying. My code is below:

//My structure to get students
struct Students{
    string RegNumber;
    string StudentFullName;
    string Program;
    string Lvl;
};

//User selects from this menu
MainMenu(){
    menu:
        cout <<"Select From The Menu\n";
        cout <<"1. Add Student Details\n 2. View Student details\n";

    //User selected from the menu option function
    int UserInputOption;
    cin >> UserInputOption;

    //validating User Input
    if(UserInputOption < 2){
        int NumberOfStudents;

        Students student[NumberOfStudents];
        //Switch statements to check user input
        switch(UserInputOption){
            case 1:
                //Enter Number of students to add to the system
                cout << "Enter The Number of Students You Want To Enter\n";
                cin >> NumberOfStudents;

                //Adding new students to the system
                cout << "*********************************************\n";
                cout << "Enrolling Students to the system\n";
                cout << "*********************************************\n";

                //loop for entering student details
                for(int i=0; i < NumberOfStudents; i++){
                    cout <<"Enter Student Reg Number: ";
                    cin >> student[i].RegNumber;
                    cout <<"Enter Student Full Name: ";
                    cin >> student[i].StudentFullName;
                    cout <<"Enter Student Program: ";
                    cin >> student[i].Program;
                    cout <<"Enter Student Level: ";
                    cin >> student[i].Lvl;
                    cout << "\n";
                }
                cout << "\n*********************************************\n";
                cout << "Students Enrolled To The System\n";
                cout << "*********************************************\n";
                //After Entering the student details the system should go back to the main menu
                goto menu;

                break;

            case 2:
                //Displaying all students of in the system
                for(int i=0; i < 1; i++){
                    cout << student[i].RegNumber;
                    cout << student[i].StudentFullName;
                    cout << student[i].Program;
                    cout << student[i].Lvl;
                }

                break;

        }
    }else{
        cout <<"Invalid Input"<<endl;
    }
}
 
int main()
{
    //Calling methods to the main method
    MainMenu();

    return 0;
}
Anesu Phiri
  • 159
  • 1
  • 1
  • 8

1 Answers1

0

There are multiple issues here:

Demanding Issues

  • UserInputOption < 2. This isn't a very good validation since it mishandles the cases where UserInputOption = 2 or UserInputOption < 0. This is better as 1 <= UserInputOption && UserInputOption <= 2.

  • int NumberOfStudents;. This variable isn't initialised, the value stored can be anything. So the next command is undefined behaviour: Students student[NumberOfStudents];.

  • Students student[NumberOfStudents];. Defining the array inside the block isn't a good idea. This will recreate the array everytime, and the data you store in one iteration is not guaranteed to be there on the next iteration. It'd be better to put the array outside, before the menu: label.
    Currently, the size of the array is not determined yet. One strategy for this is to define an upper limit, a variable to keep track of the array's active size. For example:

    const int MAX_STUDENTS = 100;   // Upper limit.
    Students student[MAX_STUDENTS];
    int num_students = 0;           // Current array size.
    
    student[0] = Students{"000", "John Doe", "Math", "Level 1"};
    num_students += 1; // Update `num_students` to reflect how many records are in the array.
    

Nitpicks

  • MainMenu(). It is good practice to provide the return type of the function, even if it doesn't return anything.

    void MainMenu() {
      // ...
    }
    
  • Avoid using goto.

TrebledJ
  • 8,713
  • 7
  • 26
  • 48