1

This is my code, Im using codeblock

#include <stdio.h>

struct  engine {
    char name[100];
    int rpm;
    int hp;
    char manufacturer[100];
};

struct engine data;

int main()
{
    printf("Please insert engine information \n");
    printf("\nName: ");
    fgets(data.name, 101, stdin);
    printf("\nRPM:");
    scanf("%d",&data.rpm );
    printf("\nHorse Power:");
    scanf("%d",&data.hp);
    printf("\nManufacturer: ");
    fgets(data.manufacturer, 101, stdin);

    printf("\nParts information are as below\n");
    printf("Name:%s\n",data.name);
    printf("RPM:%d\n",data.rpm);
    printf("Horse Power:%d\n",data.hp);
    printf("Manufacturer:%s\n",data.manufacturer);

}

The pogram stopped after input the values for name, rpm and hp.

I can't seem to find whats the problem

*Edit This is the final working code after receiving help from Alter Mann

#include <stdio.h>

struct  engine {
char name[100];
int rpm;
int hp;
char manufacturer[100];
};

struct engine data;
static void flush_stdin(void)
{
int c;

while ((c = fgetc(stdin)) != '\n' && c != EOF);
}
int main()
{
printf("Please insert engine information \n");
printf("\nName: ");
fgets(data.name, sizeof(data.name), stdin);
printf("\nRPM:");
scanf("%d",&data.rpm );
printf("\nHorse Power:");
scanf("%d",&data.hp);
printf("\nManufacturer: ");
flush_stdin();
fgets(data.manufacturer, sizeof(data.manufacturer), stdin);

printf("\nParts information are as below\n");
printf("Name:%s",data.name);
printf("RPM:%d\n",data.rpm);
printf("Horse Power:%d\n",data.hp);
printf("Manufacturer:%s\n",data.manufacturer);

}
user1852728
  • 161
  • 2
  • 6
  • 13
  • 1
    Side note: The second argument of fgets() should be the size of the character buffer (and not one more). – Martin R Mar 15 '14 at 09:40
  • @MartinR the answer given there was to use "%*[^\n]%*c" at the end of the format, Could you pleasae explain where exactly should i put it. Is it inside the scanf such as scanf("%d%*[^\n]%*c",&data.hp); – user1852728 Mar 15 '14 at 09:54
  • For your last edit, nops, thats redundant, don't change your `scanf`'s if you are flushing with `flush_stdin` at the end – David Ranieri Mar 15 '14 at 10:08
  • Thanks, changed it back as before – user1852728 Mar 15 '14 at 10:14

2 Answers2

2

You need to consume (flush) newline before fgets:

static void flush_stdin(void)
{
    int c;

    while ((c = fgetc(stdin)) != '\n' && c != EOF);
}

int main(void)
{
    printf("Please insert engine information \n");
    printf("\nName: ");
    fgets(data.name, sizeof(data.name), stdin); // don't use magic numbers like 101
    printf("\nRPM:");
    scanf("%d",&data.rpm );
    printf("\nHorse Power:");
    scanf("%d",&data.hp);
    flush_stdin();
    printf("\nManufacturer: ");
    fgets(data.manufacturer, sizeof(data.manufacturer), stdin);
    ...
David Ranieri
  • 39,972
  • 7
  • 52
  • 94
1

fgets will receive '\n' as a character.

as @Alter Mann suggests,you can use

scanf("%d%*c",&data.hp);

to discard the following '\n'.

Or you can use scanf to receive your characters if it doesn't contain blank space,like

scanf("%s",&data.manufacturer);
drinking
  • 1,533
  • 3
  • 15
  • 22