where is my infinite loop coming from?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: May 2008
Posts: 10
Reputation: c++ newbie is an unknown quantity at this point 
Solved Threads: 0
c++ newbie c++ newbie is offline Offline
Newbie Poster

where is my infinite loop coming from?

 
0
  #1
May 10th, 2008
this is what my program looks like but after prompting and reading my files, i get an infinite loop. the program compiles and i just put that simple cout statement just to see if anything will come out. can anyone see where i am messing up within my main that is causing this infinite loop?

  1.  
  2. #include <iostream>
  3. #include <fstream>
  4. #include <cstring>
  5. #include <cctype>
  6. #include <iomanip>
  7.  
  8. using namespace std;
  9.  
  10. const int MAX = 30;
  11. const int MAX_HW = 10;
  12. const int MAX_E = 3;
  13.  
  14.  
  15. struct file1
  16. {
  17. int id;
  18. string fname;
  19. string lname;
  20. int hw [MAX_HW];
  21. int exam [MAX_E];
  22. double exam_score;
  23. double hw_score;
  24. int totpts;
  25. };
  26.  
  27. void fixstring (string&);
  28. void get_data (file1 [], int&);
  29. void alpha (file1 [], int);
  30. void initial (file1 [], int);
  31. void first (file1 [], int);
  32.  
  33.  
  34. int main ()
  35. {
  36. string filename1;
  37. string studentinfo;
  38. string filename2;
  39. string gradeinfo;
  40. string filename3;
  41. string results;
  42. char ch;
  43. char letter_score;
  44. int x, x1, y, y1, z, z1;
  45. double pct;
  46. int id_temp;
  47. int hw_score;
  48. int exam_score;
  49. int hw_num;
  50. int exam_num;
  51.  
  52.  
  53. file1 names [MAX];
  54. //file2 grades [MAX];
  55.  
  56. ifstream inf1;
  57. ifstream inf2;
  58. ofstream outf;
  59. int n = 0;
  60.  
  61. cout << "Please enter name of student information file:" << endl;
  62. cin >> filename1;
  63. cout << "Please enter name of grade information file:" << endl;
  64. cin >> filename2;
  65. cout << "Please enter name of output file:" << endl;
  66. cin >> filename3;
  67.  
  68. inf1.open (filename1.c_str());
  69. inf2.open (filename2.c_str());
  70. outf.open (filename3.c_str());
  71. inf1 >> names[n].id;
  72.  
  73. while (!inf1.eof())
  74. {
  75.  
  76. inf1 >> names[n].lname >> names[n].fname;
  77. fixstring (names[n].lname);
  78. fixstring (names[n].fname);
  79. n++;
  80. inf1 >> names[n].id;
  81. }
  82.  
  83.  
  84.  
  85. for (int hw_r = 0; hw_r < n; n ++)
  86. {
  87. for (int hw_c = 1; hw_c <= MAX_HW; hw_c ++)
  88. {
  89. names[hw_r].exam[hw_c] = 0;
  90. }
  91. }
  92. for (int exam_r = 0; exam_r < n; exam_r++)
  93. {
  94. for (int exam_c = 1; exam_c <= MAX_E; exam_c ++)
  95. {
  96. names[exam_r].exam[exam_c] = 0;
  97. }
  98. }
  99.  
  100. inf2 >> id_temp;
  101. while (!inf2.eof())
  102. {
  103. for (int x = 0; x < n; x ++)
  104. if (id_temp == names[x].id)
  105. {
  106. inf2 >> ch;
  107. while (ch != 'Q')
  108. {
  109. if (ch == 'H')
  110. {
  111. inf2 >> hw_num;
  112. inf2 >> names[x].hw[hw_num];
  113. }
  114.  
  115. else if (ch == 'E')
  116. {
  117. inf2 >> exam_num;
  118. inf2 >> names[x].exam[exam_num];
  119. }
  120. inf2 >> ch;
  121. }
  122. }
  123. inf2 >> id_temp;
  124. }
  125.  
  126.  
  127.  
  128. for (int x = 0; x < n; x ++)
  129. {
  130. names[x].totpts = 0;
  131.  
  132. for (int hw_num = 1; hw_num <= MAX_HW; hw_num ++)
  133. {
  134. names[x].totpts = names[x].totpts + names[x].hw[hw_num];
  135. }
  136.  
  137. for (int exam_num = 1; exam_num <= MAX_E; exam_num ++)
  138. {
  139. names[x].totpts = names[x].totpts + names[x].exam[exam_num];
  140. }
  141. }
  142.  
  143.  
  144. alpha(names, n);
  145. outf << right << setw (4) << "NAME" << setw (40) << "ID#" << endl;
  146. for ( int x = 0; x < n; x ++)
  147. {
  148. //outf << names[n].fname << ", " << names[n].lname << names[n].id << endl;
  149. outf << names[x].lname << ", " << names[x].fname << right << setw (40 - names [x].lname.length () - names\
  150. [x].fname.length ()) << names [x].id << endl;
  151. }
  152. outf << endl <<endl;
  153.  
  154. outf.close ();
  155.  
  156. return 0;
  157. }
  158.  
  159.  
  160. void fixstring (string& names)
  161. {
  162. int wlen = names.length();
  163. names[0] = toupper (names[0]);
  164. for (int i = 1; i < wlen; i++)
  165. names [i] = tolower (names[i]);
  166. }
  167.  
  168. void alpha ( file1 names [], int n)
  169. // Given the string "names", alpha will read in the file and place and arrange the names accordingl \
  170. // y and put them in a particular order. The alphabetized array of strings will then be passed back.
  171. {
  172. int i;// initializing integer datatype to i
  173. int j;// intiializing integer datatype to j
  174. file1 temp;// string datatype being justified for temp to be used within function
  175. for (i=0; i < n-1; i++)
  176. {
  177. for (j=0; j < n - (i+1); j++)
  178. {
  179. if (names[j].lname > names [j+1].lname)
  180. {
  181. temp = names[j];
  182. names [j] = names [j+1];
  183. names [j+1] = temp;
  184. }
  185. }
  186. }
  187.  
  188. }
  189.  
  190.  
  191. /*void initial (file1 names[], int n)
  192.   {
  193.   int x, q;
  194.   for (x = 0; x < n; x++)
  195.   {
  196.   cin >> names[n].id;
  197.   for (q = 0; q = 10; q++)
  198.   names[n].hw_num[q] = 0;
  199.   }
  200.   }*/
