Debug Error Run-Time Check Failure #2

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jun 2009
Posts: 4
Reputation: begnnr_help is an unknown quantity at this point 
Solved Threads: 0
begnnr_help begnnr_help is offline Offline
Newbie Poster

Debug Error Run-Time Check Failure #2

 
0
  #1
Jun 19th, 2009
I am running into a baffling problem with my code. I am supposed to write a program to average the test scores for an entire class of students. In each case, the student should have taken 5 tests. You are to average the 5 tests.
The program run should look like this.
How many students are in the class ?
3

Enter five test scores for student number 1 90 90 70 90 80 The average for student number 1 is 84

Enter five test scores for student number 2 100 60 60 90 80 The average for student number 2 is 78

Enter five test scores for student number 3 90 70 50 70 90 The average for student number 3 is 74

Here is what I have:

  1. #include <iostream>
  2. #include <iomanip>
  3.  
  4. using namespace std;
  5.  
  6.  
  7. void handleOneStudent(int N);
  8.  
  9. int main()
  10. {
  11. int NumberOfStudents;
  12. cout << "How many students are in the class ?" << endl;
  13. cin >> NumberOfStudents;
  14. cout << endl;
  15. for (int i=1; i <= NumberOfStudents; i++)
  16. handleOneStudent(i);
  17. return 0;
  18. }
  19. void handleOneStudent(int N)
  20. {
  21. const int num_quizzes = 5;
  22. int score[num_quizzes];
  23. double average;
  24.  
  25. cout << setprecision(2)
  26. << setiosflags(ios::fixed)
  27. << setiosflags(ios::showpoint);
  28.  
  29. cout << "Enter five test scores for student number " << N << endl;
  30. cin >> score[num_quizzes];
  31.  
  32. average = (score[1] + score[2] + score[3] + score[4] + score[5])/5;
  33. cout << endl << endl;
  34.  
  35. cout << "The average for student number " << N << " is " << average << endl;
  36.  
  37. }

When I run the code I get the following error:
Error message:

Debug error!

Run-Time Check Failure #2 - Stack around the variable 'score' was corrupted.
Last edited by begnnr_help; Jun 19th, 2009 at 3:07 pm. Reason: Left out error message
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: Debug Error Run-Time Check Failure #2

 
0
  #2
Jun 19th, 2009
Easy, arrays are 0 based.
average = (score[0] + score[1] + score[2] + score[3] + score[4])/5;
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,688
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 265
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Debug Error Run-Time Check Failure #2

 
0
  #3
Jun 19th, 2009
cin >> score[num_quizzes];

num_quizzes should never be used as an index for the array as it is out of bounds and will cause a run time error. If the above was an attempt to put all five scores into the array in one call to cin that's an error, too. You need to enter each elements value with a separate call to cin (or maybe a call to memset or some such "exotic" method) whether you write code for each element separately or use a loop to abstract things a bit.
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 4
Reputation: begnnr_help is an unknown quantity at this point 
Solved Threads: 0
begnnr_help begnnr_help is offline Offline
Newbie Poster

Re: Debug Error Run-Time Check Failure #2

 
0
  #4
Jun 20th, 2009
Thanks for the tip. That was what I needed to do amoung a few other things.
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC