1

I want to make a function in C but clueless what to do I know how to do it with integers but not with characters/strings

printf("\nEnter player %d name: ", i);

scanf ("%s", playername[i]);

This allows me to read name. %d is just a number above

I want to create a function where it generates a random number (I know how to do that) and then print out a greeting message

So something like this

int grmessage;
grmessage = rand () %4;



srand (time(NULL));

if (grmessage == 0)
printf("Welcome to the game, %s!\n" , playername[i]);
else if (grmessage == 1)
printf("Hello %s and welcome!\n" , playername[i]);
else if (grmessage == 2)
printf("%s has joined us! Say hello!\n" , playername[i]);
else if (grmessage == 3)
printf("Pleasure to have you in this game %s!\n" , playername[i]);
else if (grmessage == 4)
printf("Why hello there %s!\n" , playername[i]);
else 
printf("Say hello to %s who has entered the game.\n" , playername[i]);

Now can someone tell me how to put the above block of code in a function

So essentially

...

void texter (char a) { my code above }

and then when I call the function again to have it print out the text

printf("\nEnter player %d name: ", i);

scanf ("%s", playername[i]);

texter (playername[i]);

I know I already gave the code literally but just need to fix it and make it work as that's what I would do if it was an integer

deXterlab97
  • 135
  • 8
  • I did not understand the requirement...can you elaborate a bit more? – Sourav Ghosh Nov 29 '16 at 05:28
  • Show your declaration for `playername`. Also consider using *line-oriented* functions to take user input (e.g. `fgets` or POSIX `getline`) rather than the `scanf` family. There are many hazards for new C programmers in `scanf`. It is fine to use if you fully-understand `man scanf`, otherwise, pay particular attention to what gets left in `stdin`. – David C. Rankin Nov 29 '16 at 05:37

3 Answers3

0

Just use a character array to store player's name (as you've already done). some thing like this:

char playername[no_of_players][100];

scanf ("%99s", playername[player_number]);  
//NOTE: %99s to avoid overwriting the terminating '\0' character

and you can send a string as parameter to function by using char* datatype

texter() function prototype:

void texter(char* name_sent) 
{
    //use name_sent to access the string sent into function 
}

function calling:

texter (playername[player_number]); 

Additionally, just use srand() once at the beginning your main() function, don't use it in the texter function. see this: srand() — why call it only once?

Community
  • 1
  • 1
Cherubim
  • 5,287
  • 3
  • 20
  • 37
0

you should use srand() before using rand() in order to get more random-like results. And

playername[i]

is a string not a char. So your function declaration should be

void texter (char* a) { }
Cherubim
  • 5,287
  • 3
  • 20
  • 37
cokceken
  • 2,068
  • 11
  • 22
0

Assume player name[i] is a character, when using scanf, format specifier should be %c, not %s.

Assume player name[i] is a string (this may be what you intend to do), then when writing void texter() function, the parameter should be char *playername or char player name[] not char.

Zhigang An
  • 296
  • 2
  • 13