首页 > 解决方案 > 即使它是真正的 C++,我的 while 循环也不会开始

问题描述

我的代码不想阅读评论之间的内容,只是跳过它,即使电话和 snn 设置为小于代码内部

int num = 1;

 while (num > 0 && num < 2) {
    num = 1;

    string first = "chicken";
    string last = "salad";
    string full = "chicken salad";
    int phone = 1;
    int snn = 1;

    cout << "enter your first name: ";
    cin >> first;
    cout << "enter your last name: ";
    cin >> last;
    full = first + " " + last;
    cout << phone;
    // works all the way to here then doesnt read the while
    
    while (phone < 1000000000 && phone > 9999999999) {
            cout << "enter 10 digit phone nmber (with no dashes or parenthesis): ";
            cin >> phone;
        
    }
    while (snn < 100000000 && snn > 999999999) {
        cout << "enter 9 digit snn <with no dashes>: ";
        cin >> snn;

    }
// starts working again here
    cout << "\n\tT H A N K  Y O U\n\n";
    system("pause");
    return 0;
}

标签: c++while-loop

解决方案


在两个 while 循环中,您都有一个&&代替||,这意味着您要求phone同时小于 1000000000 大于 9999999999,这将始终返回false


推荐阅读