CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Hybrid View

  1. #1
    Join Date
    Apr 2009
    Posts
    4

    Thumbs down 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.

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Stack around the variable 'dive_scores' was corrupted.

    Quote Originally Posted by thesaintex2 View Post
    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

  3. #3
    Join Date
    Apr 2009
    Posts
    4

    Re: Stack around the variable 'dive_scores' was corrupted.

    Quote Originally Posted by Paul McKenzie View Post
    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]?

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Stack around the variable 'dive_scores' was corrupted.

    Quote Originally Posted by thesaintex2 View Post
    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

  5. #5
    Join Date
    Apr 2009
    Posts
    4

    Re: Stack around the variable 'dive_scores' was corrupted.

    Quote Originally Posted by Paul McKenzie View Post
    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.

  6. #6
    Join Date
    Apr 2009
    Posts
    4

    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;
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured