| | |
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:
Solved Threads: 0
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:
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.
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:
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <iomanip> using namespace std; void handleOneStudent(int N); int main() { int NumberOfStudents; cout << "How many students are in the class ?" << endl; cin >> NumberOfStudents; cout << endl; for (int i=1; i <= NumberOfStudents; i++) handleOneStudent(i); return 0; } void handleOneStudent(int N) { const int num_quizzes = 5; int score[num_quizzes]; double average; cout << setprecision(2) << setiosflags(ios::fixed) << setiosflags(ios::showpoint); cout << "Enter five test scores for student number " << N << endl; cin >> score[num_quizzes]; average = (score[1] + score[2] + score[3] + score[4] + score[5])/5; cout << endl << endl; cout << "The average for student number " << N << " is " << average << endl; }
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
•
•
Join Date: Jul 2005
Posts: 1,688
Reputation:
Solved Threads: 265
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.
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
![]() |
Similar Threads
- Concatenate function passed w\ pointers C++ (C++)
- DLL run-time error (C++)
- Need Help on random numbers and adding them and displaying statement (C++)
- Find a character in a string (C)
- Unmanaged C++ dll taking CString as parameter crashes my app! (C++)
- C Beginner: Adding Two Polynomials of degree n (C)
- Stack corruption error: (C++)
- Run Time Error! Why ? (C++)
- Stack help: Run-Time Check Failure #2 (C++)
- Export Obejct from DLL (C++)
Other Threads in the C++ Forum
- Previous Thread: I need some help...
- Next Thread: error C2664: 'F_Ite' : cannot convert parameter 1 from 'double *' to 'double'
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






