尽管添加了break语句,但条件仍然继续执行并打印出不正确的错误消息。

huangapple go评论57阅读模式
英文:

Despite adding a break statement, if condition still moves on and prints out incorrect error message

问题

问题出在用户输入例如 "123" 时,应该打印需要输入 3 个更多的数字,但它总是打印 "A character in your guess was not a digit or a whitespace" 错误。原因是在您的代码中有一个逻辑错误。请修改以下部分:

for (int i = 0; i < 6; i++) {
    if (isdigit(input[i] == 1)){ // 这一行有问题
        numCount++;
        if (numCount < maxLength && numCount != 0){
            enoughDigits = 1;
        }
        else{
            printf("You need to enter %d digits to complete your guess\n", remainder);
        }
        error = 1;
    }
}

将上述代码块更改为以下内容:

for (int i = 0; i < 6; i++) {
    if (isdigit(input[i])) { // 移除 == 1,只检查是否是数字
        numCount++;
    }
}

if (numCount < maxLength && numCount != 0) {
    enoughDigits = 1;
} else {
    printf("You need to enter %d digits to complete your guess\n", remainder);
    error = 1;
}

这样就可以正确地检查是否输入了足够的数字,并且只在需要时打印错误消息。

英文:

So for an assignment I am supposed to check the input from a user and determine whether:

  1. if there are characters in the input: I print an error message saying it is an invalid input and ask for input again until it is valid

  2. if there are less than 6 digits in the input: I print an error message that there are x many digits remaining and ask for input again until it is valid

int main() {
    int solutionArray[6];
    int guessArray[6];
    int indexArray[6];

    /*Creating the random numbers and asking for seed*/
    
    int seed;
    printf(&quot;Enter the integer value of the seed for the game: &quot;);
    scanf(&quot;%d&quot;, &amp;seed);
    srand(seed);

    /*Adding the randomized integers into solutionArray*/
    for(int i = 0; i &lt; 6; i++){
            solutionArray[i] = rand() % 10;
            printf(&quot;%3d&quot;, solutionArray[i]);
        }
        printf(&quot;\n&quot;);

    /*Game interface*/
    printf(&quot;For each turn enter 6 digits 0 &lt;= digit &lt;= 9 \n&quot;);
    printf(&quot;Spaces or tabs in your response will be ignored. \n&quot;);


    /*Validating conditions of the user input*/
    char input[50];
    int error = 0;
    int enoughDigits = 0;

    do {
        printf(&quot;Enter your guess, 6 digits: &quot;);
        scanf(&quot;%s&quot;, input);
        int numCount = 0;
        int remainder = 0;
        int maxLength = 6;

        // Checking to see if there are any characters other than digits and prints error
        for (int i = 0; i &lt; 6; i++) {
            if (!isdigit(input[i])) {
                printf(&quot;ERROR: A character in your guess was not a digit or a white space\n&quot;);
                printf(&quot;\n&quot;);
                error = 1;
                break;
            }
        }

        /*Checking to see if enough digits have been inputted*/ 
        remainder = 6 - numCount;
        for (int i = 0; i &lt; 6; i++){
            if (isdigit(input[i] == 1)){
                numCount++;
                if (numCount &lt; maxLength &amp;&amp; numCount != 0){
                enoughDigits = 1;
            }
                else{
                    printf(&quot;You need to enter %d digits to complete your guess \n&quot;, remainder);
            }
            error = 1;
            }
        }
        
        // Print the guess if there are no errors
        if (error == 0) {
            printf(&quot;Your guess was:\n&quot;);
            for (int i = 0; i &lt; 6; i++) {
                guessArray[i] = input[i] - &#39;0&#39;;
                printf(&quot;%2d&quot;, guessArray[i]);
            }
            printf(&quot;\n&quot;);
        }

        // Printing results of the guesses 
        int partialMatches = 0;
        int perfectMatches = 0;

       for (int i = 0; i &lt; 6; i++) {
            partialMatches = 0;
            for (int j = 0; j &lt; 6; j++) {
                if (solutionArray[i] == guessArray[j]) {
                    partialMatches++;
                    break;  // Exit inner loop after finding a partial match
                }
            }
            if (solutionArray[i] == guessArray[i]) {
                perfectMatches++;
                partialMatches--;
            }
        }
        if (perfectMatches == 6){
            printf(&quot;YOU DID IT! ! \n&quot;);
            error = 0;
            break;
        }
        printf(&quot;%d matches, %d partial matches\n&quot;,perfectMatches, partialMatches);
        error = 1; 

    } while (error == 1 || enoughDigits == 1);

return 0;
}

My issue comes in when the user inputs for example "123" it should print that I need to enter 3 more digits but it only prints "A character in your guess was not a digit or a whitespace". For some reason it prints that error regardless of the input.

Attached is my code below. Thanks in advance!

答案1

得分: 1

至少有这些问题

缓冲区溢出

scanf("%s", input) 不能防止太多字符保存到 input[] 中。使用一个 宽度。由于 input 的大小为50,使用宽度为49。

scanf("%49s", input)

错误的测试

isdigit(input[i] == 1) 显然应该是 isdigit(input[i]),以测试 input[i] 是否为数字。

英文:

At least these problems

Buffer overflow

scanf(&quot;%s&quot;, input) does not prevent too many characters in saving to input[]. Use a width. Since input is size 50, use a width of 49.

scanf(&quot;%49s&quot;, input)

Wrong test

isdigit(input[i] == 1) certainly should be isdigit(input[i]) to test if input[i] is a digit.

huangapple
  • 本文由 发表于 2023年6月16日 11:30:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76486799.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定