c++ - 使用 Eclipse : Bug 在 C++ 中猜谜游戏

标签 c++ eclipse

您好,我正在 Eclipse IDE 中使用 C++ 实现一个非常基本的随机猜谜游戏。

除检查用户输入是否为 0 到 9 的一个条件外,一切正常,如果不是,则应显示消息。

有什么问题请检查我的代码:

#include <iostream>
#include <stdlib.h>
#include <time.h>

using namespace std;

int main(){

    puts ("\n\n\t\t***** Welcome to the guessing game *****");
    puts ("\n\t You've eight tries to guess a randomly generated number");
    puts ("\n\t\t\tNumber is within 1 to 9");
    puts ("\n\t -------------------------------------------------------");
    puts ("\n\t -------------------------------------------------------");

    int j;
    char l;

    reverse:
    srand (time(NULL));                         // required to keep updating randomly generated number with time
    int i = rand() % 10;                        // generate random number from 0 to (10 - 1)

    for (int k = 0; k < 8; k++){                // for the eight tries provided to user

        again:
        cout << "\n\r\t guess any number : ";
        cin >> j;                               // input by the user

        if(isdigit(j) == false){                // check whether the input no. is indeed a no. or not?
            cout << "\n\r\t Please Enter Numbers between 0 and 9 only" << endl;
            goto again;                         // if no then goto again:
        }

        else if (j == i){
            cout << "\n\r\t congratulation, You win \a";
            puts ("\n\t -------------------------------------------------------");
            cout << "\n\t Press any key to start the game again or 0 to Exit";
            cin >> l;
            if(l != '0') goto reverse;      // start the game again from reverse:
            goto target;
        }

        else if (k == 7){
            cout << "\n\r\t Sorry you lose! \a";
            puts ("\n\t -------------------------------------------------------");
            cout << "\n\t Press any key to start the game again or 0 to Exit";
            cin >> l;
            if(l != '0') goto reverse;          // start the game again from reverse:
            goto target;
        }

        else {                                  // evaluate remaining chances and start over
            cout << endl << "\r\t incorrect! " << 7-k << " chances remaining" << endl;
        }
    }

    target:
    puts ("\n\t -------------------------------------------------------");
    cout << "\n\n\n\r\t\t\t * GAME OVER * \a";  // console will get close
//  while(1);                                   // console will remain open
    return 0;
}

我已经检查过这些: C number guessing game with isdigit() verification Random guessing game - bug http://www.cplusplus.com/reference/cctype/isdigit/

请帮忙

最佳答案

j 是一个intisdigit 适用于char。注意变量的类型非常重要。

你想要的其实很简单

if (j < 0 || j > 9){
    cout << "\n\r\t Please Enter Numbers between 0 and 9 only" << endl;

顺便说一句,不要使用 goto,否则你会失去分数。再写一个循环,循环直到输入有效。

关于c++ - 使用 Eclipse : Bug 在 C++ 中猜谜游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19240382/

相关文章:

c++ - 如何使用 SDL2 获取屏幕分辨率?

c++ - _mm_cvtsd_f64 模拟高阶 float

c++ - 复制构造函数调用 extra

c - Eclipse CDT - C 项目

eclipse - "same"项目的两个不同符号

java - 查找所有单行循环和 If 并添加括号

c++ - 创建项目,项目创建失败visual studio

c++ - 将 lambda 函数传递给通用函数 C++

android - 如何在不取消/分配内存的情况下使用 ImageView

eclipse - 如何设置gdb调试器连接?