2017-05-06 50 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上的下一个读取操作将读取刚刚提供到控制台中的数据。