Hi I'm almost done with a project I have for class. I need to sort the priority of people in an airline based on a few factors. This is my project description:
"An airline company uses the formula shown below to determine the priority of passengers on the waiting list for overbooked flights. Priority Number = (A / 1000) + B - C where A is the customer’s total mileage in the past year B is the number of years in his or her frequent flier program C is a sequence number representing the customer’s arrival position when he or she booked the flight. Given a file of overbooked customers as shown in the table below, write a program that reads the file and determines each customer’s priority number. The program then builds a priority queue using the priority number and prints a list of waiting customers in priority sequence."
I feel my code encompasses the main idea, but I am having issues with getline. When I compile the code (shown below) it gives me an error: "|24|warning: implicit declaration of function 'getline' [-Wimplicit-function-declaration]|"
Please help me fix this so it will compile. I've tried a lot of things and nothing works.
Here is the code:
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
FILE * fp; //create file pointer to be read later
char * line = NULL;
int temp,count=0,len=0,mileage[100],read,years[100],sequence[100],priority[100];
char name[100][100],tempst[100];
fp = fopen("customers.txt", "r"); //opening the file
if (fp == NULL)
exit(EXIT_FAILURE);
int i;
while ((read = getline(&line, &len, fp)) != -1) { //start reading file and recording data
i=0;
while(line[i]!=' ' || (line[i+1]>='A' && line[i+1]<='z'))
{
name[count][i]=line[i];
i++;
}
name[count][i++]='\0';
mileage[count]=0;
while(line[i]!=' ')
{
mileage[count]=mileage[count]*10+(line[i]-'0');
i++;
}
i++;
years[count]=0;
while(line[i]!=' ')
{
years[count]=years[count]*10+(line[i]-'0');
i++;
}
i++;
sequence[count]=0;
while(line[i]!='\0')
{
sequence[count]=sequence[count]*10+(line[i]-'0');
i++;
}
priority[count]=(mileage[count]/1000)+years[count]-sequence[count];
count++;
}
for( i=0;i<count-1;i++) // calculate priority
{
for(int j=i+1;j<count;j++)
{
if(priority[i]<priority[j])
{
temp=priority[i];
priority[i]=priority[j];
priority[j]=temp;
strcpy(tempst,name[i]);
strcpy(name[i],name[j]);
strcpy(name[j],tempst);
}
}
}
for( i=0;i<count;i++) //print priority
{
printf("%s %d\n",name[i],priority[i]);
}
return 0;
}
#define _GNU_SOURCE
#include <stdio.h>
or
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
should get you getline
on GNU systems, but there might be another way to enable it on a different POSIX system.
After enabling getline
like that, the len
variable should be typed size_t
, not int
so that &len
is correctly typed as size_t *
.
It is possible that your project testing software doesn't support the getline
POSIX function in which case you might need to rethink your design in terms of standard C functions instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With