试图了解如何用C编程数组

时间:2014-10-14 23:23:26

标签: c

所以我正在经历艰难的学习C"在线预订我决定用这个程序测试我的技能。我想这样做,这样用户就可以输入一个字母,看看它是否与秘密字中的任何字符相匹配,这样他们就能猜出来。我应该在for语句中使用if else语句吗?此外,即使我正确输入,我仍然无法通过程序告诉我写了这封信。

#include <string.h>
#include <stdlib.h>

int main()
{
    /*Declaring the variables the user needs. One for the
      the user enters and the other for the secret word.*/

    char user_inputted_letter[1];
    char  secret_word[] = {'Q','u','a','l','m'};

    printf("See if you can guess the secret 5 letter word, if you enter in a letter \n"
            "that matches a letter in the secret word it will be displayed in \n"
            "a random order for you to guess! \n"); 

    scanf("%s",user_inputted_letter);
    printf("Your user inputted letter is %s \n",user_inputted_letter);

    /*for statement  to  scan each of the characters
      in secret_word. If statement detects if there is a match
      between the secret character and inputed value */
    int i=0;
    for(i=0; i < 6;i++)
    {
        if(secret_word[i] == user_inputted_letter[1]){
            printf("The matching letter you entered with the secret word is %s \n",user_inputted_letter);
        }
        else
            printf("There were no matching letters with the secret word\n");
    }

    return 0;
}

1 个答案:

答案 0 :(得分:0)

首先,您需要更多的输入空间。替换

char user_inputted_letter[1];

char user_inputted_letter[100];

其次,您需要比较所有字符。取代

if(secret_word[i] == user_inputted_letter[1]){

if(secret_word[i] == user_inputted_letter[i]){

和(因为你只有5个字符)

for(i=0; i < 6;i++)

for(i=0; i < 5;i++)