使用getline的问题

时间:2017-05-06 19:49:43

标签: c++ visual-studio

我正在研究我的c ++类简介的最终项目,我的代码存在问题。 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线显示在屏幕上(它只是闪烁,但在你看到它停留后有一个系统(“PAUSE”)),但随后它退出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 个答案:

答案 0 :(得分:2)

由于您执行cin >> menuInput,因此只需读入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);

您需要将其添加到从getline读取的每个std::cin来电。 这应该可以解决你的问题。

除此之外,还有一些建议:

  • 请勿使用system("cls")system("pause"),尝试将其删除,但您的控制台无法清除。
  • 将您的代码重构为单独的函数,不要将所有内容都包含在main()中。例如,在基于cin >> menuInput的{​​{1}}之后,您可以调用其中一项功能:menuInputmanageGroceries(...)manageHardware(...)

答案 1 :(得分:0)

  1. 你的问题描述很差,所以我会猜测
  2. 最大的问题是你把所有东西放在main函数中。将其拆分为较小的函数,这样可以重用大量代码,最终代码将缩小3倍。注意&#34;杂货&#34;,&#34;硬件&#34;和&#34;家务&#34;正在做同样的事情。唯一的区别是装饰性的,可以参数化。
  3. 不要使用system("cls")它是寡妇特有的,并没有做你期望的事情(它只会产生这种感觉)。这是因为不了解标准输入/输出和控制台之间的区别。
  4. 我猜测你输入了无效值,将标准输入的状态改变为无法读取数据的状态。

    使用cin.clear()清除输入上的错误状态,cin.synch()清除(忽略)在控制台缓冲区中等待的所有数据,因此cin上的下一次读取操作将读取新提供的数据控制台。