2016-04-24 25 views
0

我目前正在嘗試在C中創建一個小遊戲,並且希望生成一個小型原型,它將打印隨機單詞,讓用戶鍵入單詞,然後會顯示他們如何鍵入它,然後它會顯示用戶應該如何鍵入它。C編程 - 將一個單詞添加到數組中,然後打印出陣列中的內容

我想向用戶展示他們輸入的內容的原因是,最終他們可以是一個概述,但現在我只想讓它工作。

目前,而不是顯示用戶把它顯示的數字,我認爲它與我把這些單詞放在數組中,但我不知道。我怎樣才能讓它打印數組中的單詞?下面的代碼:

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

void main() 
{ 
    char *words[] = {"sausage","blubber","pencil","cloud","moon","water","computer","school","network","hammer","walking","violently","mediocre","literature","chair","two","window","cords","musical","zebra","xylophone","penguin","home","dog","final","ink","teacher","fun","website","banana","uncle","softly","mega","ten","awesome","attatch","blue","internet","bottle","tight","zone","tomato","prison","hydro","cleaning","telivision","send","frog","cup","book","zooming","falling","evily","gamer","lid","juice","moniter","captain","bonding","loudly","thudding","guitar","shaving","hair","soccer","water","racket","table","late","media","desktop","flipper","club","flying","smooth","monster","purple","guardian","bold","hyperlink","presentation","world","national","comment","element","magic","lion","sand","crust","toast","jam","hunter","forest","foraging","silently","tawesomated","joshing","pong","RANDOM","WORD"}; 
    char *questions[] = {"sausage1","blubber1","pencil1","cloud1","moon1","water1","computer1","school1","network1","hammer1","walking1","violently1","mediocre1","literature1","chair1","two1","window1","cords1","musical1","zebra1","xylophone1","penguin1","home1","dog1","final1","ink1","teacher1","fun1","website1","banana1","uncle1","softly1","mega1","ten1","awesome1","attatch1","blue1","internet1","bottle1","tight1","zone1","tomato1","prison1","hydro1","cleaning1","telivision1","send1","frog1","cup1","book1","zooming1","falling1","evily1","gamer1","lid1","juice1","moniter1","captain1","bonding1","loudly1","thudding1","guitar1","shaving1","hair1","soccer1","water1","racket1","table1","late1","media1","desktop1","flipper1","club1","flying1","smooth1","monster1","purple1","guardian1","bold1","hyperlink1","presentation1","world1","national1","comment1","element1","magic1","lion1","sand1","crust1","toast1","jam1","hunter1","forest1","foraging1","silently1","tawesomated1","joshing1","pong1","RANDOM1","WORD1"}; 
    char answer[255] = ""; 

    int word; 
    int vec[20] = { 0 }; 
    int i, j; 
    int x=0; 
    srand(time(NULL)); 

    do{ 
     for (i = 0; i < 20; i++) { 
      int okay = 0; 

      while (!okay) { 
       vec[i] = rand() % 100 + 1; 
       okay = 1; 

       for (j = 0; j < i; j++) { 
        if (vec[i] == vec[j]) okay = 0; 
       } 
      } 

      word=vec[i]; 

      printf("%s\n",questions[word]); //print a word 
      scanf("%255s",answer);// wait for the user to type the word 
      printf("%s\n",answer[x]);// show what the user typed 

      printf("%s\n\n",words[word]);// show how they should of typed it 

     } 
    }while (x<20,x++); 

    return 0; 
} 
+1

如果您在啓用警告的情況下編譯程序,您將獲得一些提示。 –

+0

在某些時候,請閱讀[應該在C和C++中返回什麼'main()'](http://stackoverflow.com/questions/204476/),如果您不在Windows上工作,則需要立即修復定義main()來返回int。 –

+0

'printf(「%s \ n」,answer [x]);'不符合你的想法。一個好的編譯器(例如GCC,帶'-Wall')會告訴你這個錯誤。其他編譯器可能不會 - 並且由此產生的程序可能會崩潰或產生亂碼。你的代碼有4個級別的循環。這需要使用函數的某些功能(或者清理算法,因此需要更少的循環級別)。 –

回答

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

void main() 
{ 
char *words[] = {"sausage","blubber","pencil","cloud","moon","water","computer","school","network","hammer","walking","violently","mediocre","literature","chair","two","window","cords","musical","zebra","xylophone","penguin","home","dog","final","ink","teacher","fun","website","banana","uncle","softly","mega","ten","awesome","attatch","blue","internet","bottle","tight","zone","tomato","prison","hydro","cleaning","telivision","send","frog","cup","book","zooming","falling","evily","gamer","lid","juice","moniter","captain","bonding","loudly","thudding","guitar","shaving","hair","soccer","water","racket","table","late","media","desktop","flipper","club","flying","smooth","monster","purple","guardian","bold","hyperlink","presentation","world","national","comment","element","magic","lion","sand","crust","toast","jam","hunter","forest","foraging","silently","tawesomated","joshing","pong","RANDOM","WORD"}; 
char *questions[] = {"sausage1","blubber1","pencil1","cloud1","moon1","water1","computer1","school1","network1","hammer1","walking1","violently1","mediocre1","literature1","chair1","two1","window1","cords1","musical1","zebra1","xylophone1","penguin1","home1","dog1","final1","ink1","teacher1","fun1","website1","banana1","uncle1","softly1","mega1","ten1","awesome1","attatch1","blue1","internet1","bottle1","tight1","zone1","tomato1","prison1","hydro1","cleaning1","telivision1","send1","frog1","cup1","book1","zooming1","falling1","evily1","gamer1","lid1","juice1","moniter1","captain1","bonding1","loudly1","thudding1","guitar1","shaving1","hair1","soccer1","water1","racket1","table1","late1","media1","desktop1","flipper1","club1","flying1","smooth1","monster1","purple1","guardian1","bold1","hyperlink1","presentation1","world1","national1","comment1","element1","magic1","lion1","sand1","crust1","toast1","jam1","hunter1","forest1","foraging1","silently1","tawesomated1","joshing1","pong1","RANDOM1","WORD1"}; 
char answer[20][255]; 

int word; 
int vec[20] = { 0 }; 
int i, j; 
int x=0; 
srand(time(NULL)); 

do{ 
    for (i = 0; i < 20; i++) { 
     int okay = 0; 

     while (!okay) { 
      vec[i] = rand() % 100 + 1; 
      okay = 1; 

      for (j = 0; j < i; j++) { 
       if (vec[i] == vec[j]) okay = 0; 
      } 
     } 

     word=vec[i]; 

     printf("%s\n",questions[word]); //print a word 
     scanf("%s",answer[i]);// wait for the user to type the word 
     printf("%s\n",answer[i]);// show what the user typed 

     printf("%s\n\n",words[word]);// show how they should of typed it 

    } 
}while (x<20,x++); 

return 0; 
} 

我希望這是你想要的。

+0

我沒有得到在while while循環中使用for循環並且條件少於20的目的。 –

+0

非常感謝您,我明白您對所有循環的含義,並將在稍後清理它,我只是想讓它工作。謝謝。 – Tytoowns

相關問題