2015-05-27 38 views
-3

下午好.....我目前是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; 
     } 
    } 
+3

第1步:縮進你的代碼。在點擊提交之前,使用實時預覽驗證代碼是否可讀。 –

回答

5

for循環,你顯示的名字,你真的是i << studentVector.size()

+0

這確實是個問題。簡單的錯字。 –

+0

我想這是我卡住的地方。我們觀看的視頻具有循環聲明並且在視頻中有效......不確定它「應該」是什麼。任何其他的幫助將是太棒了。 – Magnys75

+0

它看起來像你試圖比較這些值,但你不是。看看http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B。 – rds504