下午好.....我目前是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;
}
}
第1步:縮進你的代碼。在點擊提交之前,使用實時預覽驗證代碼是否可讀。 –