C++ syntax help

Thread Starter

arduinolego611

Joined Jan 23, 2022
75
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!!!

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:

twohats

Joined Oct 28, 2015
614
Hi,
I'm guessing; if (flights [ i ] [0] == match) . Do you need an" and / or ", in between i 0 ?
You have misspelt count on line 56
Good luck.......... Edit
 

WBahn

Joined Mar 31, 2012
32,823
Your code has several issues.

What is 'match' supposed to be? You never even initialize it.

You declare two sets of variables, {count, i, match}. One set is global (declared outside any function) and tthe other is local to main.

You are doing comparisons between string pointers, not strings. This will only be true if the two pointers point at the exact same string.

In addition, your basic logic doesn't appear to match what you are trying to accomplish, and your basic logic doesn't appear to match what your code is trying to do.
 

WBahn

Joined Mar 31, 2012
32,823
Hi,
I'm guessing; if (flights [ i ] [0] == match) . Do you need an" and / or ", in between i 0 ?
You have misspelt count on line 56
Good luck.......... Edit
He spelled 'count' the same way on line 56 as he does everywhere else (which is what really, errr, counts, no pun intended).
 

MrChips

Joined Oct 2, 2009
34,809
Hi,
I'm guessing; if (flights [ i ] [0] == match) . Do you need an" and / or ", in between i 0 ?
You have misspelt count on line 56
Good luck.......... Edit
He spelled 'count' the same way on line 56 as he does everywhere else (which is what really, errr, counts, no pun intended).
cout is spelled correctly.
cout stands for "character output" using <iostream> standard console input/output.
 

Thread Starter

arduinolego611

Joined Jan 23, 2022
75
Your code has several issues.

What is 'match' supposed to be? You never even initialize it.

You declare two sets of variables, {count, i, match}. One set is global (declared outside any function) and tthe other is local to main.

You are doing comparisons between string pointers, not strings. This will only be true if the two pointers point at the exact same string.

In addition, your basic logic doesn't appear to match what you are trying to accomplish, and your basic logic doesn't appear to match what your code is trying to do.
I just want to count the minimum number of flights it would take to get from MCO to LAX...I may have written that part wrong in my description. it can be a little confusing... but im pretty sure the program does follow my purpose...its just that I don't know how to check if that pointer in line 51 matches "match" (which should be the destination of the previous "match") because one is a string and the other a pointer.

I think I commented the I = num part of it out and forgot to switch it back...
 

WBahn

Joined Mar 31, 2012
32,823
I just want to count the minimum number of flights it would take to get from MCO to LAX...I may have written that part wrong in my description. it can be a little confusing... but im pretty sure the program does follow my purpose...its just that I don't know how to check if that pointer in line 51 matches "match" (which should be the destination of the previous "match") because one is a string and the other a pointer.

I think I commented the I = num part of it out and forgot to switch it back...
If I gave you a list of flights such as the one you have, and I only let you make a single pass through that list, and only let you maintain/update two pieces of information. Would you really be able to determine, by the end of the one pass through the list, the minimum number of flights (which is not that same as the minimum number of "connecting" flights, which is what you said you were looking for)?

If, by "I = num part" you are referring to the line

i = match;

that doesn't change anything. What value does match have in it at that point? You never initialize it. Why do you want to change the value stored in i?

What is the purpose of

Code:
if (flights  [i][1] == "LAX")
{
   count++;
}
What does this actually do? Not what you want it to do, but what it actually does.

Let's say that I have a list of 10,000 flights, 1,000 of which start at some airport and end at LAX. You are going to count them all up. Why? What does the total number of flights that come from arbitrary airports, only one of which the person is going to actually use to get from MCO to LAX, have to do with the minimum number of flights to get from MCO to LAX?

Instead of jumping right in and throwing a bunch of code at the problem, take out a pencil and a sheet of paper and very carefully track how YOU would use that list to determine the minimum number of flights.
 

