【问题标题】:In the following c program if condition is not working在以下 c 程序中,如果条件不起作用
【发布时间】:2015-10-05 13:33:43
【问题描述】:
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
#include <stdlib.h>
#include <time.h>

main()
{
    int iRandom;
    int iResponse;
    srand(time(0));

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

    printf("\nGuess a number between 1 and 10: ");
    scanf("%c", &iResponse);

    if (isdigit(iResponse)) {

        iResponse = iResponse - '0';

        if (iResponse >=1 && iResponse <=10 ) {


            if (iResponse == iRandom) {
                printf("\nYou guessed right.\n");
            }
            else {
                printf("\nSorry, you guessed wrong.\n");
                printf("The correct guess was %d\n", iRandom);
            }
        }
        else {
            printf("You did not enter a digit between 1 and 10.");
        }

    }
    else {
        printf("\nYou did not enter a digit.\n");
    }

    getch();
    return 0;
}

如果 (iResponse >=1 && iResponse

【问题讨论】:

  • int iResponse; => char iResponse;
  • main() --> int main(void)

标签: c if-statement char


【解决方案1】:

问题出在这一行

 scanf("%c", &iResponse);

当您输入例如 47 作为输入时,它只需要 4 而不是 47 它只读取一个字符 '4' 。 “47”不是字符。因此,只要您输入某个数字,它就会始终进入该条件。但是,如果您尝试输入“a”作为输入,它会返回您

You did not enter a digit

您可以使用以下代码使其工作

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

main()
{
    int iRandom;
    int iResponse;
    srand(time(0));

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

    printf("\nGuess a number between 1 and 10: ");
    scanf("%d", &iResponse);

    if (isdigit(iResponse+'0') || iResponse==10) {



        if (iResponse >=1 && iResponse <=10 ) {


            if (iResponse == iRandom) {
                printf("\nYou guessed right.\n");
            }
            else {
                printf("\nSorry, you guessed wrong.\n");
                printf("The correct guess was %d\n", iRandom);
            }
        }
        else {
            printf("You did not enter a digit between 1 and 10.");
        }

    }
    else {
        printf("\nYou did not enter a digit.\n");
    }

    getch();
    return 0;
}

【讨论】:

  • 47 无法输入。提到输入应该在 1 到 10 之间。
  • 如果 (iResponse >=1 && iResponse
  • @MIRMIX 你解释得对。一些天才对你和我投了反对票。
【解决方案2】:

为了对您的程序有一个好的方法,我认为您想检查用户输入以确保它是您所期望的。 这意味着您需要检查输入是否仅包含数字。 试试这个:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define TRUE 1
#define RANDOM 10

void clean_stdin(void){
    int c;
    while((c = getchar()) != 0 && c != '\n');
}

int inputCheck(void){
    int number;
    char c;

    do{
        printf("\tPlease type a Number: ");
        if (scanf("%d%c", &number, &c) == 0 || c != '\n'){
            printf("\nLetter[s] was Found in your Answear\n\n\n");
            clean_stdin();
        }else{
            break;
        }
    }while(TRUE);

    return number;
}

int main(void){
    int iResponse;
    int iRandom ;
    srand ( (unsigned int)time(NULL) );

    iRandom = (rand() % 10 + 1);
    iResponse = inputCheck();

    if(iRandom == iResponse){
        printf("\nYou guessed right.\n");
    }else{
        printf( "\nSorry, you guessed wrong.\n" );
        printf( "\n\t\t\tThe correct guess was %u\n", iRandom );
    }

    return(0);
}

你会得到:

  Please type a Number: 3r
Letter[s] was Found in your Answear


    Please type a Number: G7

Letter[s] was Found in your Answear


    Please type a Number: 6

Sorry, you guessed wrong.

            The correct guess was 4
  Please type a Number: 4
You guessed right.

通过这种方法,您可以告知用户他/她的输入。

如果你想告诉用户这个数字在 1 到 10 之间,你可以这样做:

    #include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define TRUE 1
#define MIN_RANDOM 1
#define Max_RANDOM 10

void clean_stdin(void){
    int c;
    while((c = getchar()) != 0 && c != '\n');
}

int inputCheck(int min, int max){
    int number;
    char c;

    do{
    printf("\tPlease type a Number::\t");
    if (scanf("%d%c", &number, &c) == 0 || c != '\n'){
        clean_stdin();
        printf("\nLetter[s] was Found in your Answear\n\n\n");
    }else if(number < min || number > max){
        printf("The number has to be wetween %d and %d\n\n",min, max);
    }else{
        break;
    }
    }while(TRUE);

    return number;
}

int main(void){
    int iResponse;
    int iRandom ;
    srand ( (unsigned int)time(NULL) );

    iRandom = (rand() % Max_RANDOM + MIN_RANDOM);
    iResponse = inputCheck(MIN_RANDOM,Max_RANDOM);

    if(iRandom == iResponse){
    printf("\nYou guessed right.\n");
    }else{
    printf( "\nSorry, you guessed wrong.\n" );
    printf( "\n\t\t\tThe correct guess was %u\n", iRandom );
    }

    return(0);
}

输出将是这样的:

  Please type a Number::  A1
Letter[s] was Found in your Answear


    Please type a Number::  1A

Letter[s] was Found in your Answear


    Please type a Number::  15
The number has to be wetween 1 and 10

    Please type a Number::  4

Sorry, you guessed wrong.

            The correct guess was 8

【讨论】:

    【解决方案3】:

    实际上,您将iResponse 声明为integer 变量,并在scanf 中将说明符写为%c,即字符。

    scanf("%c", &iResponse);
    

    改成-

    scanf("%d", &iResponse);
    

    【讨论】:

      【解决方案4】:

      我认为你的意思是以下

      #include <stdio.h>
      #include <stdlib.h>
      #include <time.h>
      #include <errno.h>
      
      int main( void )
      {
          char line[20];
          unsigned int iRandom;
          unsigned int iResponse;
      
          const unsigned int Base = 10;
      
          srand( ( unsigned int )time( NULL ) );
      
          iRandom = rand() % Base + 1;
      
          printf( "\nGuess a number between 1 and %u: ", Base );
          scanf( "%20s", line );
      
          errno = 0;
          iResponse = ( unsigned int )strtoul( line, NULL, Base );
      
          if ( !errno && iResponse >= 1 && iResponse <=10 ) 
          {
              if ( iResponse == iRandom ) 
              {
                  puts( "\nYou guessed right." );
              }
              else 
              {
                  puts( "\nSorry, you guessed wrong." );
                  printf( "The correct guess was %u\n", iRandom );
              }
          }
          else 
          {
              puts( "\nYou did not enter a number" );
          }
      
          return 0;
      }
      

      程序输出可能如下所示

      Guess a number between 1 and 10: A
      You did not enter a number
      
      Guess a number between 1 and 10: 4
      Sorry, you guessed wrong.
      The correct guess was 3
      
      Guess a number between 1 and 10: 4
      You guessed right.
      

      至于你的代码,那么至少在这个语句中

      scanf("%c", &iResponse);
      

      您应该将"%c" 替换为" %c"(%c 之前的空白)。尽管即使在这种情况下,结果也将由实现定义。而且使用这种方法你将无法输入可接受的号码10。:)

      【讨论】:

        【解决方案5】:

        改变

        scanf("%c", &iResponse);
        
        if (isdigit(iResponse)) {
            . . .
        

        if (scanf("%d", &iResponse)) {
            . . .
        

        工作代码将如下所示

        #include <stdio.h>
        #include <time.h>
        
        int main() {
            srand(time(0));
            int iRandom = (rand() % 10) + 1, iResponse;
            printf("\nGuess a number between 1 and 10: ");
            if (scanf("%d", &iResponse)) {
                if (iResponse >= 1 && iResponse <= 10 ) {
                    if (iResponse == iRandom)
                        printf("\nYou guessed right.\n");
                    else 
                        printf("\nSorry, The correct guess was %d\n", iRandom);
                }
                else
                    printf("You did not enter a number between 1 and 10.");
            }
            else
                printf("\nYou did not enter a number.\n");
            return 0;
        }
        

        输入

        1
        

        样本输出

        Sorry, The correct guess was 9
        

        查看演示 http://ideone.com/V7GIeK

        【讨论】:

          【解决方案6】:

          尝试制作

          char iResponse;
          

          看到这个成功了

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-07-11
            • 1970-01-01
            • 2019-02-11
            • 1970-01-01
            相关资源
            最近更新 更多