gpt4 book ai didi

c++ - 为什么我的 C++ 控制台在接受用户输入时会关闭?

转载 作者:行者123 更新时间:2023-11-30 03:24:52 26 4
gpt4 key购买 nike

我正在学习 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,<br/>
How are you? It has been a long time since we spoke. Have you seen lately?<br/>
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();。干杯!

关于c++ - 为什么我的 C++ 控制台在接受用户输入时会关闭?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49397805/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com