I am writing a program based on a prompt from a YouTube video. The program would take a list of one way flights and iterate through finding how many connecting flights it would take to get from LAX (a randomly chosen airport) from another airport, in this case I've chosen MCO.
So the program works by counting one ++ first when the second element in the first array is LAX, and then counting one ++ each time the first element of that first array matches the second element of another array. Then count one ++ for every time that happens.
My program build failed because of one error - "Invalid operands to binary expression ('std::string' (aka 'basic_string<char>') and 'int')
I know what the problem is but im new to c++ and I don't know how to fix it. Seems like a simple fix - "flights" is a string and I need an int ...but I do need a bit of help with that line. I added " //I KNOW THIS IS THE PROBLEMATIC PART" for the line that's troubling me.
here's my code, and thanks for the help!!!
So the program works by counting one ++ first when the second element in the first array is LAX, and then counting one ++ each time the first element of that first array matches the second element of another array. Then count one ++ for every time that happens.
My program build failed because of one error - "Invalid operands to binary expression ('std::string' (aka 'basic_string<char>') and 'int')
I know what the problem is but im new to c++ and I don't know how to fix it. Seems like a simple fix - "flights" is a string and I need an int ...but I do need a bit of help with that line. I added " //I KNOW THIS IS THE PROBLEMATIC PART" for the line that's troubling me.
here's my code, and thanks for the help!!!
Code:
//
// main.cpp
// Airline Connecting Flights
//
//
#include <iostream>
using namespace std;
int count = 0;
int i;
int j;
int match;
int main ()
{
//array of airports
std::string airports [15] = {"ATL", "BOS", "EWR", "DEN", "DFW", "IAH", "LGA", "JFK", "LAS", "LAX", "MCO", "ORD", "PHX", "SEA", "SLC"};
//array of flights
std::string flights [10] [2] = //keep in mind that the count starts at 0 for both horizontal and vertical
{
{"BOS", "LAX"}, // 4A
{"ATL", "EWR"},
{"DEN", "DFW"},
{"ORD", "LAX"}, // 1B
{"DEN", "BOS"}, // 3A
{"EWR", "DEN"}, // 2A
{"LGA", "JFK"},
{"MCO", "EWR"}, // 1A
{"LAS", "PHX"},
{"SLC", "SEA"},
};
int count = 0;
int i;
int match;
//function to calculate minimum number of flights from LAX to any other airport
for (i = 0; i < 11; i++)
{
if (flights [i] [1] == "LAX")
{
count++;
}
//i = match;
if (flights [i] [0] == match) //I KNOW THIS IS THE PROBLEMATIC PART
{
count++;
}
cout << count << endl;
}
}
Last edited: