Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Finding a path between nodes -- C++?

finding path between structs, struct is read in as argument. Flights look like

BOS ORD 1002120.200

XYZ ABC 2023.2323

ABC BOS 2300.22

two 3 letter strings then a double

my struct looks like this:

//struct for holding flights

struct flight{

//string departure

string dep;

//string destination

string dest;

//double for holding price

double price;

} fl;

my code to find a path looks like this:

bool findPath(string dep, string dest, vector<flight> &RAND_FLIGHTS){

cout << "Finding Path..." << endl;

//value for return

bool retbool = false;

//cycle thru vector

for(int i=0; i<RAND_FLIGHTS.size(); i++){

//if case... a single flight is the correct answer

if((RAND_FLIGHTS[i].dep == dep) && (RAND_FLIGHTS[i].dest == dest)){

retbool = true;

}

//else if case, indirect path

if((RAND_FLIGHTS[i].dep == dep) && (RAND_FLIGHTS[i].dest != dest)){

dep = RAND_FLIGHTS[i].dest;

RAND_FLIGHTS.erase(RAND_FLIGHTS.begin()+i);

findPath(dep, dest, RAND_FLIGHTS);

}

//else -- no matches for departure

else{ //if((RAND_FLIGHTS[i].dep == dep) && (RAND_FLIGHTS[i].dest != dest)){

//retbool = false;

cout << "else case" << endl;

}

}

return retbool;

}

trying to make it so it returns true if there is a path or false if there is not.

Thanks in advance for any help!

1 Answer

Relevance
  • Anonymous
    10 years ago
    Favorite Answer

    Are you sure that a type string is comparable to a string? Like if you use char arrays you can't use myChars=="something" as you are comparing the pointers not the text. Try using strcmp().

    Also, ditch retbool. If in the loop you find a path, return true. At the end of the function return false. Otherwise if your loop finds a good flight but next loop around has no flight it resets retbool to false.

Still have questions? Get your answers by asking now.