Thread Starter

arduinolego611

Joined Jan 23, 2022
75
If I gave you a list of flights such as the one you have, and I only let you make a single pass through that list, and only let you maintain/update two pieces of information. Would you really be able to determine, by the end of the one pass through the list, the minimum number of flights (which is not that same as the minimum number of "connecting" flights, which is what you said you were looking for)?

If, by "I = num part" you are referring to the line

i = match;

that doesn't change anything. What value does match have in it at that point? You never initialize it. Why do you want to change the value stored in i?

What is the purpose of

Code:
if (flights  [i][1] == "LAX")
{
   count++;
}
What does this actually do? Not what you want it to do, but what it actually does.

Let's say that I have a list of 10,000 flights, 1,000 of which start at some airport and end at LAX. You are going to count them all up. Why? What does the total number of flights that come from arbitrary airports, only one of which the person is going to actually use to get from MCO to LAX, have to do with the minimum number of flights to get from MCO to LAX?

Instead of jumping right in and throwing a bunch of code at the problem, take out a pencil and a sheet of paper and very carefully track how YOU would use that list to determine the minimum number of flights.
that's not what im trying to do, im trying to count the number of flights it would take to get from MCO to LAX. it should be 4. I know that, im trying to get the program to trace from MCO to EWR, then EWR to DEN, then DEN to BOS, then BOS to LAX

...(but backwards from LAX )
 
Last edited:

WBahn

Joined Mar 31, 2012
32,823
that's not what im trying to do, im trying to count the number of flights it would take to get from MCO to LAX. it should be 4. I know that, im trying to get the program to trace from MCO to EWR, then EWR to DEN, then DEN to BOS, then BOS to LAX

...(but backwards from LAX )
I know that's not what you are trying to do. But that is what you ARE doing. Hence, as I said, your program doesn't match what you are trying to do. Similarly, the logic you describe isn't going to accomplish what you want.

Here is the algorithm as you presented it: " 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. "

What do you want to have happen if you have five entries that have LAX as the second element? Both your description above and your code increment your counter for all five. But does that do anything to solve your problem?

Then your description says that you will increment your counter again every time the second element of an entry matches whatever the first element was any entry that had LAX as its second element. That is NOT what your program is doing, AND it is not going to help you solve your problem.

So, again, I recommend that you forget about programs and programming languages and syntax and focus on the algorithm, because the algorithm you currently have, as you've described it, won't come close to doing what you want.

Consider the following example (so that we have data you haven't seen before). As with your data, each pair means that there is a flight from the first element to the second element.

Code:
A,B
B,H
H,E
A,D
C,F
D,C
C,E
F,G
E,F
G,E
E,A
D,G
What is the smallest total number of flights to get from B to F?

Work this under the same constraints that your program will have to. You only get to look at one entry in the table at a time and you can't bounce around. You start at the top and go to the end. Then, if you need to, you start at the top and go to the end again. And again. However many times it takes. You can keep track of whatever information off to the side you want, but any changes you make to it has to be based on what is already there and whatever entry from the flight schedule that you are looking at right now. You can't step back and let your mind scan up and down the table to pick things out. You, as a human, can do that very easily. Your program can't, so force yourself to operate at the level of a program.

There are many algorithms that solve this problem, some are more efficient that others while some are easier to understand and implement than others. Focus on developing one that is easy to understand and implement. Don't worry about efficiency, at least not at first.

Here's a hint to get you started. Before you even look at the table, what is the worst case smallest number of flights that you know from each city to F?

Since you don't know if there is ANY route from ANY city (other than F) to F, the answer could be as high as infinity -- i.e., you can't get there from here. In the case of getting from F to F, the answer is trivial -- it takes zero flights.

So we have the following lists of the number of flights to get from each city to F.

Code:
A: ∞
B: ∞
C: ∞
D: ∞
E: ∞
F: 0
G: ∞
H: ∞
Now go through the list of flights, one entry at a time, and see if there are any changes you can make to this table based on that entry. What does the table look like after making one pass through the list of flights?

What changes can you make on the second pass through the flight list?

What's the worst-case number of passes you need to make in order to have a complete table of the minimum number of flights from any city to F (keeping in mind that the answer for some of them might still be infinity, so you can't just go until all of the infinity entries go away)?
 

