C ++如何打印从一组对象输入数据的对象,而不是打印所有对象

时间:2019-10-04 15:58:45

标签: c++ arrays loops object

我有一个任务来创建一个学生记录系统,在该系统中,我必须输入学生的姓名,地址和cgpa。我创建了一个大小为20的对象数组,并在每个对象中从用户那里获取名称,地址和cgpa。现在,用户不必输入所有20个对象的数据,而是取决于用户要输入多少条记录。我创建了一个仅显示输入记录的函数,但问题是它还显示了用户未输入任何记录的记录。 (对不起我的英语不好)。

    void Input(){
    int i,c;
    string n,a;
    cout << endl << "\t\t\t\t      Enter Name of the Student : ";
    cin >> n;
    set_name(n);
    cout << endl << "\t\t\t\t      Enter ID of the Student : ";
    cin >> i;
    set_id(i);
    cout << endl << "\t\t\t\t      Enter CGPA of the Student : ";
    cin >> c;
    set_cgpa(c);
    cout << endl << "\t\t\t\t      Enter Address of the Student : ";
    cin >> a;
    set_address(a);
}

void Output(Student s[], int const){
    for(int i = 0 ; i < 20 ; i++){
            cout << endl << "\t\t\t\t      The Name of Student is " << s[i].get_name() << "." << endl;
            cout << endl << "\t\t\t\t      The ID of Student is " << s[i].get_id() << "." << endl;
            cout << endl << "\t\t\t\t      The CGPA of Student is " << s[i].get_cgpa() << "." << endl;
            cout << endl << "\t\t\t\t      The Address of Student is " << s[i].get_address() << "." << endl << endl << endl;
    }
    system("pause");
}

int main(){
Student s[20];
int option;
for(int i = 0 ; i < 20 ; i++){
    system("cls");
    s[i].drawmenu();
    cout << "\t\t\t\t\t Enter An Option : ";
    cin >> option;
    if(option == 1){
        s[i].Input();
    }
    else if(option == 2){
        s[i].Output(s,20);
    }
    else if(option == 3){
        s[i].Search(s,20);
    }
    else if(option == 4){
        s[i].Update(s,20);
    }
    else if(option == 5){
        s[i].Delete(s,20);
    }
    else if(option == 6){
        break;
    }
    else if(option != 1 || option != 2 || option != 3 || option != 4 || option != 5 || option != 6){
        cout << endl << "\t\t\t\t    Entered Option is Not Valid." << endl;
        system("pause");
    }
}
return 1;

}

0 个答案:

没有答案