忽略空格在C中使用getline

参见英文答案 > Need help with getline()                                    7个
嘿,我正在尝试编写一个程序,接受人们的新任务,将其添加到堆栈,能够显示任务,能够将该堆栈保存到文本文件,然后读取文本文件.当我尝试接受来自用户的输入时,每当你输入一个带有空格的字符串时,问题就出现了,菜单选择只做循环.我需要一种方法来解决这个问题.任何帮助将不胜感激.

// 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;
        }
    }
}

解决方法:

您必须在选择选项后立即添加cin.ignore():

//get the input from the user
cin >> option;
cin.ignore();

在你的getline之后没有必要使用cin.ignore():

    getline(cin, task); // THIS IS WHERE THE ISSUE COMES IN
        //cin.ignore();

问题在于选项 – 如果你之后没有调用cin.ignore(),选项将包含行尾,循环将继续…

我希望这有帮助.

上一篇:IIS中SSL Settings的三个选项(Ignore/Accept/Require)的区别


下一篇:git 忽略不提交的文件