#include<iostream.h>
#include<conio.h>
#include<stdio.h>
struct FLIGHT
{
int Flno;
char DepTime[20];
char ArrTime[20];
float Fare;
};
void Enter(FLIGHT [], int);
void Display(FLIGHT [], int);
void LowestFareFlight(FLIGHT [], int);
void main()
{
clrscr();
int n;
FLIGHT A[20];
do
{
cout<<"Enter the number of Flight entries you'd be entering <20: ";
cin>>n;
}while(n>20);
cout<<endl<<endl;
Enter(A,n);
cout<<endl<<endl;
Display(A,n);
cout<<endl<<endl;
LowestFareFlight(A,n);
getch();
}
void Enter(FLIGHT A[], int N)
{
for(int i=0; i<N; i++)
{
cout<<"Enter details of flight "<<i+1<<endl;
cout<<"Enter Flight Number: ";
cin>>A[i].Flno;
cout<<"Enter Departure Time: ";
gets(A[i].DepTime);
cout<<"Enter Arrival Time: ";
gets(A[i].ArrTime);
cout<<"Enter flight fare: $";
cin>>A[i].Fare;
cout<<endl<<endl;
}
}
void Display(FLIGHT A[], int N)
{
for(int i=0; i<N; i++)
{
cout<<"The details of flight "<<i+1<<" are:"<<endl;
cout<<"Flight Number: "<<A[i].Flno<<endl;
cout<<"Departure Time: "<<A[i].DepTime<<endl;
cout<<"Arrival Time: "<<A[i].ArrTime<<endl;
cout<<"Fare: "<<A[i].Fare<<endl;
cout<<endl<<endl;
}
}
void LowestFareFlight(FLIGHT A[], int N)
{
for(int i=0; i<N-1; i++)
{
for(int j=0; j<(N-i-1); j++)
if(A[j].Fare>A[j+1].Fare)
{
FLIGHT Temp;
Temp=A[j];
A[j]=A[j+1];
A[j+1]=Temp;
}
}
cout<<"The the details of the flight with the lowest fare are:"<<endl<<endl;
cout<<"Flight Number: "<<A[0].Flno<<endl;
cout<<"Departure Time: "<<A[0].DepTime<<endl;
cout<<"Arrival Time: "<<A[0].ArrTime<<endl;
cout<<"Fare: "<<A[0].Fare<<endl;
}
The Output comes like:
Enter details of flight 1
Enter Flight Number: A380
Enter Departure Time: 10:00 PM
Enter Arrival Time: 6:00 AM
Enter flight fare: $
Enter details of flight 2
Enter Flight Number: Enter Departure Time: 7:00 AM
Enter Arrival Time: 6:00 PM
Enter flight fare: $
Enter details of flight 3
Enter Flight Number: Enter Departure Time: 5:00 AM
Enter Arrival Time: 2:00 PM
Enter flight fare: $
The details of flight 1 are:
Flight Number: 0
Departure Time: 10:00 PM
Arrival Time: 6:00 AM
Fare: 0
The details of flight 2 are:
Flight Number: 3123
Departure Time: 7:00 AM
Arrival Time: 6:00 PM
Fare: 322
The details of flight 3 are:
Flight Number: 13213
Departure Time: 5:00 AM
Arrival Time: 2:00 PM
Fare: 1313
The the details of the flight with the lowest fare are:
Flight Number: 0
Departure Time: 10:00 PM
Arrival Time: 6:00 AM
Fare: 0
Why is this? I have many questions, like why doesn't the program stop for the user when user is entering fare of flight 1? It just instantaneously comes to "Enter details of flight 2" without even stopping for the user to enter the value in Fare of flight 1. The same happens all throughout when the user has to enter fare (user would have to enter the fare 3 times since he has entered 3 in the starting of the output)..Also this happened exclusively for the first time..but about the time when the user is about to enter the details of the second flight (the first flight fare error has occurred till now), the user now experiences another error (in addition to not being able to enter fare value) as follows:
(a) The user is not able to enter the value of Flight Number any more.
Also another error is experienced when the the Display() function is being executed. That how come the Display() function display the Fare values as 0, 322, 1313 when the user couldn't enter the values in the starting? Need help! I think guessed some part of the error that if the user enter A380 as the flight number it would result in some error since flight number should be an integer value and this involves A also..but then would the ASCII value of A be taken to omit the error? OR the error would occur anyhow?
int Flno;
/*...*/
cout<<"Enter Flight Number: ";
cin>>A[i].Flno; //You enter A380
Well, how you think you letter A will be stored in integer variable? This operation sets stream into failed state and all other operations are going wrong after that.
It might looks like everything are going to be fixed after that, but it is an illusion created by use outdated library <conio.h> which actually wrecks things even more. Do not use it. Forget about it ad let it rot.
Use:
1 2 3 4
std::string DepTime[20]; //Do not use c-strings unless you got a
std::string ArrTime[20]; //PERFECTLY good reason to do so.
//and
std::getline(std::cin, A[i].DepTime)
Also it is <cstdio> in C++. <stdio.h> is outdated and isn't supported.
And <iostream>, not <iostream.h>
Thanks Miinipaa but this is for my school assignment and I can't help it then because in school they still go with those outdated ways of c++ ..can you post the updated code with all the amendments you talked about? thanks :)