使用getline的问题
Problems using getline
我正在为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()
中。例如,基于menuInput
的cin >> menuInput
之后,您可以调用其中一个功能:manageGroceries(...)
,manageHardware(...)
,manageChores(...)
。
- 您的问题描述很差,所以我会猜测
- 最大的问题是将所有内容都放在
main
功能中。将其拆分为较小的功能,因此可以重复使用许多代码,并且最终代码将小3倍。请注意"杂货","硬件"answers"繁琐"也在做同样的事情。唯一的差异是化妆品,可以被参数化。 - 请勿使用
system("cls")
它是特定于寡妇,并且不做您期望的事情(只会给人以这种感觉(。这是不了解标准输入/输出和控制台之间有什么区别的结果。
我猜想您对输入输入无效的值,将标准输入的状态变为未能读取数据的状态。
使用cin.clear()
清除输入上的错误状态和cin.synch()
以清理(忽略(控制台缓冲区中的所有数据,因此cin
上的下一个读取操作将读取新鲜提供到控制台的数据。
相关文章:
- 我在C++中使用 getline() 函数时遇到问题
- 使用getline读取.csv文件时出现问题
- 此C Getline函数的C 代码有什么问题
- 如何在 for 循环中解决潜在的 getline() 问题,数组的第一个索引没有显示?
- 使用getline的问题
- 找不到用户定义的 getline。自动扣款有问题?
- 我在使用 cin.getline() 从用户那里获取 3 个字符串并使用指针数组来存储它们时遇到问题
- C++将csv读取到向量中,getline()问题
- 有人能解释一下getline和char的问题吗
- 用弦乐库解析行时的std :: getline问题
- C++:ifstream getline 问题交叉编译到 OpenWRT 发行版中
- getline() 函数的问题
- 使用getline函数时出现问题
- 使用 getline() 的字符串输入问题
- 注意:(getline不是问题所在)C++getline()停止在用户定义的函数中工作,但在主函数中工作
- 使用 getline() 从文件中读取行时出现问题
- 关于getline(cin,string)的C++快速问题
- 涉及getline()和循环的C++问题
- 遇到getline、fstream、作用域不一致的问题
- Getline来自一个文件和另一个未知问题