0

I'm currently trying to create a little game in C and wanted to produce a little prototype where it would print a random word, make the user type the word, then it will show them how they typed it and then it will show the user how it should be typed.

The reason I want to show the user what they typed is so that at the end they can be an overview but right now I would just like to get it working.

Currently instead of displaying what the user put it shows numbers and I think that it has to do with the array in which I put the words but I have no idea. How can I get it to print the word in the array? Here's the code:

#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;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Tytoowns
  • 3
  • 3
  • 1
    If you compile your program with warnings enabled you will get some hints. – August Karlstrom Apr 24 '16 at 15:19
  • At some point, read [What should `main()` return in C and C++](http://stackoverflow.com/questions/204476/) and if you're not working on Windows, you need to immediately fix your definition of `main()` to return `int`. – Jonathan Leffler Apr 24 '16 at 15:21
  • The line `printf("%s\n",answer[x]);` doesn't do what you think it does. A good compiler (GCC, for example, with `-Wall`) will tell you about the error. Other compilers may not — and the resulting program may crash or produce gibberish. Your code has 4 levels of loop in it. That cries out for the use of functions somewhere along the line (or a clean up of the algorithm so fewer levels of loop are necessary). – Jonathan Leffler Apr 24 '16 at 15:29
  • The `do { … }while (x<20,x++);` condition doesn't do what you seem to think it does — your program will do one cycle only. You should also test the return value from `scanf()` to be sure you got some input from the user: `if (scanf("%254s", answer) != 1) break;` or thereabouts. Also note that `%255s` allows `scanf()` to write 255 non-null characters _and_ a null to the string — there's an off-by-one issue which is hallowed by tradition. – Jonathan Leffler Apr 24 '16 at 15:43

1 Answers1

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;
}

I hope this is what you want.

Rijul Sudhir
  • 2,064
  • 1
  • 15
  • 19
  • I didn't get the purpose of having a for loop inside a do while loop and both having a condition less than 20. – Rijul Sudhir Apr 24 '16 at 16:14
  • Thank you very much, i see what you mean with all the loops and am going to clean it up later, i just wanted to get it working. Thanks. – Tytoowns Apr 24 '16 at 16:36