ç猜数字与ISDIGIT游戏()验证 [英] C number guessing game with isdigit() verification

查看:153
本文介绍了ç猜数字与ISDIGIT游戏()验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从我的课本是一个挑战问题工作的地方,我应该产生一个随机数1-10之间,让用户猜测,并验证其与ISDIGIT反应()。我(大部分)得到了程序与下面的code工作。

我遇到的主要问题是使用ISDIGIT()所需的输入存储为一个字符,然后我有这么实际的数量相比,比较之前转换,而不是ASCII code为数。

所以我的问题是,因为这种转换只适用于数字0 - 9,我怎样才能改变code,以便成功地猜测10的用户时的生成的号码?或者,如果我想游戏的范围是1-100是什么 - 我将如何再做到这一点?如果我使用的是可能的范围比0-9更大的我不能验证与ISDIGIT()输入?什么是更好的方式来验证用户输入?

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&文件ctype.h GT;
#包括LT&;&time.h中GT;诠释主要(无效){  炭缓冲液[10];
  焦炭cGuess;
  焦炭INUM;
  函数srand(时间(NULL));  INUM =(RAND()%10)+ 1;  的printf(%d个\\ N,INUM);
  的printf(请输入你的猜测:);
  与fgets(缓冲区,缓冲区尺寸,标准输入);
  sscanf的(缓冲,%C,&安培; cGuess);  如果(ISDIGIT(cGuess))
  {
    cGuess = cGuess - '0';    如果(cGuess == INUM)
      的printf(你猜对了!);
    其他
    {
      如果(cGuess大于0&放大器;&放大器; cGuess&下; 11)
        的printf(你猜错了。);
      其他
        的printf(你没有输入正确的数字。);
    }
  }
  其他
    的printf(你没有输入正确的数字。);
返回(0);
}


解决方案

您可以使用 scanf函数的返回值来确定读取是否成功。因此,也有你的程序两条路径,读取成功和失败的阅读:

  INT的猜测;
如果(scanf函数(%d个,&安培;猜)== 1)
{
    / *猜读* /
}
其他
{
    / *的猜测是不读* /
}

在第一种情况下,你做任何你的程序逻辑说。在其他情况下,你要搞清楚什么是问题和怎么办吧

  INT的猜测;
如果(scanf函数(%d个,&安培;猜)== 1)
{
    / *猜读* /
}
其他
{
    如果(的feof(标准输入)|| FERROR(标准输入))
    {
        fprintf中(标准错误,输入意外结束或I / O错误。\\ n);
        返回EXIT_FAILURE;
    }
    / *如果没有文件错误,则输入是不是一个数字* /
    / *让我们跳过当前行。 * /
    而(的feof(标准输入)及!&安培;龟etc(标准输入)='\\ n'!);
}

I'm working on a challenge problem from my textbook where I'm supposed to generate a random number between 1-10, let the user guess, and validate their response with isdigit(). I (mostly) got the program to work with the code below.

The main issue I ran into is that using isdigit() required the input to be stored as a char, which I then had to convert before the comparison so the actual number was compared and not the ASCII code for the number.

So my question is, since this conversion only works for numbers 0 - 9, how can I change the code to allow for the user to successfully guess 10 when that's the number that is generated? Or what if I wanted the game to have a range of 1-100 - how would I then accomplish this? Can I not verify the input with isdigit() if I'm using a possible range greater than 0-9? What is a better way to verify user input?

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>

int main(void) {

  char buffer[10];
  char cGuess;
  char iNum;
  srand(time(NULL));

  iNum = (rand() % 10) + 1;

  printf("%d\n", iNum);
  printf("Please enter your guess: ");
  fgets(buffer, sizeof(buffer), stdin);
  sscanf(buffer, "%c", &cGuess);

  if (isdigit(cGuess)) 
  {
    cGuess = cGuess - '0';

    if (cGuess == iNum)
      printf("You guessed correctly!");
    else
    {
      if (cGuess > 0 && cGuess < 11)
        printf("You guessed wrong.");
      else
        printf("You did not enter a valid number.");
    }
  }
  else
    printf("You did not enter a correct number.");




return(0);
}

解决方案

You can use the return value of scanf to determine whether the read was successful. So, there are two paths in your program, successful reading and failed reading:

int guess;
if (scanf("%d", &guess) == 1)
{
    /* guess is read */
}
else
{
    /* guess is not read */
}

In the first case, you do whatever your program logic says. In the else case, you have to figure out "what was the problem", and "what to do about it":

int guess;
if (scanf("%d", &guess) == 1)
{
    /* guess is read */
}
else
{
    if (feof(stdin) || ferror(stdin))
    {
        fprintf(stderr, "Unexpected end of input or I/O error\n");
        return EXIT_FAILURE;
    }
    /* if not file error, then the input wasn't a number */
    /* let's skip the current line. */
    while (!feof(stdin) && fgetc(stdin) != '\n');
}

这篇关于ç猜数字与ISDIGIT游戏()验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