【问题标题】:my while loop wont begin even though its true C++即使它是真正的 C++,我的 while 循环也不会开始
【发布时间】:2021-04-17 18:12:02
【问题描述】:

我的代码不想读取 cmets 之间的内容,只是跳过它,即使 phone 和 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;
}

【问题讨论】:

  • phone 被初始化为1,使phone &gt; 9999999999 始终为假。 snn 也一样。你是说还是 (||)?
  • 如果您在调试器中逐步解决问题,您会在一秒钟内自己发现问题。
  • > 9999999999是多少?你需要|| 代替&amp;&amp;
  • phone 怎么可能是 和 > 9999999999?
  • 我完全忘记了 ||命令谢谢你我习惯在数字里面而不是在外面

标签: c++ while-loop


【解决方案1】:

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

【讨论】:

  • 另请注意,9999999999 可能大于 INT_MAX(假设为 32 位整数)。
猜你喜欢
  • 1970-01-01
  • 2020-01-31
  • 1970-01-01
  • 1970-01-01
  • 2018-05-30
  • 2022-01-20
  • 1970-01-01
  • 2015-09-19
  • 2016-03-14
相关资源
最近更新 更多