B_Haan
B_Haan

Reputation: 23

I want to be able to use scanf to type in a person's name and then have a sentence about that person come up individually

When i type in the code bellow, the program complies and runs but when i type in a person's name all of the printf functions for all names show up as the output. Any suggestions?

#include<stdio.h>

int main()

{ 

char *Name = malloc(sizeof(char)*200);
char *Carson = malloc(sizeof(char)*200);
char *David = malloc(sizeof(char)*200);
char *Avery = malloc(sizeof(char)*200);
char *Taylor = malloc(sizeof(char)*200);
char *Brian = malloc(sizeof(char)*200);


printf("Enter a name:\n");
scanf("%s\n",Name);

printf("%s Hello Carson\n", Carson);
printf("%s What are you looking at me for, I dont have any gum!!\n", David);
printf("%s Good luck with volleyball this weekend!!\n", Avery);
printf("%s Unauthorized user. Please back away!\n", Taylor);
printf("%s Hello user.\n", Brian);

return(0);
}

Upvotes: 2

Views: 107

Answers (1)

Laykker
Laykker

Reputation: 312

Actually, when you do

scanf("%s\n",Name);

you put the string from the standard input in "Name".

And when you do :

printf("%s Hello Carson\n", Carson);

you print the string "Carson" but there is nothing in it.

Try to do :

printf("%s Hello Carson\n", Name);

because you have already put value in "Name".

You can also read some book, this is very early learning c language.

Upvotes: 2

Related Questions