0

The program I have made has few issues.

Main issue is when I try to scan a string entry, program crashes. I can't understand where is the problem and I don't know how to solve it. Integer part works fine but string elements seems to have some problems.

How I can manage and fix that problem? I have checked several topics about it but I still could not understand, can someone show me by changing mistaken lines of my code?

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#pragma warning(disable:4996)

struct flight {
    int number;
    char source[20];
    char destination[20]; 
    struct flight* next;
};

void enter();
void display();
void delete();
int count();

typedef struct flight NODE;

NODE* head_node, * first_node, * temp_node = 0, * prev_node, next_node;
int data;
char data2[20], data3[20];
struct flight f[];

void enter()
{
    printf("\nEnter flight number: \n");
    scanf("%d", &data);
    printf("\nEnter flight source: \n");
    scanf(" %s", &data2);
    printf("\nEnter flight destination: \n");
    scanf(" %s", &data3);


    temp_node = (NODE*)malloc(sizeof(NODE));

    temp_node->number = data;
    temp_node->source = data2[20];
    temp_node->destination = data3[20];


    if (first_node == 0)
    {
        first_node = temp_node;
    }
    else
    {
        head_node->next = temp_node;
    }
    temp_node->next = 0;
    head_node = temp_node;
    fflush(stdin);
}
Ardent Coder
  • 3,777
  • 9
  • 27
  • 53
R-Gate
  • 17
  • 5

1 Answers1

1

This is not the way to copy strings in C:

temp_node->source = data2[20];
temp_node->destination = data3[20];

You can use strcpy for this purpose. Also make sure that data2 and data3 are properly initialized, your code has a lot of uninitialized variables.

strcpy(temp_node->source, data2);
strcpy(temp_node->destination, data3);

The way you are reading strings:

scanf(" %s", &data2);
scanf(" %s", &data3);

is a bit unsafe. You can specify the length in the format specifier and also avoid & (more details in anastaciu's comment):

scanf(" %19s", data2);
scanf(" %19s", data3);

And note that fflush(stdin) is UB, you might want to think about it unless you're sure of how your compiler handles it.

Aside: struct flight f[]; you did not specify the array size.

Ardent Coder
  • 3,777
  • 9
  • 27
  • 53
  • Yes that's what exactly I have done, thanks a lot everyone – R-Gate May 24 '20 at 22:26
  • What should I use instead of fflush(stdin) then? – R-Gate May 24 '20 at 22:27
  • You can refer this: https://stackoverflow.com/questions/7898215/how-to-clear-input-buffer-in-c – Ardent Coder May 24 '20 at 22:28
  • 1
    @R-Gate, `scanf(" %s", &data2);` should be `scanf(" %19s", data2);`, the same with `data3`. No `address-of` operator needed and limit the size of the scanned string to it's container -1 to reserve space for a null terminator. – anastaciu May 24 '20 at 22:31
  • even with fflush, program works normal. it seems right at the moment, isn't it? – R-Gate May 24 '20 at 22:31
  • 1
    @anastaciu See the initial comments, my answer was only intended for that purpose. Feel free to edit it :) – Ardent Coder May 24 '20 at 22:31
  • @R-Gate Maybe it's fine in your implementation, but it's a good practice to avoid writing non-portable code :) – Ardent Coder May 24 '20 at 22:32
  • 1
    @ArdentCoder, I was warning the OP of the problems with `scanf`, I did it in your answer because there are many comments already in the question and the OP might not see it, I would prefer if you add it yourself if you want, because it's not a minor edit and you would now best how to organize your answer. – anastaciu May 24 '20 at 22:39