0x80000003 Error when running program with functions

-4
float average1;
cout << "Welcome to the weighted average calulator.\nPlease enter your first grade:";
cin >> average1;

float weight1;
cout << "Enter the weight (not the weight acheived):";
cin >> weight1;

float grade2;
cout << "Enter the second grade:";
cin >> grade2;

float weight2;
cout << "Enter the weight of the second grade:";
cin >> weight2;

float grade3;
cout <<  "Enter your third grade:";
cin >> grade3;

float weight3;
cout << "The weight of it?:";
cin >> weight3;

float grade4;
cout << "You know what to do...:";
cin >> grade4;

float weight4;
cout << "Bet you're getting tired of this:";
cin >> weight4;

float grade5;
cout << "Enter the grade...:";
cin >> grade5;

float weight5;
cout << "If you don't know what to do now, you're stupid:";
cin >> weight5;

float grade6;
cout << "Grade please...:";
cin >> grade6;

float weight6;
cout << "Being a teacher can be annoying right, all these weights and grades. (Enter the weight):";
cin >> weight6;

float grade7;
cout << "Unless you're a student who's trying to figure out his grades. Trust me it can'be that bad. (Grade please, the 7th one):";
cin >> grade7;

float weight7;
cout << "Just keep working hard... Weight please:";
cin >> weight7;

float grade8;
cout << "Enter the 8th grade:";
cin >> grade8;

float weight8;
cout << "The weight of said grade?:";
cin >> weight8;

float grade9;
cout << "What's the 9th grade:";
cin >> grade9;

float weight9;
cout << "The weight of the 9th grade?:";
cin >> weight9;

float grade10;
cout << "The tenth grade? Enter it now:";
cin >> grade10;

float weight10;
cout << "Enter the weight of the tenth grade:";
cin >> weight10;

float grade11;
cout << "The 11th grade:";
cin >> grade11;

float weight11;
cout << "The weight of the 11th grade:";
cin >> weight11;

float grade12;
cout << "The 12th grade:";
cin >> grade12;

float weight12;
cout << "The weight of that  grade:";
cin >> weight12;

float grade13;
cout << "This is getting boring isn't it? The 13th grade please:";
cin >> grade13;

float weight13;
cout << "Just keep going I guess, The weight of the 13th grade please?";
cin >> weight13;

float grade14;
cout << "The 14th grade is?:";
cin >> grade14;

float weight14;
cout << "The weight of the 14th grade?:";
cin >> weight14;

float check;
check = weight1 + weight2 + weight3 + weight4 + weight5 + weight6 + weight7 + 
        weight8 + weight9 + weight10 + weight11 + weight12 + weight13 + weight14;


float under;
under = check;

float increase;
increase = 100 / under;


float averagedecimal1;
averagedecimal1 = average1 / 100;

float averagedecimal2;
averagedecimal2 = grade2 / 100;

float weightdecimal1;
weightdecimal1 = weight1 / 100;

float weightdecimal2;
weightdecimal2 = weight2 / 100;

float weighted1;
weighted1 = averagedecimal1 * weightdecimal1;

float weighted2;
weighted2 = averagedecimal2 * weightdecimal2;


float gradedecimal3;
gradedecimal3 = grade3 / 100;

float weightdecimal3;
weightdecimal3 = weight3 / 100;

float weighted3;
weighted3 = gradedecimal3 * weightdecimal3;


float gradedecimal4;
gradedecimal4 = grade4 / 100;

float weightdecimal4;
weightdecimal4 = weight4 / 100;

float weighted4;
weighted4 = gradedecimal4 * weightdecimal4;

float gradedecimal5;
gradedecimal5 = grade5 / 100;

float weightdecimal5;
weightdecimal5 = weight5 / 100;

float weighted5;
weighted5 = gradedecimal5 * weightdecimal5;

    float gradedecimal6;
    gradedecimal6 = grade6 / 100;

    float weightdecimal6;
    weightdecimal6 = weight6 / 100;

    float weighted6;
    weighted6 = gradedecimal6 * weightdecimal6;

    float gradedecimal7;
    gradedecimal7 = grade7 / 100;

    float weightdecimal7;
    weightdecimal7 = weight7 / 100;

    float weighted7;
    weighted7 = gradedecimal7 * weightdecimal7;

    float gradedecimal8;
    gradedecimal8 = grade8 / 100;

    float weightdecimal8;
    weightdecimal8 = weight8 / 100;

    float weighted8;
    weighted8 = gradedecimal8 * weightdecimal8;

    float gradedecimal9;
    gradedecimal9 = grade9 / 100;

    float weightdecimal9;
    weightdecimal9 = weight9 / 100;

    float weighted9;
    weighted9 = gradedecimal9 * weightdecimal9;

    float gradedecimal10;
    gradedecimal10 = grade10 / 100;

    float weightdecimal10;
    weightdecimal10 = weight10 / 100;

    float weighted10;
    weighted10 = gradedecimal10 * weightdecimal10;

    float gradedecimal11;
    gradedecimal11 = grade11 / 100;

    float weightdecimal11;
    weightdecimal11 = weight11 / 100;

    float weighted11;
    weighted11 = gradedecimal11 * weightdecimal11;

    float gradedecimal12;
    gradedecimal12 = grade12 /100;

    float weightdecimal12;
    weightdecimal12 = weight12 / 100;

    float weighted12;
    weighted12 = gradedecimal12 * weightdecimal12;

    float gradedecimal13;
    gradedecimal13 = grade13 / 100;

    float weightdecimal13;
    weightdecimal13 = weight13 / 100;

    float weighted13;
    weighted13 = gradedecimal13 * weightdecimal13;

    float gradedecimal14;
    gradedecimal14 = grade14 / 100;

    float weightdecimal14;
    weightdecimal14 = weight14 / 100;

    float weighted14;
    weighted14 = gradedecimal14 * weightdecimal14;


