Question: C + + code only need help with C + + code please I need help in fixing my code because I cannot get the

C++ code only
need help with C++ code please
I need help in fixing my code because I cannot get the right output for flight 2 and 3 arrival time
This is my code
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
// Structure to hold time in hours and minutes
struct Time {
int hour;
int minute;
};
// Function to parse a time string and convert it to the Time structure
Time parseTime(const string& timeString){
Time t;
istringstream iss(timeString.substr(0,5));
char discard;
iss >> t.hour >> discard >> t.minute;
if (timeString.substr(6)=="P.M." && t.hour !=12){
t.hour +=12; // Convert PM time to 24-hour format
}
else if (timeString.substr(6)=="A.M." && t.hour ==12){
t.hour =0; // Midnight is 0 in 24-hour format
}
return t;
}
// Function to format Time structure back into a human-readable string
string formatTime(const Time& time){
int hour = time.hour %12==0?12 : time.hour %12; // Convert 24-hour time to 12-hour format
string amPm = time.hour >=12?"P.M." : "A.M.";
ostringstream oss;
oss hour ":" setfill('0') setw(2) time.minute "" amPm;
return oss.str();
}
// Function to add minutes to a time, adjusting hours and wrapping around if necessary
Time addMinutes(const Time& time, int mins){
Time newTime = time;
newTime.minute += mins;
newTime.hour += newTime.minute /60;
newTime.minute %=60;
newTime.hour %=24;
return newTime;
}
// Calculate the arrival time considering flight duration and time zone differences
Time calculateArrivalTime(const Time& departureTime, int flightDurationMinutes, int timeZoneDifference){
Time arrivalTime = addMinutes(departureTime, flightDurationMinutes); // Add flight duration to departure time
arrivalTime.hour += timeZoneDifference; // Adjust for the time zone difference
if (arrivalTime.hour >=24){
arrivalTime.hour -=24; // Wrap around if hour exceeds 24
}
else if (arrivalTime.hour 0){
arrivalTime.hour +=24; // Adjust for negative hour values
}
return arrivalTime;
}
// Helper function to split strings based on a given delimiter
vector split(const string& s, char delimiter){
vector tokens;
string token;
istringstream tokenStream(s);
while (getline(tokenStream, token, delimiter)){
tokens.push_back(token);
}
return tokens;
}
int main(){
map timeZoneOffsets ={
{"Charlotte",-5},
{"Orlando",-5},
{"Houston",-6},
{"Denver",-7},
{"San Diego", -8}
};
map, int> flightTimes ={
{{"San Diego", "Denver"},119},
{{"San Diego", "Houston"},174},
{{"Denver", "Houston"},125},
{{"Houston", "Charlotte"},130},
{{"Houston", "Orlando"},121},
{{"San Diego", "Charlotte"},263},
{{"Charlotte", "Orlando"},89}
};
string fileName;
cout "Enter file name: ";
getline(cin, fileName);
ifstream file(fileName);
if (!file.is_open()){
cout "File not found." endl;
return 1;
}
int numFlights;
file >> numFlights;
file.ignore(numeric_limits::max(),'
'); // Skip to the next line after reading number of flights
for (int i =1; i = numFlights; i++){
string line;
getline(file, line);
auto parts = split(line,'');
string departureCity = parts[0];
string timeString = parts[1]+""+ parts[2];
string destinationCity = parts[3];
if (parts.size()>4){// Check if city name consists of two parts like "San Diego"
destinationCity +=""+ parts[4];
}
Time departureTime = parseTime(timeString);
int flightTime = flightTimes[{departureCity, destinationCity}];
int timeZoneDifference = timeZoneOffsets[destinationCity]- timeZoneOffsets[departureCity];
Time arrivalTime = calculateArrivalTime(departureTime, flightTime, timeZoneDifference);
cout "Flight " i ": " departureCity "" formatTime(departureTime)
"" destinationCity "" formatTime(arrivalTime) endl;
}
file.close();
return 0;
}
This is the output that I got from my code
Enter file name: airtimes.txt
Flight 1: Houston 6:00 A.M. Orlando 9:01 A.M.
Flight 2: Orlando 1:25 A.M. Charlotte 1:25 A.M.( need to get 2:54 P.M. here for flight 2 arrival)
Flight 3: Charlotte 7:30 A.M. San Diego 4:30 A.M.( need to get 8:53 A.M. here for flight 3 arrival)
C + + code only need help with C + + code please

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Finance Questions!