i dont know how to random select a word in c . a big help is highly appreciated .. tnx :)
for example the words are Ana,Lorie and Denise . how to random select from those words ?

How are those words stored? Do you know about srand() and rand()? Are you just selecting a single word, or do they need to be randomly ordered?

I'd start by storing the words in an array and then using rand() to get a random index into the array. This design can also be extended to a random shuffle if necessary.

commented: what is wrong in my code..??? -1

im new when it comes to random selection . im creating a game there are 3choices ANA,LORIE and DENISE ,the user will guess who will the computer will choose(the computer will randomly select a name)

Okay, so you're selecting a single word. Have you tried my suggestion or done any research based on my previous post?

string[] words = {"ANA","LORIE","DENISE"};


is it correct ? how am i supposed to put the random selection in there ?

Good god, you're helpless. Didn't I point you toward rand()?

string[] words = {"ANA","LORIE","DENISE"};

puts(words[rand() % 3]); /* Print a random word */

You're clearly not putting any effort into helping yourself, so that's the end of my attempt to assist.

sorry to bother again but I dont have an idea why I have errors it says undefined symbol ..

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

void main ()
{
    string[] words = {"ANA","LORIE","DENISE"};
    printf("Enter your choice");
    puts(words[rand() % 3]);
    printf("Wrong guess");

    getch();
}

Let me guess, you're coming from a Java background? I suggest you get a book on C and actually read it before trying to type random code. Though I'll take some blame for being lazy and not correcting your broken syntax for the words array. Here's your corrected code:

#include <stdio.h>
#include <stdlib.h>
 
int main(void)
{
    const char *words[] = {"ANA", "LORIE", "DENISE"};
    
    puts(words[rand() % 3]);
 
    return 0;
}

tnx a lot its working , but how about if i need to have many random selection to do ? do i need to use the time seed ?

my game is played 10 times so that means the computer will do a random selection 10 times too but sadly when i try the code you've corrected its always pick the word "LORIE" it doesnt changes .

Start at Narue's first post. The answer is there. You just have to look it up.

You can use enum datatype for this.... my idea is that when you are using a enum variable then u associate a integer to ur string.... if u can do this then u can use....

#include<stdlib.h>
int random (int n);

After using this You can select a random string.... Hope You can understand my idea... If any problem then u just ask me again.... i will post the full code..:):)

commented: Offering full code is not acceptable. -4
int random(int n);

it is a function which randomly select an integer such as...:
0<=integer<=n
... so u can easily select a string...:):):)

If any problem then u just ask me again....

What if the compiler doesn't support the non-standard function you suggested?

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>     
void main ()
{
enum name {ANA,LORIE,DENISE};
enum name var;
var = random(2);
switch(var)
{
case 0:
printf("ANA\n");
break;
case 1:
printf("LORIE\n");
break;
case 2:
printf("DENISE\n");
break;
}
getch();
}

Try this I hope it will work....:):):):)

commented: Maybe you should learn C properly and then you can stop hoping. -4

im creating a game about that . what if i want to have 3rounds ? what am i supposed to do . i only need to use if else statement or switch

what if i want to have 3rounds ? what am i supposed to do .

You're supposed to use a loop:

#include <stdio.h>

#define MAX_ROUNDS 3

int play(void);

int main(void)
{
    int rounds = 0;
    
    while (++rounds <= MAX_ROUNDS) {
        printf("Round %d\n", rounds);
        
        if (play()) {
            puts("You've won!");
            break;
        }
    }
    
    return 0;
}

Obviously, the work of the game itself is done in the play() function that you need to define.

i only need to use if else statement or switch

This statement is confusing. Do you believe you only need if statements or a switch? Or is that a requirement that was given to you for this program? While it can be done without a loop (or an explicit loop), doing so would be silly.

actually its a requirement not to use looping but tnx for your help anyway . i do appreciate it :))))

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.