B_Haan
B_Haan

Reputation: 23

How can i use scanf to plug in names and receive sentences as outputs?

I cant figure out what is wrong with my code. I want to be able to type in any of those names as the scanf function and then the words should come up that i have in the printf functions (the error occurs when i try to type in a name):

#include <stdio.h> 
#include <math.h>  

int main()  
{  

    char *Name; 
    char *Carson; 
    char *Kam; 
    char *David; 
    char *Avery; 
    char *Taylor; 
    char *Brian;  

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

    printf("%s Hello Carson\n", Carson); 
    printf("%s Whats up?\n?", Kam); 
    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: 0

Views: 37

Answers (1)

herohuyongtao
herohuyongtao

Reputation: 50667

You need to change

char *Name;

to

char Name[100];

or use malloc to allocate memory for char *Name first before reading data to it.

And it seems you didn't initialize Carson/Kam/... before print them out. Try to fix them too.

Upvotes: 1

Related Questions