943,852 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1629
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
May 27th, 2009
0

Output in alphabetical order

Expand Post »
Hello all, I was building a program which needs to output in alphabetical order and i got stuck.
(Program reads in text file and then do dijkstra's shortest path)

My output is fine except that it is not in alphabetical order....
can anyone help me with this???

here is my code:
  1. void dijkstra(string s, NodeMap &nodes)
  2. {
  3. // check and report or abort
  4. Node* source = nodes.nodemap[s];
  5. if(source==0)
  6. {
  7. cout << "Sorry we don't fly from "<< s << endl; return;
  8. }
  9. reset_nodes(nodes);
  10.  
  11. // put the source into pq and loop until empty
  12. priority_queue<Node *, deque<Node*>, compare>pq;
  13. pq.push(source);
  14. while(!pq.empty())
  15. {
  16. // process least cost node.
  17. Node* curr = pq.top();
  18. pq.pop();
  19. curr->visited = true;
  20. // process neighbors
  21. list<Edge*>::iterator edge;
  22. for(edge = curr->neighbors.begin(); edge != curr->neighbors.end(); edge++)
  23. {
  24. Node *ne = (*edge)->dest;
  25. if(!ne->visited)
  26. {
  27. ne->cost += (*edge)->weight + curr->cost;
  28. ne->visited = true;
  29. ne->back_pointer = curr;
  30. cout << "Cheapest price to "<< ne->name << " = " << ne->cost << endl;;
  31. pq.push(ne);
  32. }
  33. }
  34. }
  35. }
Last edited by Ancient Dragon; May 27th, 2009 at 7:19 am. Reason: add code tags
Reputation Points: 10
Solved Threads: 0
Newbie Poster
wnrqkflwnrqkfl is offline Offline
15 posts
since May 2009
May 27th, 2009
0

Re: Output in alphabetical order

Start with these:Then you should explain what you mean with: ...output should be in alphabetical order... (give an example)
Do you mean the following?
File looks like:
lalal nn
Output should be:
aalllnn
Last edited by tux4life; May 27th, 2009 at 2:24 am.
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
May 27th, 2009
0

Re: Output in alphabetical order

My output should looks like

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

where the city name next to weight is should be in alphabetical order.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
wnrqkflwnrqkfl is offline Offline
15 posts
since May 2009
May 27th, 2009
0

Re: Output in alphabetical order

> where the city name next to weight is should be in alphabetical order.

What exactly do you mean by this statement? What "weight" are you referring to? And by "city name should be in alphabetical order", did you mean that the last line(for example) in your Output should look like this - "London Paris Sydney Tokyo"? If not, then try and explain your problem more clearly...
Reputation Points: 130
Solved Threads: 22
Junior Poster
amrith92 is offline Offline
187 posts
since Jul 2008
May 27th, 2009
0

Re: Output in alphabetical order

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

hmm sry my english isnt perfect....

wat i mean is

those city names comes after Cheapest price to .....

it should be in alphabetical order

where mine is start with

Cheapest price to New York = 500
Reputation Points: 10
Solved Threads: 0
Newbie Poster
wnrqkflwnrqkfl is offline Offline
15 posts
since May 2009
May 27th, 2009
0

Re: Output in alphabetical order

What does qantum.txt look like?

Post a sample or attach the file.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
May 27th, 2009
0

Re: Output in alphabetical order

the things in text is...

Sydney New York 1300
New York Paris 1200
London Sydney 1100
Sydney London 1300
Paris New York 900
New York London 700
London Paris 200
Sydney Paris 900
Paris London 300
Brussels Paris 200
London Brussels 300
Sydney Brussels 700
Brussels New York 400
Paris Brussels 100
Tokyo New York 500
Tokyo Paris 600
Sydney Tokyo 300
Sydney Melbourne 200
Reputation Points: 10
Solved Threads: 0
Newbie Poster
wnrqkflwnrqkfl is offline Offline
15 posts
since May 2009
May 27th, 2009
0

Re: Output in alphabetical order

Please post an example of your full code to test.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
May 27th, 2009
0

Re: Output in alphabetical order

Here is my full code
Attached Files
File Type: cpp fly.cpp (3.2 KB, 17 views)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
wnrqkflwnrqkfl is offline Offline
15 posts
since May 2009
May 27th, 2009
0

Re: Output in alphabetical order

Your code doesn't do anything.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Sorry but im new at this but i have some questions
Next Thread in C++ Forum Timeline: how to ignore the ENTER key





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC