0x80000003运行带有函数的程序时出错

时间:2015-06-18 16:46:27

标签: c++

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;

每次运行此程序时,在第1级,第2级和第3级的输入周围,程序崩溃,错误0x80000003表示内存损坏。我还没有找到解决方案。感谢所有帮助。

1 个答案:

答案 0 :(得分:0)

好的,你已完成了大量工作以输入所有信息。下面,我有相同的代码,清理了一下,也许,让它更好地处理加权平均。此外,我添加了循环并将所有字符串放在一个位置,以便在未来添加更多“等级”和“加权平均值”时更容易。

注意for循环如何删除几乎所有被强制输入的redundent代码。没有更多的页面和页面的cin / cout。还要注意本地变量的数组使得按顺序处理条目变得更加容易。还要注意他们如何使实际数学非常干净。最后,请注意kNumGrades常量。此单个变量现在控制代码期望的条目数。


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