简体   繁体   English

如果 else 语句未运行 C++

[英]If else statement not running C++

I am trying to make a simple guess the number game and when the answer is bigger than the number the if else statement does not trigger.我试图对数字游戏进行简单的猜测,当答案大于数字时,if else 语句不会触发。 For example if the number is 20 and I choose something above 30 say 41 it will just end the code.例如,如果数字是 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;
}

Can anybody help with this thanks!!!谁能帮忙解答一下谢谢!!!

Consider this if statement when number is equal to 20 and answer is equal to 41.当 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) {

In this case the if statement gets the control because answer is greater than warmer2.在这种情况下,if 语句获得控制权,因为 answer 大于warmer2。 But answer is not equal to number (the first inner if statement) and answer is not less than warmer1.但是 answer 不等于 number (第一个内部 if 语句)并且 answer 不小于warmer1。

In this case nothing occurs and the control is passed to the end of the program.在这种情况下,什么也没有发生,控制权被传递到程序的末尾。

That is if this if statement那就是如果这个 if 语句

  if (answer > warmer2) {

gets the control then the following if statements like for example this得到控制然后下面的 if 语句像这样

  } else if (answer > number) {

will be skipped.将被跳过。

In other words, what you do is what you get.换句话说,你所做的就是你得到的。

You could resolve your problem if instead of goto statements you used for example a while or do-while loop.如果您使用例如 while 或 do-while 循环而不是 goto 语句,则可以解决您的问题。

If you were to step through your code with a debugger (or at least print out some diagnostic messages describing your game's decisions), you would see exactly why the program is terminating.如果您使用调试器单步调试代码(或至少打印出一些描述游戏决策的诊断消息),您将确切了解程序终止的原因。

Assuming number is 20 and answer is 41 , then warmer1 is 31 and warmer2 is 9 , thus:假设number20并且answer41 ,那么warmer131并且warmer29 ,因此:

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!   
}

Since the game() function does not reach a goto START;由于game()函数没有到达goto START; statement, the loop ends and game() exits, and thus main() exits, terminating the program.语句,循环结束并且game()退出,因此main()退出,终止程序。

There is almost never a good reason to use goto in modern C++ coding.在现代 C++ 编码中几乎没有充分的理由使用goto Use a while or do..while loop instead, eg:改用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;
}

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

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