为什么我的C++控制台在即将接受用户输入时关闭
Why does my C++ console shut down when its about to take user input?
我正在为C++学习PDF教程,目前正在为一个简单的字母程序进行练习。 该程序接受我的输入,但是当涉及到第二个输入时,它就会关闭。 我对C++很陌生,所以请原谅我的无知。 这是我的代码:
#include<iostream>
#include<string>
using namespace std;
int main() {
string first_name, last_name, dest_firstName, friend_Name;
char friend_sex = '0';
int dest_age = 0;
cout << "Enter your first and Last Names" << endl;
cin >> first_name;
cin >> last_name;
cout << "Hello " << first_name << " " << last_name
<< ". Enter the name of the person you want to write to. " << endl;
cin >> dest_firstName;
cout << "Enter their age" << endl;
cin >> dest_age;
cout << "Enter the name of another friend." << endl;
cin >> friend_Name;
cout << "Enter the gender of the friend." << endl;
cin >> friend_sex;
cout << "Dear " << dest_firstName << ", " <<endl;
cout << "How are you? It has been a long time since we spoke. Have you seen "
<< friend_Name << " lately? "<< endl;
if (friend_sex == 'm') {
cout << "If you see " << friend_Name << ", can you ask him to call me?" << endl;
}
else if (friend_sex == 'f') {
cout << "If you see " << friend_Name << ", can you ask her to call me?" << endl;
}
else if (dest_age = 0 || dest_age >= 110) {
cout << "Also, I've heard that not long ago was your birthday and you are "
<< dest_age << " years old. NO WAY" << endl;
}
else if (dest_age < 12) {
cout << "Also, I've heard that not long ago was your birthday and you are "
<< dest_age << ". Next year you will be "
<< dest_age + 1 << " years old." << endl;
}
else if (dest_age == 17) {
cout << "Also, I've heard that not long ago was your birthday and you are "
<< dest_age << ". Next year you will be able to vote. " << endl;
}
else if (dest_age == 70) {
cout << "Also, I've heard that not long ago was your birthday and you are "
<< dest_age << ". I hope you're enjoying retirement." << endl;
}
cout << "Yours Truly" << endl;
cout << first_name << " " << last_name << endl;
return 0;
}
这是输出:
Dear John,
How are you? It has been a long time since we spoke. Have you seen lately?
Also, I've heard that not long ago was your birthday and you are 0. Next year you will be 1 years old.
Yours Truly
Onur Ozbek
可能会
有所帮助,请尝试在询问输入后添加cin.sync();
和cin.clear();
。干杯!
你只是在那行有一个错别字:else if (dest_age = 0 || dest_age >= 110)
应该是else if (dest_age == 0 || dest_age >= 110)
.
我建议您下次使用调试器。
相关文章:
- C++如何通过用户输入删除列表元素
- 如何使用用户输入在C++中正确填充2D数组
- C++MySQL C api用户输入行
- 如何只允许用户输入正整数
- C++如何计算用户输入的数字中的偶数位数
- 通过for循环使用用户输入填充列表
- 在C++中,我如何接受不同于同一行的用户输入
- 如果用户输入无效,如何使用字符串变量-C++重复输入命令
- 显示基于用户输入的整数的字符
- 根据用户输入用字母填充矢量,并将"开始"和"结束"放在四肢
- 我试图制作一个程序,要求用户输入问题和答案,但程序循环不正确
- 打印时有二叉树问题.用户输入不打印任何内容
- C ++奇怪的数组由用户输入
- 如何允许大写和小写用户输入?
- C++读取用户输入而不按回车键(Mac OS X),与Turbo Pascal中的读取键相同
- 如何存储用户输入的所有数据,然后在他们想要查看所有数据时显示它们
- 如何使用用户输入变量制作二维数组?
- 用户输入字符串的文件附加问题..C++
- 遇到此问题时遇到困难:允许用户输入数组的值并使用 for,而循环也输出输入的最大数字
- 如何使用递归循环我的代码(当用户输入无效输入时,它会再次提示他们)?