-1

I was writing code to prints the details of three drivers but the problem is the program takes the input of driver 1 correctly but skips the input for name for driver 2 and 3 ,also despite providing the input it prints the distance travelled as 0.0.

#include<stdio.h>
struct data
{
    char name;
    int dlo;
    char route;
    float km;
}s1,s2,s3;
void details(struct data *d)
{
    printf("Enter your name: ");
    scanf(" %s",&d->name);
    printf("Enter your driving license no: ");
    scanf("%d",&d->dlo);
    printf("Enter route taken: ");
    scanf("%s",&d->route);
    printf("Enter distance travelled: \n");
    scanf("%.2f",&d->km);
}
void output(struct data *d)
{
    printf("Driver name: %s\n", &d->name);
    printf("Driving license no: %d \n", d->dlo);
    printf("Route taken: %s\n", &d->route);
    printf("Distance travelled: %.2f \n", d->km);
}
int main()
{
        printf("\n--------------driver 1---------------\n");
        details(&s1);
        printf("\n");
        output(&s1);
        printf("\n--------------driver 2---------------\n");
        details(&s2);
        printf("\n");
        output(&s2);
        printf("\n--------------driver 3---------------\n");
        details(&s3);
        printf("\n");
        output(&s3);
    return 0;
}

the output comes likes this:

--------------driver 1---------------
Enter your name: abhishek
Enter your driving licence no: 123
Enter route taken: abcd
Enter distance travelled: 
123

Driver name: abhishek
Driving licence no: 123
Route taken: abcd
Distance travelled: 0.00

--------------driver 2---------------
Enter your name: Enter your driving licence no: 321
Enter route taken: efgh
Enter distance travelled: 
45 

Driver name: 123
Driving licence no: 321
Route taken: efgh
Distance travelled: 0.00
user438383
  • 5,716
  • 8
  • 28
  • 43
  • `char name;` can store 1 character, like `'A'`, but not `"Abhishek"`. Change it to `char name[50];` and remove the `&` in front of `d->name` at the `scanf` and `printf` – mch Nov 22 '21 at 09:00
  • I have shown how the outputs comes in the question please go through that – Abhishek Ojha Nov 22 '21 at 09:27
  • Actually `char name` cannot even store a single character if you intend to use that as string, as you need room for the terminating null character – which in this case would occupy the only available character place... – Aconcagua Nov 22 '21 at 09:33
  • The problem is that you actually invoke *undefined behaviour*. You write beyond array bounds (you can consider `char name` equivalent to an array of length 1), so *anything* can happen, even (by accicent!) outputting the expected result. – Aconcagua Nov 22 '21 at 09:34
  • You need to, as @mch already mentioned, provide an array of sufficient length, though I personally would prefer powers of two (32, 64, ...). What you should then do, too, is providing the array length to the format string such that `scanf` won't write beyond array bounds either due to too long user input, so (assuming 50 as proposed by mch): `scanf("%49s")`. Be aware that you need to provide one character less than array length to leave space for the terminating null character! – Aconcagua Nov 22 '21 at 09:38
  • The same, of course, applies to `route` as well. – Aconcagua Nov 22 '21 at 09:40
  • Off-topic: While `float` appears convenient at a first glance it can render pretty problematic later on due to rounding issues. Be aware that even such a simple number like `0.1` cannot be represented exactly in binary as it is periodic there. You'll encounter problems like [comparing floats](https://stackoverflow.com/questions/17333/what-is-the-most-effective-way-for-float-and-double-comparison) (note its extension on [relative epsilon](https://stackoverflow.com/a/67843030/1312382), too). – Aconcagua Nov 22 '21 at 09:47
  • You get around all that trouble if you store the values in integral types (`int`, `long`, `long long` – or in this case preferably their unsigned counter parts as negative distances appear meaningless anyway), if appropriate using sub-units of the actually desired unit (in this case meters instead of kilometers). Parsing input gets more difficult, sure (if you still want to input kilometers – and outputting, too, but only minimally), but that pays back later on. – Aconcagua Nov 22 '21 at 09:49
  • i did as you advised but the problem remains the same ,`Enter your name: Enter your driving licence no:` is being printed – Abhishek Ojha Nov 22 '21 at 09:52
  • the problem is solved I just had to change `%.2f` to `%f`, but I don't understand how `%.2f` affected my code – Abhishek Ojha Nov 22 '21 at 09:56
  • @AbhishekOjha if you solved your issue, please provide an answer and accept it using the green tick. Thank you. – user438383 Nov 22 '21 at 10:01
  • @user438383 I am still new here can you tell me how to do that – Abhishek Ojha Nov 22 '21 at 10:11
  • @AbhishekOjha The period is not valid for [`scanf`](https://en.cppreference.com/w/cpp/io/c/fscanf) (differing from `printf` in this respect) – not sure how your implementation of `scanf` did handle that. Any number following the `%` character defines how many characters are legal to be consumed, so assuming the period simply was ignored only two characters would have been read then (i. e. only `10` from `100.5`) – though it rather seems as if your implementation did not read in anything at all. – Aconcagua Nov 22 '21 at 10:17
  • Oh, and even if just replacing "%.2f" with "%f" seemed to fix the issue – please *do* consider the single char issue commented before – while seemingly working you still rely on undefined behaviour, so the code seeming to working now might fail at any time later (when you'll least expect it), when using a different compiler or on another machine. – Aconcagua Nov 22 '21 at 10:21
  • 1
    @Aconcagua I will make sure to follow your advice from now on, thank you for your advice – Abhishek Ojha Nov 22 '21 at 10:24

1 Answers1

1

The problem was that I used %.2f in the scanf which resulted in a Run-Time Error thus by changing the scanf("%.2f",&d->km); to scanf("%f",&d->km); the error was solved also I had to use array for name and route to avoid further Run-Time Error.

I really appreciate everyone for their advice.