简体   繁体   中英

Logic error somewhere in code, cannot figure out where

Hello I'm writing a small program that allows the user to enter three test scores for a number of students than to compute student average, give a letter grade than give class average for each test once the user has given no (n) as an answer. For some reason I'm getting a very large number for total test 3 (tottest3) I've been looking at the code for almost an hour and a half now and I can't find the error.

the formulas for total test 1 to three are all identical, would the problem lie else where?

here is what I've tried doing so far:

-changing the average variable

-changing the class average to merely the total average

-adding test 1 or test 2 numbers to total test 3 instead of test 3 .

here is the output on command prompt.

Assignment #13

Hello Student #1

Please input test 1 Grade 1

Please input test 2 Grade 1

Please input test 3 Grade 1

Your Average is: 1

Your grade is F

Do you want to grade another student? (y/n) n

here are the averages for tests 1-3

average for test 1 is: 1

average for test 2 is: 1

average for test 3 is: 335796348684545909624128047159363492209569460693822234007169656776676343892387006827439949582508669307827616982407178504474449980239883634115896048874405460441709614779949982258368367180374307657350038129751972987645105499446376379659139781953069252608


Process exited with return value 0

Press any key to continue . . .

#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;

int main ()
{

cout<<"Jon Grezda CS 12 Wednesday 6-8pm"<<endl;
cout<<"Assignment #13"<<endl;

double test1, test2, test3, average; //test1/2/3, test scores, Average: average of test scores
double tottest1, tottest2, tottest3, avg1, avg2, avg3; //tottest1/2/3, avg# average for first second or third test
int student=0, avgvar; //average variable, Student number
char grade, ans; // Holds a letter grade, holds a response to a question

do{
student=student+1;
    cout<<"Hello Student #"<<student<<endl;

    cout<<"Please input test 1 Grade ";
        cin>> test1;
    cout<<"Please input test 2 Grade ";
        cin>> test2;
    cout<<"Please input test 3 Grade ";
        cin>> test3;
average=(test1+test2+test3)/3.0;

    cout<<setprecision(0)<<fixed;
    cout<<"Your Average is: "<<average<<endl;

tottest1=tottest1+test1;
tottest2=tottest2+test2;
tottest3=tottest3+test3;

 // Determine the letter grade. What grade will be assigned?
if (average > 0 && average < 60)

    grade = 'F';

else if (average >= 61 && average < 70)

    grade = 'D';

else if (average >= 70 && average < 80)

    grade = 'C';

else if (average >= 80 && average < 90)

    grade = 'B';

else if(average >= 90 && average <101)

    grade = 'A';

else 
{

    cout << "We do not give scores higher than 100 or lower than 0.\n"; // Is the score valid?

        grade = '-';
}
    cout << "Your grade is " << grade << endl;

        cout<<"Do you want to grade another student? (y/n)";
        cin>>ans;
    cout<<"\n";
} while(ans=='y');

if (ans=='n')
{
avgvar=student;
avg1=tottest1/avgvar;
avg2=tottest2/avgvar;
avg3=tottest3/avgvar;
    cout<<setprecision(0)<<fixed;
    cout<<"here are the averages for tests 1-3\n";
    cout<<"average for test 1 is: "<<avg1<<endl;
    cout<<"average for test 2 is: "<<avg2<<endl;
    cout<<"average for test 3 is: "<<avg3<<endl;
}
}
/*

*/

Your trouble happens in these lines;

tottest1=tottest1+test1;
tottest2=tottest2+test2;
tottest3=tottest3+test3;

You declared the variables tottest1, tottest2, and tottest3 but you did not give them a value. Your program has set aside some memory for those variables but it has not placed a value into those spots of memory so they are just filled with garbage. When you try to assign values to the variables by adding test1/2/3 to them, you add something meaningful to whatever garbage was stored in those uninitialized slots of memory. The result is something meaningless.

Solution: When you declare a variable, make sure to initialize it with some value, eg, 0.

It doesn't look like you're initializing your variables. Do that and you'll get more predictable results. Probably set them to 0 initially.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM