【问题标题】:Is my do while loop not working due to this function?由于此功能,我的 do while 循环是否不起作用?
【发布时间】:2020-08-08 09:30:11
【问题描述】:

我正在编写一个程序,要求用户猜测计算机正在考虑的数字 1-100。

在程序结束时,当用户猜对了数字时,我试图让程序询问用户是否想再玩一次(重新启动程序)。

为了解决这个问题,我尝试使用do while 循环和char repeat;。循环几乎从程序的开始一直延伸到结束,尽管没有成功。有谁知道我做错了什么?是不是因为talfunktion这个函数导致循环不通过?

代码:

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

int talfunktion (int tal, int guess, int tries, char repeat);

int main () {

do {
    srand(time(NULL));
    int tal = rand() % 100 + 1; //tal is the correct value that the code is thinking of 
    int guess; //guess is the guessed value of the user
    int tries = 0; // amount of tries it took until getting correct
    char repeat;


    printf("Psst, the right number is: %d \n", tal); // remove later, not relevant to uppg.

    printf("Im thinking of a number between 1 and 100, guess which!");
    printf("\nEnter: ");
    scanf("%d", &guess);
    guess = talfunktion(tal, guess, tries, repeat);

    getchar();
    getchar();
    return 0;

    }

    int talfunktion(int tal, int guess, int tries, char repeat) {
        do {
            if (guess < tal) {
                tries++;
                printf("\nYour guess is too low, try again!");
                printf("\nEnter: ");
                scanf("%d", &guess);
            }
            else if (guess > tal) {
                tries++;
                printf("\nYour guess is too high, try again!");
                printf("\nEnter: ");
                scanf("%d", &guess);
            }
        } while (guess > tal || guess < tal);

        if (guess == tal) {
            printf("\nCongratulations, that is correct!");
            tries++;
            printf("\nYou made %d attempt(s)", tries);
            printf("\nPlay Again? (y/n)");
            scanf("%c", &repeat);
    }
} while (repeat == 'y' || repeat == 'Y');


}



【问题讨论】:

  • 你的循环中有一个return 0;,所以它永远不会循环。此外,您有一个 } 匹配 do 之后的 return 没有 while 子句。
  • 此代码无法编译。你不能在 C 中嵌套这样的函数。将 talfunktion 函数移出 main 函数的主体。
  • 不要为此使用do-while。使用while (true) 循环,然后在用户拒绝时使用break 退出循环。
  • @clay0 函数声明没有意义。函数中不使用其参数try和repeat的值。
  • guess &gt; tal || guess &lt; talguess != tal 相同。另外(repeat == 'y' || repeat == 'Y') 可以简化为(repeat == 'y' )

标签: c function loops while-loop


【解决方案1】:

这是一种可能的解决方案

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

void talfunktion(int tal, int guess, int* tries)
{
            if (guess < tal)
            {
                (*tries)++;
                printf("\nYour guess is too low, try again!");
            }
            else if (guess > tal)
            {
                (*tries)++;
                printf("\nYour guess is too high, try again!");
            }
            else if (guess == tal)
            {
                (*tries)++;
                printf("\nCongratulations, that is correct!");
                printf("\nYou made %d attempt(s)", *tries);
            }
}

int main (void)
{
    int tal; //tal is the correct value that the code is thinking of
    int guess; //guess is the guessed value of the user
    int tries = 0; // amount of tries it took until getting correct
    char playAgain;

    do {
            srand(time(NULL));
            tal = rand() % 100 + 1; //tal is the correct value that the code is thinking of
            printf("\nIm thinking of a number between 1 and 100, guess which!");
            printf("\nEnter: ");
            scanf("%d", &guess);
            talfunktion(tal, guess, &tries);

            printf("\nPsst, the right number is: %d", tal); // remove later, not relevant to uppg.
            getchar(); //to halt the code for taking the input

            if(guess == tal)
            {
                tries = 0;
                printf("\nPlay Again? (y/n)\n");
                scanf("%c", &playAgain);
            }

    } while (playAgain != 'n');

return 0;
}

【讨论】:

    【解决方案2】:

    在描述问题的 cmets 中提到了几件事, 你应该看的东西:

    • 不要在另一个函数中定义一个函数
    • 注意放置返回语句的位置
    • 使用字符测试时,变量使用char类型
    • 考虑简化您的逻辑比较。 (例如guess &gt; tal || guess &lt; talguess != tal 相同)
    • 确保放置自动变量,以便它们在使用时可见。
    • 在格式说明符中放置空格:" %c" 用于scanf() 以使用换行符。 (而不是过度使用getchar()

    这是您的代码的简化版本,修改了 maintalfunktion 函数...

    char talfunktion(int tal);
    
    int main (void) {
         int tal=0;//remove from inside {...} to make it visible to rest of function
         char repeat = 'n';
    
         srand(time(NULL));
         tal = rand() % 100 + 1; //tal is the correct value that the code is thinking of 
    
        do {
    
                repeat = talfunktion(tal);
    
            }while((tolower(repeat) == 'y'));
    
            return 0;
    }
    
    char talfunktion(int tal)//do all relevant work in function and return 
    {                        //only what is necessary
         int guess = 0;
         char repeat = 'n';
    
    
        printf("Im thinking of a number between 1 and 100, guess which!");
        printf("\nEnter a number from 1 to 100: ");
        scanf("%d", &guess);
        if((guess < 1) || (guess > 100))
        {
            printf("Entered out of bounds guess...\n");
        }
        else if (guess != tal)
        {
            if(guess < tal) printf("guess too small\n");
            else printf("guess too large\n");
            printf("Try  again? <'n' or 'y'>\n");
            scanf(" %c", &repeat);//space in format specifier to consume newline character
            if(tolower(repeat) != 'y') return 'n';//tolower() allows both upper and lower case
        }
        else
        {
            printf("Congratulations: You guessed right.\n");
            printf("Play again? <'n' or 'y'>\n");
            scanf(" %c", &repeat);
        }
        return repeat;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-15
      • 1970-01-01
      • 1970-01-01
      • 2022-12-12
      • 1970-01-01
      • 1970-01-01
      • 2013-05-06
      • 1970-01-01
      相关资源
      最近更新 更多