Thread Starter

arduinolego611

Joined Jan 23, 2022
75
I know that's not what you are trying to do. But that is what you ARE doing. Hence, as I said, your program doesn't match what you are trying to do. Similarly, the logic you describe isn't going to accomplish what you want.

Here is the algorithm as you presented it: " 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. "

What do you want to have happen if you have five entries that have LAX as the second element? Both your description above and your code increment your counter for all five. But does that do anything to solve your problem?

Then your description says that you will increment your counter again every time the second element of an entry matches whatever the first element was any entry that had LAX as its second element. That is NOT what your program is doing, AND it is not going to help you solve your problem.

So, again, I recommend that you forget about programs and programming languages and syntax and focus on the algorithm, because the algorithm you currently have, as you've described it, won't come close to doing what you want.

Consider the following example (so that we have data you haven't seen before). As with your data, each pair means that there is a flight from the first element to the second element.

Code:
A,B
B,H
H,E
A,D
C,F
D,C
C,E
F,G
E,F
G,E
E,A
D,G
What is the smallest total number of flights to get from B to F?

Work this under the same constraints that your program will have to. You only get to look at one entry in the table at a time and you can't bounce around. You start at the top and go to the end. Then, if you need to, you start at the top and go to the end again. And again. However many times it takes. You can keep track of whatever information off to the side you want, but any changes you make to it has to be based on what is already there and whatever entry from the flight schedule that you are looking at right now. You can't step back and let your mind scan up and down the table to pick things out. You, as a human, can do that very easily. Your program can't, so force yourself to operate at the level of a program.

There are many algorithms that solve this problem, some are more efficient that others while some are easier to understand and implement than others. Focus on developing one that is easy to understand and implement. Don't worry about efficiency, at least not at first.

Here's a hint to get you started. Before you even look at the table, what is the worst case smallest number of flights that you know from each city to F?

Since you don't know if there is ANY route from ANY city (other than F) to F, the answer could be as high as infinity -- i.e., you can't get there from here. In the case of getting from F to F, the answer is trivial -- it takes zero flights.

So we have the following lists of the number of flights to get from each city to F.

Code:
A: ∞
B: ∞
C: ∞
D: ∞
E: ∞
F: 0
G: ∞
H: ∞
Now go through the list of flights, one entry at a time, and see if there are any changes you can make to this table based on that entry. What does the table look like after making one pass through the list of flights?

What changes can you make on the second pass through the flight list?

What's the worst-case number of passes you need to make in order to have a complete table of the minimum number of flights from any city to F (keeping in mind that the answer for some of them might still be infinity, so you can't just go until all of the infinity entries go away)?
ok thanks, that makes sense.
 

WBahn

Joined Mar 31, 2012
32,823
any ideas about the string vs int error message for future reference?
You've never given any indication of what this 'match' variable means. Your code never assigns a value to it, so there is no way for us to divine what information the number assigned to it holds.

I'm guessing (and guessing is a lousy way to do engineering) that what you meant to do was something akin to:

Code:
if (flights[i][1] == "LAX")
    match = i;
I order to save the row number of the flight that ended at LAX.

Then, later, you want to see if another flight starts that the airport of the flight that ended at LAX by doing

Code:
if (flights[i][1] == flight[match][0])
    // do something
 

Thread Starter

arduinolego611

Joined Jan 23, 2022
75
I know that's not what you are trying to do. But that is what you ARE doing. Hence, as I said, your program doesn't match what you are trying to do. Similarly, the logic you describe isn't going to accomplish what you want.

