gpt4 book ai didi

c++ - 使用 getline 的问题

转载 作者:搜寻专家 更新时间:2023-10-31 02:11:18 24 4
gpt4 key购买 nike

我正在为我的 C++ 类(class)介绍做期末项目,但我的代码有问题。 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 行显示在屏幕上(它只是闪烁,但在您看到它停留后带有一个 system("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;
}

最佳答案

由于您执行了 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);

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

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

  • 不要使用 system("cls")system("pause"),尝试将其删除,但您的控制台将无法清除。
  • 将您的代码重构为单独的函数,不要将所有内容都塞入 main() 中。例如,在基于 menuInputcin >> menuInput 之后,您可以调用其中一个函数:manageGroceries(...), manageHardware(...), manageChores(...)

关于c++ - 使用 getline 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43824752/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com