简体   繁体   English

为什么我的输入在这个 C++ 程序中如此混乱?

[英]Why is my input so messed up in this C++ program?

I am trying to make a command line mail application for Linux (though I'm developing on Xcode cause a VM uses a lot of power for my computer).我正在尝试为 Linux 制作一个命令行邮件应用程序(尽管我正在 Xcode 上开发,因为 VM 为我的计算机使用了大量电源)。 I have two options in the main menu (where the user inputs a number to select).我在主菜单中有两个选项(用户输入一个数字来选择)。 I use this after every input whether a simple can or a get line(can, stringName):无论是简单的罐头还是get line(can,stringName),我在每次输入后都使用它:

void clearCin() {
    cin.clear();
    cin.ignore(INT_MAX, '\n');
}

That is why I am perplexed that I am getting all sorts of weird behavior with my input.这就是为什么我很困惑我的输入会出现各种奇怪的行为。 Here's my code and a sample output (the end function is just a for loop that does cost << endl;):这是我的代码和示例 output (结尾 function 只是一个成本 << endl; 的 for 循环):

Code代码

bool stdEmail() {

    string to, cc, bcc, subject, message;

    cout << "When entering email addresses, seperate multiple email addresses with a space";
    endl(2);

    cout << "To: ";
    getline(cin, to);
    clearCin();

    cout << "cc: ";
    getline(cin, cc);
    clearCin();

    cout << "bcc: ";
    getline(cin, bcc);
    clearCin();
    endl(1);

    cout << "Subject: ";
    getline(cin, subject);
    clearCin();
    endl(1);

    cout << "Now enter your message, when you're finished, type a period on a new line";
    endl(2);

    ofstream file;
    file.open("newMessage.txt", fstream::trunc);

    bool repeat = true;

    while (repeat) {
        getline(cin, message);
        clearCin();
        if (message == ".") {
            repeat = false;
        } else {
            file << message << endl;
        }
    }

    file.close();
    return true;
}

Output Output

--------------------------------------------------
Welcome to mark's Multi-Mail program

Main menu:
--------------------------------------------------
1. Send personalized emails to multiple recipients
2. Send a standard email
3. Exit the program
--------------------------------------------------
2

When entering email addresses, seperate multiple email addresses with a space

To: one@example.com two@example.com

cc: three@example.com

bcc: 


Subject: Thanks for your help!


Now enter your message, when you're finished, type a period on a new line

Here is a sample message
.

Now I am trying to get it out of taking the message which should have happened when I typed a .


.


Program ending Have a Nice Day

Program ended with exit code: 0

Here is what shows up in newMessage.txt:以下是 newMessage.txt 中显示的内容:

Here is a sample message这是一个示例消息

cin.ignore(INT_MAX, '\n'); discards a newline and getline completes when it reads a newline so you would have to press enter twice after typing your message for getline to read it and you would have to press enter twice after entering a '.'丢弃换行符,getline 在读取换行符时完成,因此您必须在输入消息后按两次回车键才能读取它,并且在输入“。”后必须按两次回车键。 to end your loop.结束你的循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM