Average score

This is a discussion on Average score within the C++ Programming forums, part of the General Programming Boards category; Code: TotalAverage + AverageDay = TotalAverage; TotalAverage + AverageEvening = TotalAverage;...

  1. #16
    T-Mac wiz23's Avatar
    Join Date
    Apr 2005
    Location
    Houston
    Posts
    48
    Code:
    TotalAverage + AverageDay = TotalAverage;
    TotalAverage + AverageEvening = TotalAverage;

  2. #17
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Quote Originally Posted by wiz23
    Code:
    TotalAverage + AverageDay = TotalAverage;
    TotalAverage + AverageEvening = TotalAverage;
    1) Those are not legal statements:
    To assign a variable a value, you do this:

    var1 = 20;
    var2 = 30 + 20 - 10;
    var3 = var1 + var2;

    Notice how the variable is always on the left of the equals sign when you assign it a value, AND it is the only thing on the left.
    2) I asked you this:
    What values are stored in TotalAverage1 and TotalAverage2? Post the lines of code where they are assigned values. Remember for a variable to be assigned a value it has to be on the left side of the equals sign
    where are TotalAverage1 and TotalAverage2 in your reply:

    TotalAverage + AverageDay = TotalAverage;
    TotalAverage + AverageEvening = TotalAverage;
    Last edited by 7stud; 04-30-2005 at 08:55 AM.

  3. #18
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    You need to set aside your program for awhile and try to write much shorter programs--no more than 5 - 10 lines. Before you can continue, you have to be able to write a program that does this:

    1) Declare a variable and initialize it to any value, say 100.
    2) Declare another variable and initialize it to, say 50.
    3) Add 5 to the first variable.
    4) Add 15 to the second variable.

    5) Display both variables to a console window.

    6) Add 20 and the second variable to the first variable.

    7) Display the first variable to the console window.
    Last edited by 7stud; 04-30-2005 at 08:54 AM.

  4. #19
    T-Mac wiz23's Avatar
    Join Date
    Apr 2005
    Location
    Houston
    Posts
    48
    I want to calculate the actual average score from these two input test files, one is called StudentsDay.txt and the other one is called StudentsEvening.txt
    StudentsDay.txt look like this:

    Code:
    Code:
    s1012 Anne Bennet       45 77 62 18 79
    s1236 Tony Bridges       81 83 89 95 63
    s2341 Michael Butcher   72 83 48 26 75
    StudentsEvening.txt look like this:

    Code:
    Code:
    s5454 Joe Sunshine      66 89 78 74 85
    s5466 Vanessa White   62 77 44 88 79
    s5553 Jim Sunder         66 74 48 53 95
    I have already got their individual average score from each of the student and now I only want to get the actual average from the 6 students average score. So this is what I meant:

    Code:
    Anne Bennet average is 56.20
    Tony Bridges average is 82.20
    Michael Butcher average is 60.80
    Joe Sunshine average is 78.40
    Vanessa While average is 70.00
    Jim Sunder average is 67.20
    The average of 6 students is 69.13

    Here's the code:

    Code:
    #include <fstream>
    #include <iomanip>
    #include <string>
    using namespace std;

    int main()
    {
    ifstream inFileDay;
    ifstream inFileEvening;
    ofstream outFile;

    string studentdayId;
    string studenteveningId;
    string givenname, surname;
    int score1, score2, score3, score4, score5;
    int studentcounter;
    double AverageDay, AverageEvening;
    string grade;

    float maxscore;
    float minscore;
    float allaverage = 0;
    char maxTime='D';
    char minTime='D';
    string maxGrade;
    string minGrade;
    string maxSID;
    string minSID;
    string maxName;
    string minName;

    maxscore=-1;
    minscore=101;

    inFileDay.open("studentsDay.txt");
    inFileEvening.open("studentsEvening.txt");
    outFile.open("report_regular.txt",std::ios::trunc) ;

    outFile.setf(ios::fixed, ios::floatfield);
    outFile.setf(ios::showpoint);
    outFile<<setprecision(2);

    outFile<<endl<<"+++++Section 1 Student List+++++"<<endl<<endl;

    while (!inFileDay.eof() )
    {
    inFileDay>>studentdayId;
    outFile<<studentdayId<<" ";
    inFileDay>>givenname>>surname;
    outFile<<surname<<", ";
    outFile<<givenname<<" ";
    outFile<<"D ";

    inFileDay>>score1>>score2>>score3>>score4>>score5;

    AverageDay=static_cast<double>(score1+score2+score 3+score4+score5)/5.0;

    outFile<<AverageDay<<" ";
    outFile.setf(ios::right);

    if(AverageDay>=85 && AverageDay<=100)
    grade="HD";
    else if(AverageDay>=75 && AverageDay<=85)
    grade="D ";
    else if(AverageDay>=65 && AverageDay<=75)
    grade="Cr";
    else if(AverageDay>=55 && AverageDay<=65)
    grade="P ";
    else
    grade="F ";

    outFile<<grade<<endl;

    if(maxscore<AverageDay)
    {
    maxscore=AverageDay;
    maxSID=studentdayId;
    maxName=givenname+" "+surname;
    maxGrade=grade;
    }
    if(minscore>AverageDay)
    {
    minscore=AverageDay;
    minSID=studentdayId;
    minName=givenname+" "+surname;
    minGrade=grade;
    }

    studentcounter++;
    }

    while (!inFileEvening.eof() )
    {
    inFileEvening>>studenteveningId;
    outFile<<studenteveningId<<" ";
    inFileEvening>>givenname>>surname;
    outFile<<surname<<", ";
    outFile<<givenname<<" ";
    outFile<<"E ";
    inFileEvening>>score1>>score2>>score3>>score4>>sco re5;

    AverageEvening=static_cast<double>(score1+score2+s core3+score4+score5)/5.0;

    outFile<<setw(2)<<AverageEvening<<" ";
    outFile.setf(ios::right);

    if(AverageEvening>=85 && AverageEvening<=100)
    grade="HD";
    else if(AverageEvening>=75 && AverageEvening<=85)
    grade="D ";
    else if(AverageEvening>=65 && AverageEvening<=75)
    grade="Cr";
    else if(AverageEvening>=55 && AverageEvening<=65)
    grade="P ";
    else
    grade="F ";

    outFile<<grade<<endl;

    if(maxscore<AverageEvening)
    {
    maxscore=AverageEvening;
    maxSID=studentdayId;
    maxName=givenname+" "+surname;
    maxGrade=grade;
    maxTime='E';
    }
    if(minscore>AverageEvening)
    {
    minscore=AverageEvening;
    minSID=studentdayId;
    minName=givenname+" "+surname;
    minGrade=grade;
    minTime='E';
    }

    studentcounter++;
    allaverage = (AverageDay++ + AverageEvening++)/6;
    }




    outFile<<"\n+++++Section 2 The Maximum+++++\n\n"
    <<maxName<<" ("<<maxSID<<", "<<maxTime<<") is my best student, with "
    <<"a score of "<<maxscore<<" ("<<maxGrade<<")\n"<<minName<<" ("<<minSID
    <<", "<<minTime<<"), you need to drop this class... you only got a "
    <<minscore<<" ("<<minGrade<<')'<<std::flush;
    outFile<<"The average of 6 students is" << allaverage << endl;
    return 0;
    }

    The purple text is the part I try to get the actual results from the 6 students average but I still could not get it. I have tried many times. Can somebody help me please?

  5. #20
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    Not only are you posting code without code tags, it also seems to be code containing old mistakes.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #21
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    I want to calculate the actual average score from these two input test files
    Unfortunately, you can't write programs unless you first learn how to assign a value to a variable. The short program I asked you to complete was meant to teach you how to do that.

  7. #22
    Registered User
    Join Date
    May 2005
    Posts
    6
    why is it when i run it, it outputs sumthing like this?
    as you can see, it outputs the last student of each txt twice
    Code:
    +++++Section 1 Student List+++++
    
    s1012 Bennet, Anne D 56.20 P 
    s1236 Bridges, Tony D 82.20 D 
    s2341 Butcher, Michael D 60.80 P 
    s3232 Smith, Ellisa D 83.40 D 
    s3232 Smith, Ellisa D 83.40 D 
    s5454 Sunshine, Joe E 78.40 D 
    s5466 White, Vanessa E 70.00 Cr
    s5553 Sunder, Jim E 67.20 Cr
    s6532 Kane, Robert E 64.00 P 
    s6533 Stanger, James E 82.00 D 
    s6533 Stanger, James E 82.00 D

  8. #23
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    Quote Originally Posted by henz321
    why is it when i run it, it outputs sumthing like this?
    as you can see, it outputs the last student of each txt twice
    Probably because you are testing eof to control your loop. This is bad... as already mentioned earlier in this thread.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extracting lowest and highest score from array
    By sjalesho in forum C Programming
    Replies: 6
    Last Post: 03-01-2011, 05:24 PM
  2. C program using structs to calculate grades
    By TampaTrinDM88 in forum C Programming
    Replies: 4
    Last Post: 07-06-2009, 12:33 PM
  3. trouble with creating a loop to read certain number of inputs
    By import tuner650 in forum C++ Programming
    Replies: 2
    Last Post: 03-20-2008, 07:28 PM
  4. Moving Average Question
    By GCNDoug in forum C Programming
    Replies: 4
    Last Post: 04-23-2007, 11:05 PM
  5. Debug Error Really Quick Question
    By GCNDoug in forum C Programming
    Replies: 1
    Last Post: 04-23-2007, 12:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21