繁体   English   中英

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

[英]Why does my C++ console shut down when its about to take user input?

我正在阅读C ++的PDF教程,目前正在做一个简单的Letter程序的练习。 该程序接受我的输入,但是当涉及第二个输入时,它只是关闭了。 我是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

您只是在该行中有一个错别字: else if (dest_age = 0 || dest_age >= 110)应该是else if (dest_age == 0 || dest_age >= 110)

我建议您下次使用调试器。

可能会cin.sync();帮助,请尝试同时添加cin.sync(); cin.clear(); 在询问输入之后。 干杯!

暂无
暂无

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

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