please help me

This is a discussion on please help me within the C++ Programming forums, part of the General Programming Boards category; This is the code that is suppose to compute the class's average and standard diviation from a file which contains ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    12

    please help me

    This is the code that is suppose to compute the class's average and standard diviation from a file which contains a collection of student ids and corresponding scores from my computer class and assign each student a letter grade. the class can have no less than 7 students and no more than 25 student

    I need help with aeraging the grades

    Code:
    #include <fstream.h>
    #include <iostream.h>
    #include <stdlib.h>
    #include <iomanip.h>
    
    struct mike
    {
    int id;
    float average = 0;
    char lettergrade[1];
    int numgrade;
    int total = 0;
    }hope;
    
    void main()
    {
    ifstream MyData;
    MyData.open("getdata.dat");
    ofstream PutData;
    PutData.open("putdata");
    
    if(MyData.fail())
     {
            cout << "\n\nFile not successfully opened\n\n";
     }
    cout << "\n\nFile Successfully opened\n\n";
    
    int i=0;
    
    do{
    MyData >> hope.id >> hope.numgrade;
    
    cout << hope.id << "\t" << hope.numgrade << "\t" << hope.lettergrade << "\t" << hope.average;
    
    hope.total = hope.total + hope.numgrade;
    hope.average = hope.average/hope.total;
    
    if (hope.numgrade >= 90) strcpy(hope.lettergrade,"A");
    else if (hope.numgrade >= 80) strcpy(hope.lettergrade,"B");
    else if (hope.numgrade >= 70) strcpy(hope.lettergrade,"C");
    else if (hope.numgrade >= 60) strcpy(hope.lettergrade,"D");
    else strcpy(hope.lettergrade,"F");
    
    
    PutData << "\n\n\n    Class Grades";
    PutData << "\n";
    PutData << "               Student ID: " << hope.id << endl;
    PutData << "      Student Numbergrade: " << hope.numgrade << endl;
    PutData << "      Student Lettergrade: " << hope.lettergrade << endl;
    PutData << setiosflags(ios::fixed)
            << setiosflags(ios::showpoint)
            << setprecision(2);
    PutData << "            Class Average: " << hope.average << endl;
    }while(i>7 && i<25);
    
    MyData.close();
    PutData.close();
    }

  2. #2
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    First, this title does not meet the standards of our forums. Secondly, do a board search as this has been covered. At the very least be more specific as to where your problem is...

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    12

    Averaging problem

    Can Someone help me in finding the average when I compile my average stays at 0.00

    Code:
    // Project: This project is designed to compute class's average
    // and standard deviation from a file which contains a collection
    // of students' ids and corresponding scores for my computer class
    // Then it will assign each student a lettergrade as follows:
    //  100-90 = A, 89-80 = B, 79-70 = C, 69-60 = D, BELOW 60 = F
    
    #include <fstream.h>
    #include <iostream.h>
    #include <stdlib.h>
    #include <iomanip.h>
    
    
    struct mike
    { // declaring variables
    int id;
    float average;
    char lettergrade[1];
    int numgrade;
    }hope;
    
    // DEFINING MAIN FUNCTION
    void main()
    {
    ifstream MyData;
    MyData.open("getdata.dat");
    ofstream PutData;
    PutData.open("putdata");
    
    if(MyData.fail())
     {
            cout << "\n\nFile not successfully opened\n\n";
     }
    cout << "\n\nFile Successfully opened\n\n";
    
    int i;
    
    do{
    i++;
    MyData >> hope.id >> hope.numgrade;
    hope.average = hope.numgrade/i;
    
    PutData << "\n\n" << hope.id << "\t" << hope.numgrade << "\t" << hope.lettergrad
    e << "\t" << hope.average << endl;
    cout << "\n\n" << hope.id << "\t" << hope.numgrade << "\t" << hope.lettergrade <
    < "\t" << hope.average << endl;
    
    if (hope.numgrade >= 90)
    strcpy(hope.lettergrade,"A");
    else if (hope.numgrade >= 80) strcpy(hope.lettergrade,"B");
    else if (hope.numgrade >= 70) strcpy(hope.lettergrade,"C");
    else if (hope.numgrade >= 60) strcpy(hope.lettergrade,"D");
    else strcpy(hope.lettergrade,"F");
    
    PutData << "\n    Class Grades";
    PutData << "\n";
    PutData << "               Student ID: " << hope.id << endl;
    PutData << "      Student Numbergrade: " << hope.numgrade << endl;
    PutData << "      Student Lettergrade: " << hope.lettergrade << endl;
    PutData << setiosflags(ios::fixed)
            << setiosflags(ios::showpoint)
            << setprecision(2);
    PutData << "            Class Average: " << hope.average << endl;
    
    
    cout << "\n    Class Grades";
    cout << "\n";
    cout << "               Student ID: " << hope.id << endl;
    cout << "      Student Numbergrade: " << hope.numgrade << endl;
    cout << "      Student Lettergrade: " << hope.lettergrade << endl;
    cout << setiosflags(ios::fixed)
            << setiosflags(ios::showpoint)
            << setprecision(2);
    cout << "     Class average is " << hope.average << endl;
    cin.get();
    }while(i<25);
    
    MyData.close();
    PutData.close();

  4. #4
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>int i;
    i isn't initialized. It'll probably start out at about 2,000,000,000.

    >>hope.average = hope.numgrade/i;
    I have no idea what you're trying to do here. What is numgrade? What is i supposed to represent? How is the data laid out in the file? You'll have to provide a lot more information.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    82
    I'm getting this error in dev-c++:

    9 C:\Dev-Cpp\projects\main.cpp ISO C++ forbids initialization of member `average'

    Need to look up, but it seems reasonable that you can't assign a default value like that (outside of making it static, which is not what you want.

    That's a benefit of classes, the constructor can take care of stuff like that.

    Code:
    struct hope  // you're not hacking c anymore
    {
    int id;
    float average = 0;     // apparently not legal
    char lettergrade[1];
    int numgrade;
    int total = 0;
    };

  6. #6
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Indeed, you cannot initialize values like that. There is no need in C since classes do not exist and you can initialize variables manually after creation, but in C++ a mechanism has been provided for initializing members of structs/classes: Constructors.
    Code:
    struct hope
    {
    int id;
    float average;
    char lettergrade[1];
    int numgrade;
    int total;
    
    hope() :average(0.f), total(0) {}  //Initialize average and total to 0 on creation
    };
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  7. #7
    Registered User
    Join Date
    May 2003
    Posts
    82
    hmm... interesting. Trying to make this work, but getting a syntax error I don't understand.

    Code:
    #include <fstream>
    #include <iostream>
    #include <cstdlib>
    #include <iomanip>
    using namespace std;
    
    class hope
    {
          public:
        int id;
        float average;
        char lettergrade[1];
        int numgrade;
        int total;
        
        hope() {average = 0; total = 0;};
    };
    
    int main()
    {
    ifstream MyData;
    MyData.open("getdata.dat");
    ofstream PutData;
    PutData.open("putdata");
    
    if(MyData.fail())
     {
            cout << "\n\nFile not successfully opened\n\n";
     }
    else
    {
        cout << "\n\nFile Successfully opened\n\n";
        
        int i=0;
        
        do{
        MyData >> hope.id >> hope.numgrade; // syntax error here
        
        cout << hope.id << "\t" << hope.numgrade << "\t" << hope.lettergrade << "\t" << hope.average;
        
        hope.total = hope.total + hope.numgrade;
        hope.average = hope.average/hope.total;
        
        if (hope.numgrade >= 90) strcpy(hope.lettergrade,"A");
        else if (hope.numgrade >= 80) strcpy(hope.lettergrade,"B");
        else if (hope.numgrade >= 70) strcpy(hope.lettergrade,"C");
        else if (hope.numgrade >= 60) strcpy(hope.lettergrade,"D");
        else strcpy(hope.lettergrade,"F");
        
        
        PutData << "\n\n\n    Class Grades";
        PutData << "\n";
        PutData << "               Student ID: " << hope.id << endl;
        PutData << "      Student Numbergrade: " << hope.numgrade << endl;
        PutData << "      Student Lettergrade: " << hope.lettergrade << endl;
        PutData << setiosflags(ios::fixed)
                << setiosflags(ios::showpoint)
                << setprecision(2);
        PutData << "            Class Average: " << hope.average << endl;
        }while(i>7 && i<25);
    }
    
    MyData.close();
    PutData.close();
    }

  8. #8
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    I'm assuming you're getting an error that tells you that hope isn't an object. Well, that's because hope is the class/struct type, and you haven't declared an instance of hope anywhere. In the original code, the struct type was mike and an object called hope was created immediately.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  9. #9
    Registered User
    Join Date
    May 2003
    Posts
    82
    oh man....
    /smacks head.

    I'm going to sheepishly blame that on the naming scheme and the fact I was thinking about C.

    then this might work:
    Code:
    #include <fstream>
    #include <iostream>
    #include <cstdlib>
    #include <iomanip>
    using namespace std;
    
    struct mike
    {
        int id;
        float average;
        char lettergrade[1];
        int numgrade;
        int total;
        
        mike() : average(0.f), total(0) {};  // initialize members
    } hope;
    
    int main()      // int: not just a good idea, it's the law
    {
    ifstream MyData;
    MyData.open("getdata.dat");
    ofstream PutData;
    PutData.open("putdata");
    
    if(MyData.fail())
     {
            cout << "\n\nFile not successfully opened\n\n";
     }
    else              // do following if data stream opened
    {
        cout << "\n\nFile Successfully opened\n\n";
        
        int i=0;
        
        do{
        MyData >> hope.id >> hope.numgrade;
        
        cout << hope.id << "\t" << hope.numgrade << "\t" << hope.lettergrade << "\t" << hope.average;
        
        hope.total = hope.total + hope.numgrade;
        hope.average = hope.average/hope.total;   // apparently suspicious
        
        if (hope.numgrade >= 90)
        strcpy(hope.lettergrade,"A");
        else if (hope.numgrade >= 80) strcpy(hope.lettergrade,"B");
        else if (hope.numgrade >= 70) strcpy(hope.lettergrade,"C");
        else if (hope.numgrade >= 60) strcpy(hope.lettergrade,"D");
        else strcpy(hope.lettergrade,"F");
        
        
        PutData << "\n\n\n    Class Grades";
        PutData << "\n";
        PutData << "               Student ID: " << hope.id << endl;
        PutData << "      Student Numbergrade: " << hope.numgrade << endl;
        PutData << "      Student Lettergrade: " << hope.lettergrade << endl;
        PutData << setiosflags(ios::fixed)
                << setiosflags(ios::showpoint)
                << setprecision(2);
        PutData << "            Class Average: " << hope.average << endl;
        }while(i>7 && i<25);
    }
    
    MyData.close();
    PutData.close();
    }
    Last edited by AH_Tze; 02-10-2005 at 05:44 PM.

  10. #10
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    The problem is probably that numgrade and the counter i are both integers. Eventually your counter probably gets higher than one of the grade scores, resulting in 0. ie.. 5 / 6 == 0 in integer math.

    Additionally, staring at your code, I'm not entirely certain what you are trying to accomplish with
    Code:
    hope.average = hope.numgrade/i;
    It appears as if you're averaving each grade as it comes through.
    Last edited by Scribbler; 02-10-2005 at 05:10 PM.

  11. #11
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    For these lines:

    strcpy(hope.lettergrade,"A");

    You can do this instead:

    hope.lettergrade[0]='A';

    ...but if you use cout to display the lettergrade you also have to use array notation as well. You can't output the contents of a char array using just the name unless there is a '\0' in the last position of the array. So, you would do this:

    cout<<hope.lettergrade[0];
    Last edited by 7stud; 02-10-2005 at 05:04 PM.

  12. #12
    Registered User
    Join Date
    May 2003
    Posts
    82
    Post a valid data file so we can test it.

    I don't know about everyone else here, but I have trouble finding bugs by just staring at code and being extremely clever.

    And building off what 7stud said, if the grade string is just 1 character long, you could just use a char instead of an array of char's.
    If you want to support multiple characters, the STL string is always preferred.
    Last edited by AH_Tze; 02-10-2005 at 05:05 PM.

  13. #13
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    The problem is probably that numgrade and the counter i are both integers. Eventually your counter probably gets higher than one of the grade scores
    I don't see how that's possible since the scores are going to be 30, 50, 70, 90, etc., so a running total of scores should always be greater than the number of students. But, since the poster made the total a float, it seems they want a float for the total, so they should just make all the values floats.

    The average only needs to be calculated after the loop, and I would think all the output would be outside the loop as well, but maybe they are doing it that way for error checking at this point.

    Code:
    int i = 0;
    
    do{
    MyData >> hope.id >> hope.numgrade;
    i++;
    hope.total = hope.total + hope.numgrade;
    hope.average = hope.total/i;
    Last edited by 7stud; 02-10-2005 at 05:19 PM.

  14. #14
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    Dunno, I'm guessing he might have a really low score in there somewhere. If one of the last scores is < 20 it could happen. Who knows, maybe he gave somebody a single digit grade. I remember a lot of old class projects similar to this one, I'd just enter a bunch of random junk data (until I learned that a controlled environment was better for debugging).

    It's a logic error. Whether it's the cause in this case remains to be seen.
    Last edited by Scribbler; 02-10-2005 at 05:18 PM.

  15. #15
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Quote Originally Posted by Scribbler
    Dunno, I'm guessing he might have a really low score in there somewhere. If one of the last scores is < 20 it could happen.
    Code:
    90 70 60 50 10 --> total = 280
    
    1  2  3  4  5  --> i = 5       average = 280/5 = 56
    Last edited by 7stud; 02-10-2005 at 05:25 PM.

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

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