float weightedscore13;
weightedscore13 = weighted1 + weighted2 + weighted3 + weighted4 + weighted5 + 
                  weighted6 + weighted7 + weighted8 + weighted9 + weighted10 + 
                  weighted11 + weighted12 + weighted13 + weighted14;

    float last13;
    last13 = weightedscore13 * 100;

    float corrected;
    corrected = last13 * increase;

     cout << "Your weighted average is:";
cout << corrected;
cout << "%" <<endl;

cin.ignore();
cin.get();
return 0;

Every time I run this program, around the entering of the 1st, 2nd and sometimes 3rd grade the program crashes with the error 0x80000003 indicating memory corruption. I haven't been able to find a solution yet. All help is appreciated.

c++
asked on Stack Overflow Jun 18, 2015 by Shrinjay Mukherjee • edited Jun 18, 2015 by Michael Dorgan

1 Answer

0

Ok, you have done a ton of work to enter in all that info. Below, I have your same code, cleaned up a bit, and just perhaps, having it handle the weighted averaging better. In addition, I have added loops and placed all the strings into a single location to make it much easier if you wanted to add more "grades" and "weighted averages" going forward.

Notice how the for loops remove nearly all the redundent code that you were forced to enter. No more pages and pages of cin/cout. Also notice the arrays for your local variables making so much easier to handle entry in order. Also notice how they made the actual math very clean to read. Finally, notice the kNumGrades constant. This single variable now controls how many entries your code expects.


static const kNumGrades = 14;

int main(void)
{
    const char *userString[kNumGrades][2] =
    {
        {
            "Welcome to the weighted average calulator.\nPlease enter your first grade:",
            "Enter the weight (not the weight acheived):",
        },
        {
            "Enter the second grade:",
            "Enter the weight of the second grade:"
        },
        {
            "Enter your third grade:",
            "The weight of it?:",
        },
        {
            "You know what to do...:",
            "Bet you're getting tired of this:",
        },
        {
            "Enter the grade...:",
            "If you don't know what to do now, you're stupid:",
        },
        {
            "Grade please...:",
            "Being a teacher can be annoying right, all these weights and grades. (Enter the weight):",
        },
        {
            "Unless you're a student who's trying to figure out his grades. Trust me it can'be that bad. (Grade please, the 7th one):",
            "Just keep working hard... Weight please:",
        },
        {
            "Enter the 8th grade:",
            "The weight of said grade?:",
        },
        {
            "What's the 9th grade:",
            "The weight of the 9th grade?:",
        },
        {
            "The tenth grade? Enter it now:",
            "Enter the weight of the tenth grade:",
        },
        {
            "The 11th grade:",
            "The weight of the 11th grade:",
        },
        {
            "The 12th grade:",
            "The weight of that  grade:",
        },
        {
            "This is getting boring isn't it? The 13th grade please:",
            "Just keep going I guess, The weight of the 13th grade please?",
        },
        {
            "The 14th grade is?:",
            "The weight of the 14th grade?:",
        },
    };

    float grade[kNumGrades];
    float weight[kNumGrades];
    float weigthSum = 0;

    for(int i=0; i<kNumGrades; i++)
    {
        cout << userString[0][i];
        cin >> grade[i];

        cout << userString[1][i];
        cin >> weight[i];

        weigthSum += weight[i];
    }

    float weightedScore = 0;

    for(int i=0; i<kNumGrades; i++)
    {
        weightedScore += grade[i] * weight[i] / weigthSum;
    }

    cout << "Your weighted average is:";
    cout << weightedScore;
    cout << "%" << endl;

    cin.ignore();
    cin.get();

    return 0;
}
answered on Stack Overflow Jun 18, 2015 by Michael Dorgan

User contributions licensed under CC BY-SA 3.0