
April 5th, 2009, 08:15 PM
#1
Stack around the variable 'dive_scores' was corrupted.
Hey everyone I'm having some trouble with my C++ program to calculate the average of 7 dive scores and I am getting the error "Stack around the variable 'dive_scores' was corrupted. Thanks
Code:
int main()
{
double dive_scores[6]; // an array of 7 elements
double single_dive_score; // the input of each dive score
double diff_factor = 0.0; // the input of the difficulty factor
double sum = 0.0; // sum of the dive scores
double avg = 0.0; // average of the scores
int n = 0; // the number of items read from the user
// initialize!
for (int i = 0; i < 7; i++)
{
dive_scores[i] = 0.0;
}
// perform input
while ( n < 7 )
{
// prompt the user and read the value...
cout << "Enter score " << (n+1) << " between 0.0 and 10.0 " << endl;
cin >> single_dive_score;
if (single_dive_score <= 10)
{
dive_scores[n] = single_dive_score;
n++;
}
else if ((single_dive_score < 0)  (single_dive_score > 10))
{
cout << "Invalid Number" << endl;
return 0;
}
}
if (n > 0)
{
//calculate the average...
for(int i = 0; i < n; i++)
{
sum+= dive_scores[i];
}
// do the final step in the average calculation...
avg = computeAverage(sum,n);
}
cout << "Enter difficulty factor:" << endl;
cin >> diff_factor;
for (n = 0; n < 7; n++)
{
std::cout << "Dive Score #" << (n+1) << ":" << dive_scores[n] << std::endl;
}
//calcuate the final score...
outputFinal(avg,diff_factor);
// exit the program with success (0 == success)
return 0;
}
double computeAverage(double sum, int n)
{
double avg = 0.0;
//compute the average of the scores
avg = sum / n;
return avg;
}
double outputFinal(double avg, double diff_factor)
{
double final_score = 0.0;
final_score = (avg * diff_factor);
return final_score;
}
Last edited by thesaintex2; April 5th, 2009 at 08:47 PM.

April 5th, 2009, 08:40 PM
#2
Re: Stack around the variable 'dive_scores' was corrupted.
Originally Posted by thesaintex2
Hey everyone I'm having some trouble with my C++ program to calculate the average of 7 dive scores and I am getting the error "Stack around the variable 'dive_scores' was corrupted. Thanks
1) Please use code tags when posting code. The code you posted is practically unreadable.
2) The error is here:
Code:
double dive_scores[6]; // an array of 7 elements
An array of 7 elements? The only available positions for the array are dive_scores[0] to dive_scores[5] (6 elements).
Your code assumes you have 7 elements when you only have 6.
Regards,
Paul McKenzie

April 5th, 2009, 08:48 PM
#3
Re: Stack around the variable 'dive_scores' was corrupted.
Originally Posted by Paul McKenzie
1) Please use code tags when posting code. The code you posted is practically unreadable.
2) The error is here:
Code:
double dive_scores[6]; // an array of 7 elements
An array of 7 elements? The only available positions for the array are dive_scores[0] to dive_scores[5] (6 elements).
Your code assumes you have 7 elements when you only have 6.
Regards,
Paul McKenzie
Sorry about the code tag thing. But since there are 7 dive scores wouldn't there be 7 arrays [0]  [6]?

April 5th, 2009, 08:56 PM
#4
Re: Stack around the variable 'dive_scores' was corrupted.
Originally Posted by thesaintex2
Sorry about the code tag thing. But since there are 7 dive scores wouldn't there be 7 arrays [0]  [6]?
You are confusing accessing the array with declaration of the array.
When you declare an array, you are stating the number of elements. Therefore what you did was declare that the array has 6 elements.
Regards,
Paul McKenzie

April 5th, 2009, 08:59 PM
#5
Re: Stack around the variable 'dive_scores' was corrupted.
Originally Posted by Paul McKenzie
You are confusing accessing the array with declaration of the array.
When you declare an array, you are stating the number of elements. Therefore what you did was declare that the array has 6 elements.
Regards,
Paul McKenzie
You're the man. Thanks a lot.

