忽略空间在C ++中使用getline [英] Ignore Spaces Using getline in C++
问题描述
嘿,我想写一个程序,从人接受新任务,将其添加到堆栈,能够显示任务,能够将该堆栈保存到文本文件,然后阅读文本文件。问题出现时,我试图接受用户的输入,每当你输入一个字符串,它有一个空格,菜单选择做什么只是循环。我需要一种方法来解决这个问题。任何帮助将非常感激。
//基本文件io操作
#include< iostream&
#include< fstream>
#include< stack>
#include< string>
using namespace std;
int main(){
//声明堆栈
stack< string>列表;
//为菜单开始循环
string inputLine;
cout<< 欢迎来到待办事项列表! << endl;
//尝试读取文件
ifstream myfile(to-do.txt);
if(myfile.is_open()){
//读取待处理列表的每一行并将其添加到堆栈
while(myfile.good()) {
getline(myfile,inputLine);
list.push(inputLine);
}
myfile.close();
cout<< 文件读取成功! << endl;
} else {
cout<< 没有文件要加载...创建一个空白堆栈。 << endl;
}
int选项;
//而我们不想退出
while(true){
//显示程序的选项
cout< endl 你想干什么? << endl;
cout<< 1.查看堆栈上当前的任务。 << endl;
cout<< 2.删除堆栈中的顶层任务。 << endl;
cout<< 3.向堆栈添加一个新任务。 << endl;
cout<< 4.将当前任务保存到文件。 << endl;
cout<< 5.退出。 << endl endl;
//从用户获取输入
cin>>选项;
//使用该选项执行必要的任务
if(option< 6&& option> 0){
if(option == 1){
//创建一个缓冲区列表以显示所有
stack< string> buff = list;
cout< endl;
//打印出栈
while(!buff.empty()){
cout< buff.top()<< endl;
buff.pop();
}
} else if(option == 2){
list.pop();
} else if(option == 3){
//使一个字符串保存输入
string task;
cout< endl 输入您要添加的任务:< endl;
getline(cin,task); //这是问题发生在
cin.ignore();
//添加字符串
list.push(task);
cout< endl;
} else if(option == 4){
//将堆栈写入文件
stack< string> buff = list;
ofstream myfile(to-do.txt);
if(myfile.is_open()){
while(!buff.empty()){
myfile< buff.top();
buff.pop();
if(!buff.empty()){
myfile<< endl;
}
}
}
myfile.close();
} else {
cout<< 谢谢,再见! << endl;
break;
}
} else {
cout< 输入正确的数字! << endl;
}
}
}
您必须在选择选项后添加 cin.ignore()
:
//从用户获取输入
cin>>选项;
cin.ignore();
并且 cin.ignore()
必须在 getline
后:
getline //这是问题发生在
//cin.ignore();
问题出现在 options
没有调用 cin.ignore()
之后,选项将包含行尾,循环将继续...
我希望这有助于。
Hey, I'm trying to write a program that will accept new tasks from people, add it to a stack, be able to display the task, be able to save that stack to a text file, and then read the text file. The issue comes when I am trying to accept input from the user, whenever you enter a string with a space in it, the menu to choose what to do just loops. I need a way to fix this. Any help would be greatly appreciated.
// basic file io operations
#include <iostream>
#include <fstream>
#include <stack>
#include <string>
using namespace std;
int main () {
//Declare the stack
stack<string> list;
//Begin the loop for the menu
string inputLine;
cout << "Welcome to the to-do list!" << endl;
//Trying to read the file
ifstream myfile ("to-do.txt");
if(myfile.is_open()){
//read every line of the to-do list and add it to the stack
while(myfile.good()){
getline(myfile,inputLine);
list.push(inputLine);
}
myfile.close();
cout << "File read successfully!" << endl;
} else {
cout << "There was no file to load... creating a blank stack." << endl;
}
int option;
//while we dont want to quit
while(true){
//display the options for the program
cout << endl << "What would you like to do?" << endl;
cout << "1. View the current tasks on the stack." << endl;
cout << "2. Remove the top task in the stack." << endl;
cout << "3. Add a new task to the stack." << endl;
cout << "4. Save the current task to a file." << endl;
cout << "5. Exit." << endl << endl;
//get the input from the user
cin >> option;
//use the option to do the necessary task
if(option < 6 && option > 0){
if(option == 1){
//create a buffer list to display all
stack<string> buff = list;
cout << endl;
//print out the stack
while(!buff.empty()){
cout << buff.top() << endl;
buff.pop();
}
}else if (option == 2){
list.pop();
}else if (option == 3){
//make a string to hold the input
string task;
cout << endl << "Enter the task that you would like to add:" << endl;
getline(cin, task); // THIS IS WHERE THE ISSUE COMES IN
cin.ignore();
//add the string
list.push(task);
cout << endl;
}else if (option == 4){
//write the stack to the file
stack<string> buff = list;
ofstream myfile ("to-do.txt");
if (myfile.is_open()){
while(!buff.empty()){
myfile << buff.top();
buff.pop();
if(!buff.empty()){
myfile << endl;
}
}
}
myfile.close();
}else{
cout << "Thank you! And Goodbye!" << endl;
break;
}
} else {
cout << "Enter a proper number!" << endl;
}
}
}
You have to add cin.ignore()
right after options is chosen:
//get the input from the user
cin >> option;
cin.ignore();
And cin.ignore()
is not necessary after your getline
:
getline(cin, task); // THIS IS WHERE THE ISSUE COMES IN
//cin.ignore();
The problem is in options
- if you didn't call cin.ignore()
after it, options will contain end of line and loop will continue...
I hope this helps.
这篇关于忽略空间在C ++中使用getline的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!