为什么我的输入在这个C++程序中如此混乱
Why is my input so messed up in this C++ program?
我正在尝试制作一个适用于Linux的命令行邮件应用程序(尽管我是在Xcode上开发的,因为虚拟机会占用我的计算机大量的电源(。我在主菜单中有两个选项(用户输入一个数字进行选择(。我在每次输入后都使用这个,无论是简单的can还是get行(can,stringName(:
void clearCin() {
cin.clear();
cin.ignore(INT_MAX, 'n');
}
这就是为什么我感到困惑,因为我的输入中出现了各种奇怪的行为。这是我的代码和一个示例输出(结束函数只是一个for循环,它的成本为<<endl;(:
代码
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;
}
输出
--------------------------------------------------
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
以下是newMessage.txt中显示的内容:
这是的示例消息
cin.ignore(INT_MAX, 'n');
丢弃换行符,getline在读取换行符时完成,因此您必须在键入消息后按enter两次才能让getline读取它,并且您必须在输入"后按enter二次结束你的循环。
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- C++,系统无法执行指定的程序
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- C++ Windows 驱动程序MSB3030无法复制该文件,因为它找不到
- 重载操作程序时出错>>用于类中的字符串 memebr
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- 试图在visual studio上用C++创建一个桌面应用程序
- 模板元程序查找相似的连续类型名称
- FFmpeg:制作一个应用程序比直接使用ffmepg更好吗
- 如何通过cpp程序运行shell脚本
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用
- IPC使用多个管道和分支进程来运行Python程序
- 如何将c++程序的一些输出传递给shell,以便在shell中使用
- 为什么这个C++程序的输出在cmd中产生了巨大的混乱
- 如何在等待程序启动混乱/ GTK+时制作加载/介绍动画弹出窗口
- 程序来创建不同大小的图案,但存在一些混乱
- C++BEGINNER阶乘程序,输出混乱
- Arduino程序读取回混乱的数据
- 运行护士程序后终端混乱