【问题标题】:Create a guessing game till I want to stop创建一个猜谜游戏直到我想停下来
【发布时间】:2020-03-25 15:51:41
【问题描述】:

我是编程新手,我正在尝试修改这个程序,以便我可以输入猜测,直到我想停止。任何帮助都会很棒!

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
main()
{
    char name[10][20]= {"DUMBO","MICKEY MOUSE","GOOFY","DONALD DUCK"};
    char charName[20];
    int count;
    char found;

    printf("Enter the name of a Disney cartoon character ");
    gets(charName);
    strupr (charName);
    found = 'n';

      for (count=0;count<4;count++)
    { if(!strcmp(charName, name[count]))
        {found = 'y';}
    }

    if(found == 'y')
        puts("Match");
        else
        puts("No Match");

        system("pause");
        return;
    }

【问题讨论】:

  • 你必须把gets和条件if found=='y'放在你的循环中

标签: dev-c++


【解决方案1】:

保持它运行的一个好方法是使用 while 循环。我对 C++ 不太熟悉,但总体思路是:

declare list of names

nameMatch = 0

while nameMatch == 0:
    Ask the user to enter a name
    if input == one of the names
        puts("Match")
        nameMatch = 1
    else
        puts("No Match")

这样,它会一直询问,直到用户选择正确的名称或您要设置的任何其他条件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-15
    • 2020-11-22
    • 2014-05-12
    • 1970-01-01
    相关资源
    最近更新 更多