Last edited by Ancient Dragon; May 10th, 2008 at 12:30 am. Reason: add line numbers for convenience
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,629
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: 1496
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: where is my infinite loop coming from?

 
0
  #2
May 10th, 2008
line 87: array elements are numberd from 0 to but not includeing the number of elements in the array. So that loop should be coded like this:
for (int hw_c = 0; hw_c < MAX_HW; hw_c ++)

line 89: that's initializing the wrong array. It should be initialzing array hw.

I'll need the two text file in order to actually test your code.
Last edited by Ancient Dragon; May 10th, 2008 at 12:38 am.
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: May 2008
Posts: 89
Reputation: n1337 is on a distinguished road 
Solved Threads: 9
n1337 n1337 is offline Offline
Junior Poster in Training

Re: where is my infinite loop coming from?

 
0
  #3
May 10th, 2008
The best way (I find) of troubleshooting is to just stick a whole bunch of print statements in your code ( printf()...or I guess you could use cout no difference).

If you stick specific print statements before and after each loop, you can tell whether your program made it to that point or not. This way, you can isolate whatever loop is the problem and work from there. Note that, though your code may enter an infinite loop, the problem may lie in previous code...so keep a sharp eye out!

(Note: I always think it best to force you to think about the problem rather than just giving you a straight up answer. Fact is, every1 runs into these problems, no matter how experienced a programmer you are. So its could practice to learn to troubleshoot your own code. That said, I also didn't bother to analyze your code...thought it would take too long. So this way, its a win win situation -- you learn to troubleshoot, and I don't need to figure out what you are doing in your program

But if you still need help, post back and I, or someone else, will take a closer look...)

EDIT: apparently somebody else has already replied, so nvm
Last edited by n1337; May 10th, 2008 at 12:41 am.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 10
Reputation: c++ newbie is an unknown quantity at this point 
Solved Threads: 0
c++ newbie c++ newbie is offline Offline
Newbie Poster

Re: where is my infinite loop coming from?

 
0
  #4
May 10th, 2008
this is what my data looks like


studentinfo
567 white robert
43 blackburn donna
722 Grey jAMes
19 bRoWn jAnE


gradeinfo
722 E 1 95 E 2 89 E 3 90 Q
567 E 1 67 H 1 20 H 5 20 E 2 76 Q
83 this line should be ignored, 83 is not a valid ID#
19 H 1 20 H 2 20 H 3 19 H 4 18 H 5 16 Q
43 E 3 91 Q
19 H 6 20 H 7 20 H 8 20 H 9 20 H 10 18 Q
722 H 5 20 H 6 20 H 7 20 Q
19 E 1 85 E 2 90 Q
567 E 3 80 Q
722 H 4 15 H 3 14 H 2 20 H 1 20 Q
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 10
Reputation: c++ newbie is an unknown quantity at this point 
Solved Threads: 0
c++ newbie c++ newbie is offline Offline
Newbie Poster

Re: where is my infinite loop coming from?

 
0
  #5
May 10th, 2008
i have adjusted all my loops accordingly and i fixed that array but i'm still getting the infinite loop after i prompt and read in my output file
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,629
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: 1496
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: where is my infinite loop coming from?

 
0
  #6
May 10th, 2008
The infinit loop is here:
for (int hw_r = 0; hw_r < n; n ++)
That should be hw_r++, not n++. That's an easy mistake to make, I think we have all done that at one time or another.
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: May 2008
Posts: 10
Reputation: c++ newbie is an unknown quantity at this point 
Solved Threads: 0
c++ newbie c++ newbie is offline Offline
Newbie Poster

Re: where is my infinite loop coming from?

 
0
  #7
May 10th, 2008
thank you. i just saw it also. there was minore tweakings i had to do through the prog too but i think i'm on the right track now. fingers crossed
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,629
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: 1496
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: where is my infinite loop coming from?

 
0
  #8
May 10th, 2008
Originally Posted by n1337 View Post
The best way (I find) of troubleshooting is to just stick a whole bunch of print statements in your code ( printf()...or I guess you could use cout no difference).
The best was to do it is to learn how to use your compiler's debugger so that you can easily see that the program is doing and the value of all variables at any given time.
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: Aug 2005
Posts: 15,629
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: 1496
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: where is my infinite loop coming from?

 
0
  #9
May 10th, 2008
The loops that read the data are also coded wrong -- there is no need for using eof() at the top of the loop. Here is a better and more accurate way to code those loops
  1. while(n < MAX && inf1 >> names[n].id >> names[n].lname >> names[n].fname)
  2. {
  3. fixstring (names[n].lname);
  4. fixstring (names[n].fname);
  5. n++;
  6. }
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  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC