显示每门课程的学生人数

时间:2019-10-04 12:17:00

标签: c++

在前面的问题中,我被建议使用一个类来存储学生信息。现在的问题是:当我一次显示学生,然后尝试在不退出程序的情况下输入更多学生时,它不会显示新添加的学生。

代码:

#include <iostream>
#include <conio.h>
#include <stdio.h>
#include <string>
#include <iomanip>
#include <fstream>
#include <cstring>

using namespace std;

class students {
public:
    char name[50];
    char sname[45];

    int idno;
    char cours[40];
    int age;
    int modules;

    void read_data();
    void display();
};

void students::read_data()
{
    cout << "Enter name :";
    cin >> name;
    cout << "Enter Surname : ";
    cin >> sname;
    cout << "Enter course : ";
    cin >> cours;
    cout << "Enter age :";
    cin >> age;
    cout << "Enter modules:";
    cin >> modules;
}
void students::display()
{
    cout << "-----------------------------------------------" << endl;
    cout << "Student ID no : " << idno << endl;
    cout << "Student name is :" << name << endl;
    cout << "Student Surname is : " << sname << endl;
    cout << "Student course is :" << cours << endl;
    cout << "Student age is  :" << age << endl;
    cout << "Student modules are:" << modules << endl;
    cout << "----------------------------------------------" << endl;
}

int main()
{
    ofstream cfile;
    ofstream sfile;
    char dow;
    int arr = 0;
    do {

        students stud[100];

        cout << "Press 1 Enter record \n";
        cout << "Press 2 To check student course file \n";
        cout << "\n\t Select option::";

        int idcheck = 0;

        int sw;
        cin >> sw;

        switch (sw) {

        case 1:

            cout << "\n Enter the data of the student no " << arr + 1 << " is :\n";

            cout << "\t Enter the Roll No = ";
            int id2;
            int id;

            cin >> id;

            for (int j = 0; j <= arr; j++) {

                id2 = id;
                if (id2 == stud[j].idno) {
                    idcheck = 1;
                }
            }
            if (idcheck != 1) {
                stud[arr].idno = id;

                stud[arr].read_data();

                arr = arr + 1;
            }
            else {
                cout << "This Record is Already Entered \n";
            }

            break;
        case 2: {
            string str;
            int n, count = 0;

            cout << "Enter number of courses: ";
            cin >> n;

            for (int i = 0; i <= n - 1; i++) {

                cout << "Enter course: ";
                cin >> str;

                count = 0;
                for (int i = 0; i <= str.size(); i++)

                {

                    if (str == stud[i].cours)

                    {
                        count++;
                    }
                }

                cout << "Number of students doing " << str << " are " << count << endl;
            }
        }
            cout << "\n__________________________________________________________________________________________________________________________________________________________________" << endl;
            for (int i = 0; i < 1; i++) {
                cout << setw(12) << "Rollno ||";
                cout << setw(12) << " Name   ||";
                cout << setw(12) << " Surname    || ";
                cout << setw(12) << "Age  ||";
                cout << setw(12) << " Course  ||";
                cout << setw(12) << " Module   ||";
                for (int k = 0; k < arr; k++) {
                    if (stud[k].idno != 'd') {
                        cout << "\n";
                        cout << " ";
                        cout << setw(5) << stud[k].idno;

                        cout << setw(15) << stud[k].name;

                        cout << setw(14) << stud[k].sname;

                        cout << setw(14) << stud[k].age;

                        cout << setw(12) << stud[k].cours;

                        cout << setw(14) << stud[k].modules;
                    }
                }
            }
            cout << "\n______________________________________________________________________________________________________________________________________________________________________________" << endl;

            break;

            break;

        default:
            cout << "\t Wrong option Selected ";
            break;
        }

        cout << "\n\n\t Do You Want to Continue Again [Y/N]";
        cin >> dow;

    } while (dow == 'y');
    return 0;
    getch();
}

1 个答案:

答案 0 :(得分:0)

for (int i = 0; i <= n - 1; i++) {

    cout << "Enter course: ";
    cin >> str;

    count = 0;
    for (int i = 0; i <= str.size(); i++) //maybe should be i<=arr;
    {
        if (str == stud[i].cours)
        {
            count++;
        }
    }

    cout << "Number of students doing " << str << " are " << count << endl;
}

我认为问题是i <= str.size();的for循环; 我认为向该课程的所有学生展示的正确方法是:i <= arr。