2017-05-06 54 views
2

我正在研究我的C++類的介紹的最終項目,並且我的代碼有問題。 getline似乎並沒有工作,儘管我按照其他時間正確地輸入了它並在其他地方看到過。這裏是一個代碼問題使用getline的問題

if (groceryMenu == 2) 
     { 
      string groceryItem; 

      system("CLS"); 

      cout << "Enter what you would like to add to the grocery list: " << endl; 
      getline(cin, groceryItem); 

      groceryVector.push_back(groceryItem); 

當此運行段,COUT線顯示在屏幕上(它只是閃爍,但與系統(「暫停」)後,你可以看到它停留),但隨後它退出if循環並返回主循環。我無法弄清楚我在這裏做錯了什麼。任何幫助表示讚賞:)

這是我的代碼的其餘部分,如果有幫助。我知道這很粗糙,我剛開始。

// 7.3 lists and vectors 
#include<iostream> 
#include<iomanip> 
#include<string> 
#include<vector> 
#include<fstream> 
#include "stdafx.h" 
using namespace std; 


int main() 
{ 
    int menuInput = 0; 
    int exitProgram = 0; 
    vector<string> groceryVector; 
    vector<string> hardwareVector; 
    vector<string> choreVector; 
    fstream inputFile, outputFile; 

    string groceryInput; 
    inputFile.open("grocery.txt"); 

    while (getline(inputFile, groceryInput)) 
    { 
     groceryVector.push_back(groceryInput); 
    } 
    inputFile.close(); 

    string hardwareInput; 
    inputFile.open("hardware.txt"); 

    while (getline(inputFile, hardwareInput)) 
    { 
     hardwareVector.push_back(hardwareInput); 
    } 
    inputFile.close(); 

    string choreInput; 
    inputFile.open("chore.txt"); 

    while (getline(inputFile, choreInput)) 
    { 
     choreVector.push_back(choreInput); 
    } 
    inputFile.close(); 

    while (exitProgram == 0) 
    { 
     system("CLS"); 

     cout << "List Manager" << endl; 
     cout << "Press 1 to manage the grocery list." << endl; 
     cout << "Press 2 to manage the hardware store list." << endl; 
     cout << "Press 3 to manage the chore list." << endl; 
     cout << "Press 4 to exit." << endl; 

     cin >> menuInput; 

     if (menuInput == 4) 
     { 
      system("CLS"); 
      cout << "Now exiting program." << endl; 
      exitProgram = 2; 
      break; 
     } 

     while (menuInput == 1) 
     { 
      system("CLS"); 

      int groceryMenu = 0; 

      cout << "Press 1 to read the grocery list." << endl; 
      cout << "Press 2 to add an item to the list." << endl; 
      cout << "Press 3 to delete an item from the list." << endl; 
      cout << "Press 4 to return to the main menu." << endl; 

      cin >> groceryMenu; 

      if (groceryMenu == 1) 
      { 
       system("CLS"); 

       for (string groceryList : groceryVector) 
       { 
        cout << groceryList << endl; 
       } 

       system("PAUSE"); 
      } 

      if (groceryMenu == 2) 
      { 
       string groceryItem; 

       system("CLS"); 

       cout << "Enter what you would like to add to the grocery list: " << endl; 
       getline(cin, groceryItem); 

       groceryVector.push_back(groceryItem); 

      } 

      if (groceryMenu == 3) 
      { 
       int eraseLine = 0; 

       system("CLS"); 
       cout << "What line would you like to erase from the list?" << endl; 
       cin >> eraseLine; 

       groceryVector.erase(groceryVector.begin() + (eraseLine - 1)); 
      } 

      outputFile.open("grocery.txt"); 

      for (string groceryList : groceryVector) 
      { 
       outputFile << groceryList << endl; 
      } 
      outputFile.close(); 

      if (groceryMenu == 4) 
      { 
       menuInput = 0; 
      } 
     } 

     while (menuInput == 2) 
     { 
      system("CLS"); 

      int hardwareMenu = 0; 

      cout << "Press 1 to read the hardware list." << endl; 
      cout << "Press 2 to add an item to the list." << endl; 
      cout << "Press 3 to delete an item from the list." << endl; 
      cout << "Press 4 to return to the main menu." << endl; 

      cin >> hardwareMenu; 

      if (hardwareMenu == 1) 
      { 
       system("CLS"); 

       for (string hardwareList : hardwareVector) 
       { 
        cout << hardwareList << endl; 
       } 

       system("PAUSE"); 
      } 

      if (hardwareMenu == 2) 
      { 
       string hardwareItem; 

       system("CLS"); 

       cout << "Enter what you would like to add to the hardware list: " << endl; 
       getline(cin, hardwareItem); 

       hardwareVector.push_back(hardwareItem); 
      } 

      if (hardwareMenu == 3) 
      { 
       int eraseLine = 0; 

       system("CLS"); 
       cout << "What line would you like to erase from the list?" << endl; 
       cin >> eraseLine; 

       hardwareVector.erase(hardwareVector.begin() + (eraseLine - 1)); 
      } 

      outputFile.open("hardware.txt"); 

      for (string hardwareList : hardwareVector) 
      { 
       outputFile << hardwareList << endl; 
      } 
      outputFile.close(); 

      if (hardwareMenu == 4) 
      { 
       menuInput = 0; 
      } 
     } 

     while (menuInput == 3) 
     { 
      system("CLS"); 

      int choreMenu = 0; 

      cout << "Press 1 to read the chore list." << endl; 
      cout << "Press 2 to add an item to the list." << endl; 
      cout << "Press 3 to delete an item from the list." << endl; 
      cout << "Press 4 to return to the main menu." << endl; 

      cin >> choreMenu; 

      if (choreMenu == 1) 
      { 
       system("CLS"); 

       for (string choreList : choreVector) 
       { 
        cout << choreList << endl; 
       } 

       system("PAUSE"); 
      } 

      if (choreMenu == 2) 
      { 
       string choreItem; 

       system("CLS"); 

       cout << "Enter what you would like to add to the chore list: " << endl; 
       getline(cin, choreItem); 

       choreVector.push_back(choreItem); 
      } 

      if (choreMenu == 3) 
      { 
       int eraseLine = 0; 

       system("CLS"); 
       cout << "What line would you like to erase from the list?" << endl; 
       cin >> eraseLine; 

       choreVector.erase(choreVector.begin() + (eraseLine - 1)); 
      } 

      outputFile.open("chore.txt"); 

      for (string choreList : choreVector) 
      { 
       outputFile << choreList << endl; 
      } 
      outputFile.close(); 

      if (choreMenu == 4) 
      { 
       menuInput = 0; 
      } 
     } 
    } 

    return 0; 
} 

