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

Thread: Weird average

  1. #1
    Join Date
    Oct 2009
    Posts
    14

    Weird average

    What's wrong with my code when i calculate the average? I set it up so
    average = sum of all scores divided by the number of scores.
    But it keeps on returning a number like 5.634543-e306 and i don't know why it's doing that.


    #include <iostream>
    #include <fstream>
    #include <iomanip>

    using namespace std;

    const int ARRAY_SIZE = 0;

    void score_reader ( ifstream& inFile, int&count);
    void printScores(const double list[], int listSize);
    double averageArray(const double list [], int listSize);




    int main()
    {
    int count = 0;
    int sub;
    int category;
    int student_number;
    double average;
    double scores[ARRAY_SIZE] ;
    string filename;
    ifstream inFile;
    ifstream inData;


    // Call function score_reader with scores1.txt
    cout << "What's the name of the input file?: " ;
    cin >> filename;

    inFile.open(filename.c_str () );

    if (!inFile)
    {
    cout << "NO!! THAT'S WRONG!! \n\n";
    system ("pause");
    return 1;

    }




    cout <<"Results:\n";

    printScores (scores, ARRAY_SIZE);
    score_reader (inFile, count);
    cout << endl;


    cout<<endl<<"The average test score is "<<average<<endl;

    cout << "\nNumber of students in the class: " << count << "\n\n";

    cout << "\n Class Average is =" << " "<< "\n\n";

    cout << "\nNumber of students in each Category: ";
    cout << endl;
    cout << endl;

    inFile >> category >> student_number;
    while (inFile )
    {
    cout << category << " " <<student_number << endl;
    scores[category -1] = scores[category -1] + student_number;
    inFile >> category >> student_number;
    }


    inFile.close ();


    system("pause");
    return 0;
    }



    void score_reader ( ifstream& inFile, int&count)
    {
    string first_name;
    string last_name;
    int score1;
    int score2;
    int score3;
    int score4;
    int score5;
    double scores[ARRAY_SIZE];





    while (inFile) //if not at end of file, continue reading numbers
    {
    //cout<<score_reader<<endl; //print numbers to screen
    inFile >> first_name;
    inFile >> last_name;
    inFile >> score1;
    inFile >> score2;
    inFile >> score3;
    inFile >> score4;
    inFile >> score5;
    count++;

    cout << last_name << "," << first_name <<"\t"<<score1 << "\t " << score2;
    cout << "\t " << score3 << "\t " << score4 << "\t " << score5;

    cout << endl;
    }
    inFile.close( ); //close file
    assert(inFile.fail( ));


    }




    void printScores(const double list[], int listSize)
    {
    int index;

    cout << "Name\t\tTest1\tTest2\tTest3\tTest4\tTest5\tAverage**\tGrade " <<endl << endl;

    for (index = 0; index < listSize; index++)
    cout << index + 1 << "\t\t\t\t\t\t\t\t" << list[index] << endl;

    }

    double averageArray(const double list [], int listSize)
    {
    double total = 0;

    for (int i = 0; i < listSize; i++)
    total = total + list[i];

    return (total/listSize);
    }

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Weird average

    const int ARRAY_SIZE = 0;

    Errr....zero?

  3. #3
    Join Date
    Oct 2009
    Posts
    14

    Re: Weird average

    Well, i'm going to be using more than one file that have different amounts of scores and people in it.
    One has 17 people, another has 4 and another has 12.
    So i thought that since it was like that there it can't equal anything but zero.

  4. #4
    Join Date
    Dec 2008
    Posts
    144

    Re: Weird average

    Quote Originally Posted by hougigo View Post
    Well, i'm going to be using more than one file that have different amounts of scores and people in it.
    One has 17 people, another has 4 and another has 12.
    You should use a vector in this case.

  5. #5
    Join Date
    Apr 2005
    Posts
    107

    Re: Weird average

    You never assign anything to 'average'. It's just trash on the stack.

  6. #6
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Weird average

    Arrays are sequences with fixed length. Once declared, they size cannot change. Lists are sequences with variable length. They can grow and shrink. I too suggest you use a vector. Here is a tutorial: http://www.codeguru.com/cpp/cpp/cpp_...cle.php/c4027/.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

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