-1

the following code can be run properly in compiler but there are some minor error which i cant identify out. it can only input the first automobile detail but when comes to the second record it jump to the second input statement instead the first one.please point out where are the correction and thank you for everything.

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

struct auto_t
{
    char make[20];
    char model[20];
    int oRead;
    int manufDate;
    int purchDate;
    float cpct;
    float f_level;
};

int main(int argc, char** argv)
{
    struct auto_t automobile[2];
    int i;
    for(i=0; i<2; i++)
    {
        printf("component:");
        gets(automobile[i].make);
        printf("model:");
        gets(automobile[i].model);
        printf("Odoer reading:");
        scanf_s("%d", &automobile[i].oRead);
        printf("manufacturing date(ddmmyyyy):");
        scanf_s("%d", &automobile[i].manufDate);
        printf("purchasing date(ddmmyyyy):");
        scanf_s("%d", &automobile[i].purchDate);
        printf("capacity:");
        scanf_s("%f", &automobile[i].cpct);
        printf("fuel level:");
        scanf_s("%f", &automobile[i].f_level);
        printf("\n");
    }
    printf("\nName\tModel\tOReaad\tManufDate\tPurchDate\tCpct\tFuel\n");
    for(i=0; i<2; i++)
    {
        printf("%s\t%s\t%d\t%d\t%d\t%.f\t%.f\n", automobile[i].make, automobile[i].model, automobile[i].oRead, automobile[i].manufDate, automobile[i].purchDate, automobile[i].cpct, automobile[i].f_level);
    }
    system("pause");
}
  • 2
    ***Do NOT use `gets()`!*** It's dangerous and non-standard. Use `fgets()` instead. And use a debugger as well. – The Paramagnetic Croissant Nov 16 '14 at 07:00
  • compiler show error when using fgets(). is there any other option instead using gets() or fgets(). thank you for the info The Paramagnetic Croissant – Goh Kian Hwa Nov 16 '14 at 07:03
  • 1
    "compiler show error when using fgets" is not helpful. You need to tell us **what error specifically.** Also, `fgets()` is standard and secure, so please use it. – The Paramagnetic Croissant Nov 16 '14 at 07:06
  • the message is 'too few arguments for call' when using fgets() – Goh Kian Hwa Nov 16 '14 at 07:19
  • 1
    And doesn't that ring a bell? I mean, just **read and interpret the error message,** and read the documentation of `fgets()`. – The Paramagnetic Croissant Nov 16 '14 at 07:20
  • The main problem is that the `scanf_s("%f", &automobil[i].f_level);` statement leaves the newline in the input buffer, so the next time around the loop, the first `gets()` — which others have correctly chastised you for using; we should really be chastising your teacher more — the first `gets()` reads the newline left by the `scanf_s()`, throwing all your inputs off. This is a common problem. You might do best to read the line with `fgets()` and then use `sscanf()` to parse the float value (and similarly with the other numeric values), or you may prefer to read characters to the newline. – Jonathan Leffler Nov 16 '14 at 07:44
  • Cool Guy and The Paramagnetic Croissant, thank you for the info and the changes. But there's still have the problem where i cannot input the second details after finishing inputting the first detail. – Goh Kian Hwa Nov 16 '14 at 07:52

1 Answers1

0

Never use gets() as it is dangerous. Use fgets() instead. So change

printf("component:");
gets(automobile[i].make);
printf("model:");
gets(automobile[i].model);

to

printf("component:");
fgets(automobile[i].make,20,stdin);
printf("model:");
fgets(automobile[i].model,20,stdin);

or to

printf("component:");
fgets(automobile[i].make,sizeof(automobile[i].make),stdin);
printf("model:");
fgets(automobile[i].model,sizeof(automobile[i].model),stdin);
Community
  • 1
  • 1
Spikatrix
  • 20,225
  • 7
  • 37
  • 83