Program Compiles, and excecutes but crashes when run.

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Mar 2008
Posts: 8
Reputation: mofoparrot is an unknown quantity at this point 
Solved Threads: 0
mofoparrot mofoparrot is offline Offline
Newbie Poster

Program Compiles, and excecutes but crashes when run.

 
0
  #1
Apr 30th, 2008
Hey guys, I have been working on this program for my entire semester. I got it down to do what I want I think... except when I compile it, I get no errors or warnings (using Dev) and when I execute it runs through half of it and it stops and gives me a error saying the program stopped responding.

I tried debuging it but I don't know what it's telling me when it sends me to a if loop. Anyone have any suggestions, the program is displayed below and the text file is attached.

  1. #include<iostream> //File input/ouput include files
  2. #include<fstream>
  3. #include<iomanip>
  4. #include<set>
  5. #include<list>
  6.  
  7.  
  8.  
  9. using namespace std; //C++ standard libraries
  10. const int NUMARCS = 16; //Need to use const ints for array declaration
  11. const int NUMNODES = 8;
  12. int i;
  13. int main() //Main function
  14. {
  15. ifstream readin("fstarin.txt", ios::in); //open input file
  16. ofstream readout("hw5out.txt", ios::out); //open output file
  17. if (!readin) {
  18. cerr << "File could not be opened" << endl; //Make sure file opens
  19. exit(1);
  20. }
  21. //declare arrays to be used
  22. int tail[NUMARCS+1], head[NUMARCS+1], cost[NUMARCS+1], cap[NUMARCS+1];
  23. //Initilize point[i] = 0... this will be a useful condition later
  24. int point[NUMNODES+2] = {0};
  25. int i = 1;
  26. /*This is a fairly concise loop to read the input file and assign
  27. the input stream to the arrays created. This same loop outputs the
  28. arrays as well. Note that setw() is for formatting output*/
  29. while (readin >> tail[i] >> head[i] >> cost[i] >> cap[i])
  30. i++;
  31. readout << setiosflags(ios::left) << setw(7) << "Arc #" << setw(7) << "Tail"<< setw(7)<< "Head" << setw(7)<< "Cost" << "Capacity" << endl;
  32. for (i = 1; i <= NUMARCS; i++)
  33. {
  34. readout << setiosflags(ios::left) << setw(7) << i << setw(7) << tail[i]
  35. << setw(7) << head[i]
  36. << setw(7) << cost[i]
  37. << cap[i] << endl;
  38. }
  39. /*Now I use a loop to assign the point vector to the first arc in the
  40. forward star having the same tail node. Note that nodes with no
  41. emanating arcs will still have point[i] = 0*/
  42. for (i = 1; i<= NUMARCS+1; i++)
  43. {
  44. if (point[tail[i]] == 0)
  45. {
  46. point[tail[i]] = i;
  47. }
  48. }
  49. //Now I clean up those nodes with no emanating arcs
  50. for (i = 1; i<=NUMNODES; i++)
  51. {
  52. if (point[i] == 0)
  53. {
  54. point[i] = point[i+1];
  55. }
  56. }
  57. /*Lastly, we can set the pointer for the n+1 node, we can do it this
  58. simply wlog becasue this condition will hold for all forward star
  59. representations*/
  60. point[NUMNODES+1] = NUMARCS +1;
  61.  
  62. //Output the pointer vector
  63. readout << endl;
  64. readout << "The point vector is: [";
  65. for (i = 1; i <= NUMNODES+1; i++)
  66. {
  67. readout << point[i] << " ";
  68. }
  69. readout << "]";
  70. readout << endl;
  71. readout << endl;
  72. /*This is the initialization phase of Dijkstra's: we need to construct the
  73. temporary and permenently labeled node sets. The Perm set is initially empty,
  74. the temporary initially contains all nodes. We then declare and initialize
  75. the label and predecessor vector*/
  76. //There are containers called sets with predefined operations that are useful
  77. //in this context
  78. set<int> Perm;
  79. set<int> Temp;
  80. set<int>::iterator it;
  81. for (i = 1; i <= NUMNODES; i++)
  82. {
  83.  
  84. Temp.insert(i);
  85. }
  86. int label[NUMNODES+1], pred[NUMNODES+1], lownode, lowlabel;
  87. for (i = 1; i <=NUMNODES; i++)
  88. {
  89. label[i] = 1000;
  90. }
  91. label[1] = 0;
  92. pred[1] = 0;
  93. /*We enter the main loop, checking that not all nodes are permanently labeled*/
  94. while (Perm.size() < NUMNODES)
  95. {
  96. lownode = 0;
  97. lowlabel = 100000;
  98. /*The first loop now checks for the lowest temporary distance label. This is
  99. an easy, loop-based check*/
  100. for (it = Temp.begin(); it != Temp.end() ; it++)
  101. {
  102. if (label[*it] < lowlabel)
  103. {
  104. lowlabel = label[*it];
  105. lownode = *it;
  106. }
  107. }
  108. //Remove the node from the temp set, add it to the permanent set
  109. Temp.erase(lownode);
  110. Perm.insert (Perm.end(),lownode);
  111. /*Now, using the forward star, identify those arcs emanating from our newly
  112. permanently labeled noede and update distance labels and pred vector as
  113. appropriate*/
  114. for (i= point[lownode]; i < point[lownode+1]; i++)
  115. {
  116. if (label[head[i]] > label[lownode] + cost[i])
  117. {
  118. label[head[i]] = label[lownode] + cost[i];
  119. pred[head[i]] = lownode;
  120. }
  121. }
  122. }
  123. //Output the distance labels and predecessor vector
  124. readout << "The optimal distance labels are:" << "[";
  125. for (i = 1; i <=NUMNODES; i++)
  126. {
  127. readout << " " << label[i];
  128. }
  129. readout << "]" << endl << endl;
  130. readout << "The predecessor vector is:" << "[";
  131. for (i = 1; i <=NUMNODES; i++)
  132. {
  133. readout << " " << pred[i];
  134. }
  135. readout << "]" << endl << endl;
  136. /*Now we can identify the shortest path from 1 to all. Use the list container
  137. as lists do not automatically sort the contents*/
  138. list<int> path;
  139. list<int>::iterator iter;
  140. for (i = 2; i <=NUMNODES; i++)
  141. {
  142. readout << "The shortest path from node 1 to node " << i << " is: 1";
  143. int current = i;
  144. path.erase(path.begin(), path.end());
  145. while (pred[current] != 0 )
  146. {
  147. path.insert(path.begin(), current);
  148. current = pred[current];
  149. }
  150. for (iter = path.begin(); iter !=path.end(); iter++)
  151. {
  152. readout << "-" << *iter;
  153. }
  154. readout << endl;
  155. }
  156. return 0;
  157. }

