documentation
The char a = ant; is only valid if ant is a character defined at some other location (unlikely in your code example). A char variable can only hold values of a single character ( 'a', 'b', 'c' for example). In your case it might be better to investigate enumerations ( enum ) as it will allow a name to be associated to an integer value as you seem to want:
enum animal { ANT = 0, BEAR, CAT, DOG, ... };
L7Sqr
Practically a Master Poster
657 posts since Feb 2011
Reputation Points: 201
Solved Threads: 124
If you wanna store the names like a = ant you should use strings (char*).
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#define sz 12
int main()
{
int i, j;
char* animals[sz] = {"ant", "bear", "cat", "dog", "emu", "goat", "lizard", "monkey", "parrot", "rabbit", "snake", "tiger"};
char* chosen[4] = {"", "", "", ""};
srand(time(NULL));
for( i = 0; i < 4; i++ )
{
chosen[i] = animals[rand()%12];
for( j = 0; j < 4; j++ )
if( strcmp(chosen[i], chosen[j]) == 0 && j != i ) //checks to see if the animal picked is unique
{
i--; //if not decrease counter so it overwrites the last animal
break;
}
}
for( i = 0; i < 4; i++ )
{
printf("%c - ", chosen[i][0]); //for first letter of animals name
printf("%s ", chosen[i]); //for whole name of animal
}
printf("\n");
return 0;
}
sfuo
Practically a Master Poster
656 posts since Jul 2009
Reputation Points: 164
Solved Threads: 99
sfuo
Practically a Master Poster
656 posts since Jul 2009
Reputation Points: 164
Solved Threads: 99