0

Just starting to learn C/C++ in my CS 221 class.

Here's the start of my code:

int main()
{
    int id;
    char name[100];
    double wages;

    printf("Enter employee's id: ");
    scanf("%4i", id);
    printf("Enter employee's full name: ");
    scanf("%99[^\n]",name);
    printf("Enter gross salary for %s :",name);

    return 0;
}

Having trouble wrapping my head around char arrays, stdin and buffers in c. Why does the above code skip the user imputing a name? do I need fflush in there? couldn't really get that to work either.

1 Answers1

0

Not sure why you use

scanf("%4i", id);

But try:

scanf("%d", &id );

This fixes the issue of %4i format, and replaces it with %d for "Decimal integer" format.

This also fixes the issue of , id); because written like this, will not allow id to be stored, and would definitely crash.

If you have issues with numbers you are entering crashing from being too big, maybe check the boundaries of your data type int which will be platform specific.

Also change:

scanf("%99[^\n]",name);

to:

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

This takes the data entered by the user through scanf and interprets it as a "String of characters" and places it in name properly.

Burstful
  • 347
  • 1
  • 10