C程序调用一个循环中包含字符串数组 [英] C Program calling array containing strings within a loop

查看:127
本文介绍了C程序调用一个循环中包含字符串数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是相当新的C,而我与我的数组有问题。
该应用程序是一个字谜游戏,应该比较网友猜测正确的话。
我试图记录所有不正确的答案,以及在两个不同阵列的答案的正确版本。
然而,在节目的最后,当我尝试和打印所有的猜测不正确,正确的版本,只打印数组中的第一项。
我想这可能是造成阵列的一个问题,因为他们持有的字符串,所以在C中,它本质上是一个数组,我认为内的阵列?
为什么我的code刚刚从阵列打印的第一项?

(还有更多的code,然而,这仅仅是个关键部分,我觉得这是需要观察的唯一部分)

 字符correctWord [20];
炭anagramWord [20];
诠释猜测,得分;
字符* incorrectGuess [20];
字符* correctVersion [20];无效userGuess(){
炭userWordGuess [20];的printf(字谜:);
的printf(anagramWord);
的printf(\\ n您的猜测:);
scanf函数(%S,userWordGuess); //读取用户输入
strtok的(correctWord,\\ n);
猜测++;如果(STRCMP(userWordGuess,correctWord)== 0){
    的printf(恭喜你,你猜对了!(请等待下一个问题......)\\ n);
        得分++;
        睡眠(1600);
        系统(CLS);
    }其他{
        的printf(不正确的,更努力(请等待下一个问题......)\\ n!);
        睡眠(1600);
        系统(CLS);
        INT I = 0;
        incorrectGuess [I] =(userWordGuess);
        correctVersion [I] =(correctWord);
        我++;
    }
}无效finalScore(){
    INT I;
    系统(CLS);
    INT百分比=((得分/猜测)* 100);
    的printf(恭喜你 - 游戏结束!);
    的printf(\\ n猜测:%D,猜测);
    的printf(\\ n得分:%D,成绩);
    的printf(\\ n比例正确:%D,百分比);
    INT numberOfIncorrect =(猜测分数);
    对于(i = 0; I< = numberOfIncorrect;我++){
        的printf(incorrectGuess [I]);
        的printf(correctVersion [I]);
    }
    残培();
}


解决方案

在此code,你是不是初始化猜测分数与任何价值,并尝试增加它们。你应该有

  INT猜测= 0,得分= 0;

然后,在你的else块

 其他
{
    的printf(不正确的,更努力(请等待下一个问题......)\\ n!);
    睡眠(1600);
    系统(CLS);
    INT I = 0; //初始化我每次都要0
    incorrectGuess [I] =(userWordGuess);
    correctVersion [I] =(correctWord);
    我++;
}

在注释行,你初始化 I 0 每次

I am fairly new to C, and am having problems with my array. The application is an anagram game, and should compare the users guess to the correct word. I am trying to log all of the incorrect answers, as well as the correct versions of the answers in two seperate arrays. However, at the end of the program when I try and print all of the incorrect guesses and correct versions, it only prints the first term in the array. I think it may be a problem caused by the arrays, as they are holding strings, so in C it is essentially an array within an array I think? Why does my code just print the first term from the arrays?

(There is more code, however this is just the key part, I think this is the only section that needs looking at)

char correctWord[20];
char anagramWord[20];
int  guesses, score;
char* incorrectGuess[20];
char* correctVersion[20];

void userGuess(){
char userWordGuess[20];

printf("Anagram: ");
printf(anagramWord);
printf("\n Your Guess: ");
scanf("%s",userWordGuess); //Reads in user input
strtok(correctWord, "\n");
guesses++;

if(strcmp(userWordGuess, correctWord) == 0){
    printf("Congratulations, you guessed correctly! (Please wait for the 

next question...)\n");
        score++;
        Sleep(1600);
        system("cls");
    }else{
        printf("Incorrect, try harder!(Please wait for the next question...) \n");
        Sleep(1600);
        system("cls");
        int i = 0;
        incorrectGuess[i]=(userWordGuess);
        correctVersion[i]=(correctWord);
        i++;
    }
}

void finalScore(){
    int i;
    system("cls");
    int percentage = ((score/guesses) * 100);
    printf("Congratulations - Game Complete!");
    printf("\n Guesses: %d", guesses);
    printf("\n Score: %d", score);
    printf("\n Percentage Correct: %d", percentage);
    int numberOfIncorrect = (guesses-score);
    for(i=0;i<=numberOfIncorrect;i++){
        printf(incorrectGuess[i]);
        printf(correctVersion[i]);
    }
    getch();
}

解决方案

In this code, you are not initializing guesses and score with any values, and you try to increment them. You should have

int  guesses = 0 , score = 0 ;

and then, in your else block

else
{
    printf("Incorrect, try harder!(Please wait for the next question...) \n");
    Sleep(1600);
    system("cls");
    int i = 0;                             // you initialize i to 0 every time
    incorrectGuess[i]=(userWordGuess);
    correctVersion[i]=(correctWord);
    i++;
}

in the commented line, you initialize i to 0 each time.

这篇关于C程序调用一个循环中包含字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