Here is the algorithm as you presented it: " 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. "

What do you want to have happen if you have five entries that have LAX as the second element? Both your description above and your code increment your counter for all five. But does that do anything to solve your problem?

Then your description says that you will increment your counter again every time the second element of an entry matches whatever the first element was any entry that had LAX as its second element. That is NOT what your program is doing, AND it is not going to help you solve your problem.

So, again, I recommend that you forget about programs and programming languages and syntax and focus on the algorithm, because the algorithm you currently have, as you've described it, won't come close to doing what you want.

Consider the following example (so that we have data you haven't seen before). As with your data, each pair means that there is a flight from the first element to the second element.

Code:
A,B
B,H
H,E
A,D
C,F
D,C
C,E
F,G
E,F
G,E
E,A
D,G
What is the smallest total number of flights to get from B to F?

Work this under the same constraints that your program will have to. You only get to look at one entry in the table at a time and you can't bounce around. You start at the top and go to the end. Then, if you need to, you start at the top and go to the end again. And again. However many times it takes. You can keep track of whatever information off to the side you want, but any changes you make to it has to be based on what is already there and whatever entry from the flight schedule that you are looking at right now. You can't step back and let your mind scan up and down the table to pick things out. You, as a human, can do that very easily. Your program can't, so force yourself to operate at the level of a program.

There are many algorithms that solve this problem, some are more efficient that others while some are easier to understand and implement than others. Focus on developing one that is easy to understand and implement. Don't worry about efficiency, at least not at first.

Here's a hint to get you started. Before you even look at the table, what is the worst case smallest number of flights that you know from each city to F?

Since you don't know if there is ANY route from ANY city (other than F) to F, the answer could be as high as infinity -- i.e., you can't get there from here. In the case of getting from F to F, the answer is trivial -- it takes zero flights.

So we have the following lists of the number of flights to get from each city to F.

Code:
A: ∞
B: ∞
C: ∞
D: ∞
E: ∞
F: 0
G: ∞
H: ∞
Now go through the list of flights, one entry at a time, and see if there are any changes you can make to this table based on that entry. What does the table look like after making one pass through the list of flights?

What changes can you make on the second pass through the flight list?

What's the worst-case number of passes you need to make in order to have a complete table of the minimum number of flights from any city to F (keeping in mind that the answer for some of them might still be infinity, so you can't just go until all of the infinity entries go away)?


thanks a lot, this is a fun challenge

I am thinking along the lines of this:

(although the syntax is far from correct... the idea is more logical than the last. its more of a mock-up than actual code)


Code:
        if (flights [i] [1] == flight [match] [0])
        {
            airports [j] ++;
        }
    }
    count = ATL + BOS + EWR + DEN + DFW + IAH + LGA + JFK + LAS + MCO + ORD + PHX + SEA + SLC;
    cout << count << endl;
 

WBahn

Joined Mar 31, 2012
32,823
thanks a lot, this is a fun challenge

I am thinking along the lines of this:

(although the syntax is far from correct... the idea is more logical than the last. its more of a mock-up than actual code)


Code:
        if (flights [i] [1] == flight [match] [0])
        {
            airports [j] ++;
        }
    }
    count = ATL + BOS + EWR + DEN + DFW + IAH + LGA + JFK + LAS + MCO + ORD + PHX + SEA + SLC;
    cout << count << endl;
I'm not following this logic at all. In general, there will be many paths from MCO to LAX. You only want to count the number of flights on the route with the fewest number.
 

Thread Starter

arduinolego611

Joined Jan 23, 2022
75
I'm not following this logic at all. In general, there will be many paths from MCO to LAX. You only want to count the number of flights on the route with the fewest number.
oh wow I just realized that makes no sense...sorry, I apologize bc I am just trying to figure out the logic...

I think im getting closer, I appreciate the responses
 
Top