首页 > 解决方案 > 有没有办法将 5 个学生的名字添加到一个向量中,每个学生都有 3 个考试分数?

问题描述

我又迷路了,我知道有一种方法可以使用 push_back 添加到向量中,但我不知道如何才能真正让它工作。我想要做的是将 5 个学生添加到一个向量(Egrades)中,并且每个学生收集 3 个单独的考试成绩并将它们附加到该学生的姓名上,以便我以后可以计算平均值。

在下面的代码中,我取出了案例中的行,只放了学生 1 来缩短长度。

我哪里错了?我是不是忘记放东西了?

#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>

using namespace std;


int main(void)

{

    // Delcarations

    int score1;
    int score2;
    int score3;

    int scores[5];

    vector <int> Egrades;

    string student1;
    string student2;
    string student3;
    string student4;
    string student5;

    // array with menu options
    string menu[15];
    menu[0] = "                                 **************";
    menu[1] = "                              ***  MAIN  MENU  ***";
    menu[2] = "                                 **************";
    menu[3] = "                    Please Choose From The Following Options:";
    menu[6] = "  1. Add Student:";
    menu[8] = "  2. Compute Student Test Score Average:";
    menu[10] = "  3. Search A Student Average Test Score:";
    menu[12] = "  4. Compute The Average Of The Average Exams:";
    menu[14] = "  5. Exit";

 char selection;

    cout << "\t\t\t     Enter Your Selection: ", cin >> selection, cout << endl;

        switch (selection)

        {


        case '1':

            system("CLS");

            do
            {

                {
                    cout << "Student Information Section:\n\n";

                    // Student 1 Info
                    cout << "Enter Student 1 Name:\n";
                    cin >> student1; cout << endl;

                    cout << "Enter Exam 1 Score: ";
                    cin >> score1; cout << endl;

                    cout << "Enter Exam 2 Score: ";
                    cin >> score2; cout << endl;

                    cout << "Enter Exam 3 Score: ";
                    cin >> score3; cout << "\n" << endl;
                }

                    system("pause");

                    return main();

            } 
            
            while (cin >> score1, score2, score3);

            break;
                
        case '2':

            break;
            
        case '3':

            break;

        case '4':    

            break;

        case '5':

            exit(-1);

            break;

        default: cout << "\n Invalid selection\n\n";

        }


    return 0;

}

标签: c++arraysalgorithmvectorpush-back

解决方案


推荐阅读