麻烦与矢量编码支付名称

Trouble dispaying names with Vector coding

本文关键字:编码 麻烦      更新时间:2023-10-16

下午好.....我目前是一名学生在C++课程,我们正在为向量编写代码。在作业中,我们必须显示3个学生的名字。当我在以下代码运行后添加名称时,它只显示向量的数量而不是名称(这是必需的)。感谢您的帮助。

#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using std::cout;
using std::cin;
using namespace std;
int main()
{
    bool running = true;
    char choice;
    vector <string> studentVector;
    cout << "My Student Vector" << endl;
    while (running == true)
    {
        cout << "Choose a Selection: [A]dd Student, [V]iew Student, [R]emove      Student, [E]xit" << endl;
        cin >> choice;
        cin.ignore();
        if (choice == 'A' || choice == 'a')
        {
            cout << "Enter Student's Name: ";
            string tempVar;
            getline(cin, tempVar);
            studentVector.push_back(tempVar);
        }
        else if (choice == 'V' || choice == 'v')
        {
            cout << "You have " << studentVector.size() << "Student(s)." << endl;
            for (int i = 0; i << studentVector.size(); i++)
            {
                cout << i << ". " << studentVector.at(i) << endl;
            }
        }
        else if (choice == 'R' || choice == 'r')
        {
            int pos;
            cout << "Enter the vector position for the student in question: ";
            cin >> pos;
            cin.ignore();
            studentVector.erase(studentVector.begin() + pos);
        }
        else if (choice == 'E' || choice == 'e')
        {
            running = false;
        }
        else
        {
            cout << "Invalid Selection" << endl;
        }
    }

在显示名称的for循环中,您真的是指i << studentVector.size()吗?