使用getline的问题

Problems using getline

本文关键字:问题 getline 使用      更新时间:2023-10-16

我正在为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线在屏幕上显示(它只是闪烁,但是在您看到它停留后使用系统("暂停"((,但是随后它退出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;
}

由于您执行cin >> 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()中。例如,基于menuInputcin >> menuInput之后,您可以调用其中一个功能:manageGroceries(...)manageHardware(...)manageChores(...)
  1. 您的问题描述很差,所以我会猜测
  2. 最大的问题是将所有内容都放在main功能中。将其拆分为较小的功能,因此可以重复使用许多代码,并且最终代码将小3倍。请注意"杂货","硬件"answers"繁琐"也在做同样的事情。唯一的差异是化妆品,可以被参数化。
  3. 请勿使用system("cls")它是特定于寡妇,并且不做您期望的事情(只会给人以这种感觉(。这是不了解标准输入/输出和控制台之间有什么区别的结果。

我猜想您对输入输入无效的值,将标准输入的状态变为未能读取数据的状态。

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