0

I was wondering if anyone could help me. I am working on a database management system and when I try to test out adding a new student. The program will randomly skip inputs and go straight to the next one and not allowing me to enter in the skipped input.

I.e it will allow me to enter details until name, display the prompt for the programme of study then instantly ask for the level of study on the same line.

int main()
{

    //Setting max length of the name data entered to 20
    char data[20];
    int n = 0, option = 0, nCount = 0;

    //Initial subject mark
    string empty = "00";
    string programme = " ";
    string department = " ";
    char level = ' ';

    //Name of file in which database is stored
    ifstream f("SDB.txt");
    string line;

    //For loop counts total number of lines in the file
    for (int i = 0; std::getline(f, line); ++i) {
        nCount++;
    }

    //The option page
    while (option != 5) {
        cout << "\nPlease choose an option\n"
                "1. Add new students\n"
                "2. Student Login\n"
                "3. Staff Login\n"
                "4. Admin View\n"
                "5. Exit\n"
                "Enter option: ";

        cin >> option;

        //What happens when you select each option
        if (option == 1) {
            cout << "Enter the number of students: ";
            cin >> n;

            nCount = nCount + n;

            for (int i = 0; i < n; i++) {
                ofstream outfile;
                outfile.open("SDB.txt", ios::app);

                //Stores the entire data of a student
                cout << "Please enter your student ID number: ";
                cin >> data;
                outfile << data << "\t";

                cout << "Please enter your name: ";
                cin >> data;
                int len = strlen(data);

                //Check the length of the entered name
                while (len < 20) {
                    data[len] = ' ';
                    len = len + 1;
                }
                outfile << data << "\n";

                //Joins you up under a Programme of study
                cout << "Please enter the Programme of study you are in: ";
                cin >> programme;
                outfile << programme << "\t";

                cout << "Please enter which level of study you are in(C, I or H): ";
                cin >> level;
                outfile << level << "\t";

                //Inserts empty data into the file initially
                outfile << empty << "\t";
                outfile << empty << "\t";
                outfile << empty << "\t";
                outfile << empty << "\t";

                cout << "\nPlease enter which department you are in: ";
                cin >> department;

                outfile << department << endl;
            }
        }
    }
drescherjm
  • 10,365
  • 5
  • 44
  • 64
LGGawd
  • 1
  • 1
  • 1
    @drescherjm you mean `>>` instead of `<<` – Remy Lebeau Jul 28 '22 at 20:18
  • I would agree with @drescherjm except that as far as I can see you never call cin.getline() in the code that you posted. One thing that I do see is that `data` is a char[20] - if the user ever enters too much you will have a buffer overflow and undefined behavior - use std::string instead. – Avi Berger Jul 28 '22 at 20:21

0 Answers0