The program ends automatically

Hello. Im new to programming. I need help because when i run this and type a name with 2 or more character it automatically ends.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  int main(int argc, char *argv[])
{
    
char name;
p("Enter your name:");
s("%c",&name);

int year;
p("Enter year of birth:");
s("%d",&year);
int age;
age = 2013 - year;

p("You are %d years old %c.\n",age ,name);
    system("PAUSE");
    return EXIT_SUCCESS;
}
Use %s instead of %c as you will be inputing a string not a single character.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main(int argc, char *argv[])
{
    
char name;
p("Enter your name:");
s("%s",&name);

int year;
p("Enter year of birth:");
s("%d",&year);
int age;
age = 2013 - year;

p("You are %d years old %s.\n",age ,name);
    system("PAUSE");
    return EXIT_SUCCESS;
}


This should do.
Last edited on
i replace %c with %s as you mentioned. I tried to run it and this is the result:

http://s7.postimg.org/60olyqujf/123213123213.jpg
Did you try changing name to a char* as well?
http://postimg.org/image/ufuy0ur8h/


but when i enter second name or surname this is the result

http://postimg.org/image/cr3798fhd/


so confusing
Note that I have little knowledge in C, so don't take my opinion seriously.

The scanf function (is that what you are using?) might only read up to spaces similar to the C++ cin >> foo instruction. If that's the case, it threw "Doe" for the next scanf call, which tried to insert it to your int variable. That's probably why it failed.

I found a couple solutions here:
http://cboard.cprogramming.com/c-programming/23907-scanf-spaces.html
http://stackoverflow.com/questions/1247989/how-do-you-allow-spaces-to-be-entered-using-scanf

They both involve using fgets instead of scanf.
I used fgets() and this is the result:
http://postimg.org/image/li1mu0pjf/


I tried using scanf("%[^\n]". But it confuses me and fgets() is much easier.


@Protostar
@Daleth

Thank you for your help.
Topic archived. No new replies allowed.