here is the text, it's also attached
1 2 2 4
1 3 3 7
2 4 2 6
2 5 6 8
2 6 8 8
3 2 5 5
3 5 3 8
3 8 5 9
4 3 2 4
4 5 3 4
4 6 3 9
4 7 1 5
5 7 6 4
5 8 1 2
7 6 1 5
8 7 2 9
Attached Files
File Type: txt fstarin.txt (142 Bytes, 0 views)
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,678
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1504
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Program Compiles, and excecutes but crashes when run.

 
0
  #2
Apr 30th, 2008
Your program is experiencing out-of-bounds errors. The arrays are allocated 17 elements numbered 0-16 and your program is attempting to write to elements 1-17. Element #17 does not exist.

Learn to count the C and C++ way -- with 0 as the base value. Your program is chuck full of buffer overruns and out-of-bound errors.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 8
Reputation: mofoparrot is an unknown quantity at this point 
Solved Threads: 0
mofoparrot mofoparrot is offline Offline
Newbie Poster

Re: Program Compiles, and excecutes but crashes when run.

 
0
  #3
Apr 30th, 2008
I thought i accounted for that though, by having i start at 1 in every itteration. I don't understand what I'm suppose to do to change it. I tried looking through the program and I don't have anything that seems that it wouldn't work.

I tried changing the const instead of 16 to 15, and the nodes from 8 to 7, and then vise versa up to 17, and 9, and still having the same thing happening...
Last edited by mofoparrot; Apr 30th, 2008 at 11:48 pm. Reason: needed to add a word to sentence
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 8
Reputation: mofoparrot is an unknown quantity at this point 
Solved Threads: 0
mofoparrot mofoparrot is offline Offline
Newbie Poster

Re: Program Compiles, and excecutes but crashes when run.

 
0
  #4
May 1st, 2008
When I try to debug it, it points to this line but I don't know what's wrong w/ it...

  1. for (i = 1; i<= NUMARCS+1; i++)
  2. {
  3. if (point[tail[i]] == 0)
  4. {
  5. point[tail[i]] = i;
  6. }
  7. }
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 44
Reputation: rje7 is an unknown quantity at this point 
Solved Threads: 3
rje7 rje7 is offline Offline
Light Poster

Re: Program Compiles, and excecutes but crashes when run.

 
0
  #5
May 1st, 2008
Originally Posted by mofoparrot View Post
When I try to debug it, it points to this line but I don't know what's wrong w/ it...

  1. for (i = 1; i<= NUMARCS+1; i++)
  2. {
  3. if (point[tail[i]] == 0)
  4. {
  5. point[tail[i]] = i;
  6. }
  7. }
try this

  1. for (i = 0; i< NUMARCS+1; i++)
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 8
Reputation: mofoparrot is an unknown quantity at this point 
Solved Threads: 0
mofoparrot mofoparrot is offline Offline
Newbie Poster

Re: Program Compiles, and excecutes but crashes when run.

 
0
  #6
May 1st, 2008
I did, I tried changing all the 1's to 0's also, that didn't help, then I tried leaving them at 0's and bring down the numbers of everything that didn't work tried bringing it up.. I honestly can't figure it out tried singling out stuff and same problem.

It isn't pointing at the for, it's pointing at the if, i tried changing that 0 to 1 also. I honestly can't think anymore spent about eight hours writing this today with all the errors and i can't get it to work
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 8
Reputation: mofoparrot is an unknown quantity at this point 
Solved Threads: 0
mofoparrot mofoparrot is offline Offline
Newbie Poster

Re: Program Compiles, and excecutes but crashes when run.

 
0
  #7
May 1st, 2008
it's telling me it's unable to handle exception when it poitns to that if statement
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 8
Reputation: mofoparrot is an unknown quantity at this point 
Solved Threads: 0
mofoparrot mofoparrot is offline Offline
Newbie Poster

Re: Program Compiles, and excecutes but crashes when run.

 
0
  #8
May 1st, 2008
Holy crap I solved it guys!!! Thank you sooooooooo much the program works!!!

All I had to do was take out the +1 on

for (i = 1; i< NUMARCS+1; i++)

and change it too

for (i = 0; i< NUMARCS; i++)

I can't thank you guys enough really honestly, I freaking love Daniweb!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum


Views: 534 | Replies: 7
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC