Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

help please

tek-69tek-69 Member Posts: 5
I'm pretty new to programming, and i'm just playin around with c trying to get a feel for it. this is my source code:

// fun with c !!

#include

int main()
{

int num;
int num2;
char name;
printf("What's your name?
", name);
scanf("%s", &name);
printf("Hi %s.
", name);
printf("Pick a number:
", num);
scanf("%d", &num);
printf("Pick another number:
", num2);
scanf("%d", &num2);
printf("The 1st number was %d. The second number was %d.
", num, num2);
return (0);
}

what i want it to do is ask for a name, then say ok thename then ask for 2 numbers an spit em back out at me. When i compile i get no errors or warnings but when i try to run it, it asks for the name but when i give it a name and hit enter it simply says SEGMENTATION FAULT and exits. What am i doing wrong and how can i fix it ? any help would be greatly appreciated.
thanks,
tek

Comments

  • MT2002MT2002 Member Posts: 1,444
    : I'm pretty new to programming, and i'm just playin around with c trying to get a feel for it. this is my source code:
    :
    : // fun with c !!
    :
    : #include
    :
    : int main()
    : {
    :
    : int num;
    : int num2;
    : char name;
    : printf("What's your name?
    ", name);
    : scanf("%s", &name);
    : printf("Hi %s.
    ", name);
    : printf("Pick a number:
    ", num);
    : scanf("%d", &num);
    : printf("Pick another number:
    ", num2);
    : scanf("%d", &num2);
    : printf("The 1st number was %d. The second number was %d.
    ", num, num2);
    : return (0);
    : }
    :
    : what i want it to do is ask for a name, then say ok thename then ask for 2 numbers an spit em back out at me. When i compile i get no errors or warnings but when i try to run it, it asks for the name but when i give it a name and hit enter it simply says SEGMENTATION FAULT and exits. What am i doing wrong and how can i fix it ? any help would be greatly appreciated.
    : thanks,
    : tek
    :
    [BLUE] Here is your problem:[/BLUE][CODE]
    scanf("%s", &name);[/CODE]
    [BLUE]'scanf()' takes the address to hold the string.
    Unfortanly, 'name' is a char-it could only store
    a single character.

    To fix the problem, just declare 'name' as an array
    of characters:[/BLUE][CODE]
    char name[30];[/CODE][BLUE]
    This would be able to store up to 30 chars for the
    string.

    Good Luck!

    ~MT2002[/BLUE]
  • tek-69tek-69 Member Posts: 5
    Worked like a charm, thanks for explaining it, and not just telling me do this do that. i hope your around for the next problem :P
Sign In or Register to comment.