April 5th, 2009, 09:27 PM
#6
Re: Stack around the variable 'dive_scores' was corrupted.
One last problem... My final score is always coming out as 0. Why is this?
Code:
#include <iostream>
#include <iomanip>
#include <string>
#include <cstring>
#include <cmath>
#include <cstdlib>
///////////////////////////////////////////////////////////////////////////////
// Namespaces used....
///////////////////////////////////////////////////////////////////////////////
using namespace std;
///////////////////////////////////////////////////////////////////////////////
// Type Definitions...
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// Function prototypes...
///////////////////////////////////////////////////////////////////////////////
double computeAverage(double sum, int n);
//computes the average of the scores...
//
double outputFinal(double avg, double diff_factor);
//outputs the final score...
///////////////////////////////////////////////////////////////////////////////
// Constants...
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
//
// FUNCTION NAME: main
// PARAMETERS: None
// RETURN TYPE: int
// PURPOSE: Entry point for the application.
//
///////////////////////////////////////////////////////////////////////////////
int main()
{
double dive_scores[7]; // an array of 7 elements
double single_dive_score; // the input of each dive score
double diff_factor = 0.0; // the input of the difficulty factor
double sum = 0.0; // sum of the dive scores
double avg = 0.0; // average of the scores
double final_score = 0.0; // final score
int n = 0; // the number of items read from the user
// initialize!
for (int i = 0; i < 7; i++)
{
dive_scores[i] = 0.0;
}
// perform input
while ( n < 7 )
{
// prompt the user and read the value...
cout << "Enter score " << (n+1) << " between 0.0 and 10.0 " << endl;
cin >> single_dive_score;
if (single_dive_score <= 10)
{
dive_scores[n] = single_dive_score;
n++;
}
else if ((single_dive_score < 0)  (single_dive_score > 10))
{
cout << "Invalid Number" << endl;
return 0;
}
}
if (n > 0)
{
//calculate the average...
for(int i = 0; i < n; i++)
{
sum+= dive_scores[i];
}
// do the final step in the average calculation...
avg = computeAverage(sum,n);
}
cout << "Enter difficulty factor:" << endl;
cin >> diff_factor;
for (n = 0; n < 7; n++)
{
std::cout << "Dive Score #" << (n+1) << ": " << dive_scores[n] << std::endl;
}
//calcuate the final score...
outputFinal(avg,diff_factor);
cout << "The average is score is " << avg << "." << endl;
cout << "The final score is " << final_score << "." << endl;
// exit the program with success (0 == success)
return 0;
}
///////////////////////////////////////////////////////////////////////////////
//
// FUNCTION NAME: computeAverage
// PARAMETERS: double sum, int n
// RETURN TYPE: double
// PURPOSE: computes the average of the scores
//
///////////////////////////////////////////////////////////////////////////////
double computeAverage(double sum, int n)
{
double avg = 0.0;
//compute the average of the scores
avg = sum / n;
return avg;
}
///////////////////////////////////////////////////////////////////////////////
//
// FUNCTION NAME: computeAverage
// PARAMETERS: double sum, int n
// RETURN TYPE: double
// PURPOSE: computes the average of the scores
//
///////////////////////////////////////////////////////////////////////////////
double outputFinal(double avg, double diff_factor)
{
double final_score = 0.0;
final_score = avg * diff_factor;
return final_score;
}

April 5th, 2009, 09:37 PM
#7
Re: Stack around the variable 'dive_scores' was corrupted.
Originally Posted by thesaintex2
One last problem... My final score is always coming out as 0. Why is this?
You've coded all of this, so the question is this: have you used the debugger to find the problem, or at the very least, use output statements to see where the problem may be?
Regards,
Paul McKenzie
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

Click Here to Expand Forum to Full Width
This is a CodeGuru survey question.
Featured
