如果 else 语句不是 运行 C++

If else statement not running C++

我想做一个简单的猜数字游戏,当答案大于数字时,if else 语句不会触发。 例如,如果数字是 20,我选择 30 以上的数字,比如 41,它就会结束代码。

#include <iostream>
#include <string>
#include <cmath>
#include <unistd.h>
#include <chrono>
#include <thread>
#include <fstream>
#include <random>
#include <time.h>
#include "functions.h"

using namespace std;

void game() {
  int number;
  int answer;
  cout << "Welcome to the guess the number game!" << endl;
  cout << "The computer will generate a random number from 1 to 100 and you will try to guess it."<< endl;
  cont();
  ran(number,100,1);
  START:
  cout << number;
  system("clear");
  cout << "Type your answer here: ";
  if (!(cin >> answer)) {
    cin.clear();
    cin.ignore(10000,'\n');
    cout << "THAT IS NOT AN OPTION!" << endl;
    sleepcp(250);
    system("clear");
    goto START;
  }
  int warmer1;
  int warmer2;
  warmer1 = number + 11;
  warmer2 = number - 11;
  if (answer > warmer2) {
    if (answer == number) {
      cout << "You got it!";
    } else if (answer < warmer1) {
      cout << "You are close."<< endl;
      sleepcp(250);
      system("clear");
      goto START;
    }
  } else if (answer > number) {

    cout << "Less." << endl;
    sleepcp(250);
    system("clear");
    goto START;
  } else if (answer < number) {
    cout << "More."<< endl;
    sleepcp(250);
    system("clear");
    goto START;
  }
   
}  

int main() {
  game();
  return 0;
}

谁能帮忙解决一下谢谢!!!

当 number 等于 20 且 answer 等于 41 时考虑这个 if 语句。

  if (answer > warmer2) {
    if (answer == number) {
      cout << "You got it!";
    } else if (answer < warmer1) {
      cout << "You are close."<< endl;
      sleepcp(250);
      system("clear");
      goto START;
    }
  } else if (answer > number) {

在这种情况下,if 语句获得控制权,因为 answer 大于 warmer2。但是 answer 不等于 number(第一个内部 if 语句)并且 answer 不小于 warmer1.

在这种情况下,什么也不会发生,控制被传递到程序的末尾。

也就是if这个if语句

  if (answer > warmer2) {

获取控制权,然后是以下 if 语句,例如 this

  } else if (answer > number) {

将被跳过。

换句话说,你所做的就是你得到的。

如果您使用 while 或 do-while 循环而不是 goto 语句,您可以解决您的问题。

如果您要使用调试器单步执行代码(或至少打印出一些描述游戏决策的诊断消息),您就会明白程序终止的确切原因。

假设number20answer41,那么warmer131warmer29,因此:

void game() {
  ...
  START:
  ...
  if (answer > warmer2) { // 41 > 9 is TRUE
    if (answer == number) { // 41 == 20 is FALSE
      ...
    } else if (answer < warmer1) { // 41 < 31 is FALSE
      ...
      goto START; // <-- NOT REACHED!
    }
    // <-- execution reaches here!
  } else if (answer > number) { // <-- NOT EVALUATED!
    ...
    goto START; // <-- NOT REACHED!
  } else if (answer < number) { // <-- NOT EVALUATED!
    ...
    goto START; // <-- NOT REACHED!
  }
  // <-- execution reaches here!   
}

由于game()函数没有到达goto START;语句,循环结束,game()退出,因此main()退出,程序终止。

在现代 C++ 编码中几乎没有理由使用 goto。请改用 whiledo..while 循环,例如:

#include <iostream>
#include <limits>
#include "functions.h"

using namespace std;

void game() {
    int number, answer, warmer1, warmer2;
    cout << "Welcome to the guess the number game!" << endl;
    cout << "The computer will generate a random number from 1 to 100 and you will try to guess it." << endl;
    cont();
    ran(number, 100, 1);
    warmer1 = number + 11;
    warmer2 = number - 11;
    do {
        system("clear");
        cout << "Type your answer here: ";
        if (!(cin >> answer)) {
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
            cout << "THAT IS NOT AN OPTION!" << endl;
        }
        else if (answer == number) {
            cout << "You got it!";
            break;
        }
        else {
            if (answer <= warmer1 && answer >= warmer2) {
                cout << "You are close." << endl;
            }
            if (answer > number) {
                cout << "Less." << endl;
            }
            else {
                cout << "More."<< endl;
            }
        }
        sleepcp(250);
    }
    while (true);
}  

int main() {
    game();
    return 0;
}