下午好.....我目前是C ++课程的学生,我们正在编写Vector代码。在作业中,我们必须显示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;
}
}
答案 0 :(得分:5)
在显示名称的for
循环中,您的意思是i << studentVector.size()
吗?