回答

2

既然你做這只是讀取您的menuInput號碼,但保持了新的生產線\n仍然在緩衝區中。當你致電getline它會返回你空行,因爲有\n仍在等待。當你混合getline使用getline之前,您應該清除輸入緩衝區:

cout << "Enter what you would like to add to the grocery list: " << endl; 
cin.ignore(); // <<== you need this! 
getline(cin, groceryItem); 

你需要將它添加到每個從std::cin讀你getline電話。 這應該解決您的問題。

除此之外,一些建議:

  • 不使用system("cls")system("pause"),嘗試將其刪除,但隨後你會控制檯尚不清楚。
  • 將你的代碼重構成單獨的函數,不要把所有東西都填入main()。例如,在基於menuInput的之後,可以調用以下功能之一:manageGroceries(...)manageHardware(...)manageChores(...)
0
  1. 您的問題描述很差,所以我會猜
  2. 最大的問題是,你把一切都放在main功能。將它分解爲更小的函數,這樣可以重複使用大量的代碼,並且最終的代碼會小3倍。注意「雜貨店」,「硬件」和「家務」正在做同樣的事情。唯一的區別是美容和可以參數化。
  3. 不要使用system("cls")它是寡婦特定的,不會做你期望的(它只會給你這種感覺)。這是不理解標準輸入/輸出和控制檯之間有什麼區別的結果。

我猜你已經輸入無效值什麼樣的標準輸入狀態改變狀態,它無法讀取數據。

使用cin.clear()清除輸入上的錯誤狀態,並清除(忽略)在控制檯緩衝區中等待的所有數據,因此在cin上的下一個讀取操作將讀取剛剛提供到控制檯中的數據。