开发者

Problem choosing a random word from a list of words in C

I am in an intro C programming class, and am trying to program a game of hangman. I am currently on the VERY early stages though, and hoping to get help with the part where I have the computer choose the word to be used for the game. Here is what I have so far:

#include <string.h>  
#include<string.h>  
#include <time.h> /* contains prototype for function time */  
#define MAX 10  

int main()  
{   
int randnum;
char gameword[MAX];
randnum = 1+(rand() % 20);
printf("%d\n", randnum);

if (randnum=1)(
    gameword= char word1[MAX]=who);
if randnum=2(
    gameword=char word2[MAX] = lives);
if randnum=3(
   gameword=char word3[MAX] = in);
if randnum=4(
   gameword=char word4[MAX] = a);
if randnum=5(
   gameword=char word5[MAX] = pineapple);
if randnum=6(
   gameword=char word6[MAX] = under);
if randnum=7(
   gameword=char word7[MAX] = the);
if randnum=8(
   gameword=char word8[MAX] = sea);
if randnum=9(
   gameword=char word9[MAX] = absorbant);
if randnum=10(
   gameword=char word10[MAX] = and);
if randnum=11(
   gameword=char word11[MAX] = yellow);
if randnum=12(
   gameword=char word12[MAX] = porous);
if randnum=13(
   gameword=char word13[MAX] 开发者_运维知识库= is);
if randnum=14(
   gameword=char word14[MAX] = he);
if randnum=15(
   gameword=char word15[MAX] = sponge);
if randnum=16(
   gameword=char word16[MAX] = bob);
if randnum=17(
   gameword=char word17[MAX] = square);
if randnum=18(
   gameword=char word18[MAX] = pants);
if randnum=19(
   gameword=char word19[MAX] = crabby);
if randnum=20(
   gameword=char word20[MAX] = patties);

printf("%c", gameword);
return 0;
}  

I am getting errors like:

line 19 suggest parentheses around assignment used as truth value

line 20 expected expression before 'char'

line 64 expected ';' before '}' token

line 64 control reaches end of non-void function

Any help would be great. I have a long way to go on this assignment, but I'm getting stuck very early here.


Some things:

  • in C (and many other language) the assignment operator = is not the same as the equality == operator. First one is used to assign a value to a variable while second one is used to check for equality
  • the code gameword=char word2[MAX] = lives); doesn't make much sense, what you want to do? you want to choose between a list of words. You should first hard code (I suppose) the list of words with something like

    char *words[]={"foo", "bar", "baz", "lol"};

then you generate the random number and choose the right string, eg words[randnum]


First off you'll want to use == as a conditional operator where you have randnum=1

== compares, = assigns

Next, you have an issue with syntax. It should be if (randnum == 20)

But, you'll probably want to place your strings into an array. So you can just use an array offset.


Use else if instead of second and next if


You should be using else if instead of if, if, if, if... This way once it hits the correct option, it won't loose moretime checking the others.

Also, = is assignment, == is used to compare.

Then, except the first "if", all the others are missing the conditions:

if randnum=2(
    gameword=char word2[MAX] = lives);

Should be

if (randnum=2)
    gameword=char word2[MAX] = lives);


= is the assignment operator. == tests for equality. You want to change your code to the following:

if(randnum == 1)
    gameword = "who"; 
else if(randnum == 2)
    gameword = "lives";
else if(randnum == 3)
    gameword = "in";

and so on. Remember that each if statement is going to be followed by a single line statement or a block of code enclosed in braces.

Your print statement should be as follows: printf("%s", gameword); %s tells printf that you are printing a string. %c will only print a single character.


// ...
if (randnum == 13)
  gameword = "is"; else
// ...

printf("%s\n", gameword);

And here is your code, more C-like:

#include <string.h>  
#include<string.h>  
#include <time.h> /* contains prototype for function time */  

int main()  
{   
  int randnum;  
  char gameword[10];

  randnum = 1 + (rand() % 20);  
  printf("%d\n", randnum);  

  switch (randnum)
  {
    case 1:
        gameword = "who";
    break;  

    case 2:
        gameword = "lives";
    break;

    case 3:
        gameword = "in";
    break;

    case 4:
        gameword = "a";
    break;

    case 5:
        gameword = "pineapple";
    break;

    case 6:
        gameword = "under";
    break;

    case 7:
        gameword = "the";
    break;

    case 8:
        gameword = "sea";
    break;

    //...
  }

  return 0;
}


You should be able to spot a lot of your errors just by eyeballing correct code and making a visual comparison. You're using the wrong symbols, you've got mismatched parentheses, and the gameword= lines are plain bizarre looking.

If statements

if (randnum=1)(
    gameword= char word1[MAX]=who);
  • Use two equal signs for comparisons.
  • You need a balanced set of parentheses around the conditional expression.
  • Use curly braces { and } to surround the code inside the if statement.
  • You need to quote strings like "who" and "pineapple". Double quotes.
  • Your assignment statements are not even close, sorry. What you were aiming for is gameword = "who";
  • Except with gameword = "who"; you will get an error about illegal assignment to an array. It's too much to go into here, but suffice it to say that you need to use the strcpy function: strcpy(gameword, "who")

Taking in all those comments, here's a valid if statement you can use as a model:

if (randnum == 1) {  
    strcpy(gameword, "who");
}

Printing

printf("%c", gameword);

The %c format specifier is for printing single characters. You are trying to print a string, so use %s instead. Also it'll look better if you add a newline character '\n' at the end.

printf("%s\n", gameword);

Random numbers

Once you get this to compile you'll end up seeing the same word printed every time. The rand() function is only a pseudo-random number generator. The random numbers it returns aren't truly random; they are actually generated using a fixed algorithm. This means it will always return the same random numbers in the same order unless you seed the random number generator.

A decent seed is the system clock. The clock changes regularly so you can use it to feed in a varying seed number to the RNG. Insert this line at the top of your program before you call rand():

srand(time(NULL));


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


int main (void) {
    const int numberOfWords = 3
    srand((unsigned)time(NULL)); //generate a random seed based on time so it's different every time!
    int ran = rand()% numberOfWords; //Generate a random number between 0 to numberOfWords - 1
    char *hello[] = {"One", "Two", "Three"}; //Fill in the array with words
    printf("%s", hello[ran]); //Print a string and choose the nth element from the array
    //remember arrays are 0 based!
    return 0;
}

Aside from all the mismatched brackets, and == and things, using 20 if statements isn't the way to go. What you really want is to have a character array and use your random number as an index. I would highly suggest you look over the above code carefully and not just copy and paste it. I know I shouldn't be giving you all the code, but so many if statements are just painful.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