Hi all, I asked about this question few days ago, and I have tried few things other experts told me

I think i got overcame problem where i can read the tab spaced file but now the output is the problem.....

When i run my program it outputs:

Enter filename: qantum.txt
Enter start city: Sydney

(so far its okay but...)

Cheapest price to New York = 1300

it outputs only one result where it should outputs all the city which is connected with Sydney....

The following output is what it should be:

Enter filename : qantum.txt
Enter start city : Tokyo
Cheapest price to Brussels = 700
Tokyo Paris Brussels
Cheapest price to London = 900
Tokyo Paris London
Cheapest price to Melbourne = 2200
Tokyo Paris London Sydney Melbourne
Cheapest price to New York = 500
Tokyo New York
Cheapest price to Paris = 600
Tokyo Paris
Cheapest price to Sydney = 2000
Tokyo Paris London Sydney


I'll upload my code so plz help me..

Dani AI

Generated

Two directions to check quickly (based on 's symptom and 's suggestion to extract a minimal example): the program is either not parsing the tab-separated file reliably, or the graph/printing logic stops early or only records one path. Common, hard-to-see culprits are leftover CR characters from Windows line endings, mixing operator>> and std::getline (leaving a stray newline in the input buffer), or a break/return inside the loop that prints results.

Recommendations and small, safe snippets to try:

  • Verify the file is parsed exactly as expected: print every token after parsing to confirm source, destination, and cost are read correctly and that no trailing \r remains. Remove \r and trim whitespace before using strings as map keys.
  • Avoid mixing >> and getline without clearing the input buffer. Use std::getline(std::cin >> std::ws, line) or call std::cin.ignore(...) when switching forms of input.
  • Compute single-source shortest paths once (e.g., Dijkstra) from the start city; then iterate the distance/parent tables to print all reachable cities and their reconstructed paths. That avoids re-running or accidentally only showing the first found path.

Parsing + trimming example:

#include <string>
#include <sstream>
#include <algorithm>
#include <cctype>

static inline std::string trim(std::string s) {
    s.erase(s.begin(), std::find_if(s.begin(), s.end(),
        [](unsigned char ch){ return !std::isspace(ch); }));
    s.erase(std::find_if(s.rbegin(), s.rend(),
        [](unsigned char ch){ return !std::isspace(ch); }).base(), s.end());
    return s;
}

// parse one tab-separated line: "src<TAB>dest<TAB>cost"
void parse_line(const std::string &line) {
    std::string l = line;
    if (!l.empty() && l.back() == '\r') l.pop_back(); // strip Windows CR
    std::istringstream iss(l);
    std::string src, dest, cost;
    std::getline(iss, src, '\t');
    std::getline(iss, dest, '\t');
    std::getline(iss, cost);
    src = trim(src); dest = trim(dest); cost = trim(cost);
    // debug print: confirm tokens and store into graph structure
}

After running your shortest-path routine, reconstruct every path from the parent table and print cost + route (run Dijkstra once, then loop all nodes to build their route). For reference on reliable line-reading behavior, see std::getline on cppreference and for the single-source shortest-path pattern, see Dijkstra's algorithm on Wikipedia.

If parsing prints the expected edges and the adjacency lists look correct but output still shows only a single city, inspect any break, return, or loop-condition logic in the result-printing block and ensure the parent/distance containers are not accidentally reinitialized or cleared inside that loop.

Recommended Answers

All 2 Replies

Oops sorry dont know y but it only works like that when i type Sydney rest city says "Press any key to continue" right away!!!

plz help me with this first...

If possible, can you extract the problematic part of the code and post it so we don't have to download a file? Maybe you can make a very small example that demonstrates the problem without having the overhead of the rest of the code.

Dave

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.