Thread: File Input and mutator/accessor function help

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    16

    File Input and mutator/accessor function help

    Code:
    #include <iostream.h>
    #include <fstream.h>
    #include <conio.h>
    using namespace std;
    
    
    
    class Student
    {
    private:
    int hw_scores[10];
    int total_percent;
    char letter_grade;
    public:
    // Accessors
    int get_max_hw_score();
    int get_min_hw_score();
    void get_hw_scores(int hw[10]);
    int get_total_percent();
    char get_letter_grade();
    // Mutators
    void set_hw_scores (int hw[10]);
    // Constructor
    Student ();
    };
    
    //*************************************************************
    
    void main()
    {
            ifstream inFile;
            int my_scores[10];
            Student student1;
            // INSERT FILE INPUT DETAILS HERE
            inFile.open("my_scores.txt");
            if(!inFile)
            {
                    cout<<"ERROR!";
            }
            // Read in test scores.
            for (int x=0;x<10;x++)
            {
                    inFile >> my_scores[x];
            }
            student1.set_hw_scores(my_scores);
            // INSERT SCREEN OUTPUT STATEMENTS HERE
            cout<<"Your letter grade thus far is a(n): "<<student1.get_letter_grade();
            cout<<"\n\nPercentage: "<<student1.get_total_percent();
            cout<<"\n\nYour highest grade was a: "<<student1.get_max_hw_score();
            cout<<"\n\nYour lowest grade was a:  "<<student1.get_min_hw_score();
    
            in_file.close();
    
            getch();
    
    }
    
    //***********************************************************
    
    int Student::get_max_hw_score()
    {
            int max=hw_scores[0];
    
            for (int y=1; y<10; y++)
            {
                    if (hw_scores[y] > max)
                    max = hw_scores[y];
            }
            return max;
    }
    
    //------------
    
    int Student::get_min_hw_score()
    {
            int min=hw_scores[0];
    
            for (int x=1; x<10; x++)
            {
                    if (hw_scores[x] < min)
                    min = hw_scores[x];
            }
            return min;
    }
    
    //------------
    
    void Student::get_hw_scores(int hw[10])
    {
            for (int g=0;g<10;g++)
            {
                    hw_scores[g] = hw[g];
            }
    
    }
    
    //------------
    
    int Student::get_total_percent()
    {
    
            int sum=0;
    
            for (int q=1; q<10; q++)
            {
                    sum = sum + hw_scores[q];
            }
    
            total_percent=sum/10;
    
            if (total_percent>=90)
            {
                    letter_grade='A';
            }
                    else if (total_percent>=80)
                    {
                            letter_grade='B';
                    }
                    else if (total_percent>=70)
                    {
                            letter_grade='C';
                    }
                    else if (total_percent>=60)
                    {
                            letter_grade='D';
                    }
                    else if (total_percent<60)
                    {
                            letter_grade='F';
                    }
    
            return total_percent;
    }
    
    //------------
    
    char Student::get_letter_grade()
    {
            return letter_grade;
    }
    
    //------------
    
    void Student::set_hw_scores(int hw[10])
    {
            for (int h=0;h<10;h++)
            {
                    hw[h] = hw_scores[h];
            }
    
    }
    
    //------------
    
    Student::Student()
    {
            for (int b=0;b<10;b++)
            {
                    hw_scores[b] = 0 ;
            }
    
    }
    When I run the program, all my integers=0 and my char = nothing...

    Any suggestions would be greatly appreciated! Thanks!
    Last edited by Troll; 10-31-2005 at 01:37 PM.

  2. #2
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    hw[h] = hw_scores[h];

    should be the other way around
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    16
    Thank you so much!


    now there's only one more problem. the character grade that's supposed to be derived from the If-Else If statements isn't giving me a character....

    Thank you.

  4. #4
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    first of all, you aren't calling get_total_percent(), you could call this in the get_letter_grade function and return the value it gives you. Also, your for statement is off.

    Code:
    char Student::get_letter_grade()
    {
            get_total_percent();
            return letter_grade;
    }
    and the for statement needs to be:
    Code:
    for (int q=0; q<10; q++)
    {
            sum = sum + hw_scores[q];
    }
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    16
    Ok, lets try again with updated code....

    I'm still not getting a Char answer, or any answer for that matter for a letter grade.


    Code:
    #include <iostream.h>
    #include <fstream.h>
    #include <conio.h>
    using namespace std;
    
    
    
    class Student
    {
    private:
    int hw_scores[10];
    int total_percent;
    char letter_grade;
    public:
    // Accessors
    int get_max_hw_score();
    int get_min_hw_score();
    void get_hw_scores(int hw[10]);
    int get_total_percent();
    char get_letter_grade();
    // Mutators
    void set_hw_scores (int hw[10]);
    // Constructor
    Student ();
    };
    
    //*************************************************************
    
    void main()
    {
            ifstream in_file;
            int my_scores[10];
            Student student1;
            // INSERT FILE INPUT DETAILS HERE
            in_file.open("my_scores.txt");
            if(!in_file)
            {
                    cout<<"ERROR!";
            }
            // Read in test scores.
            for (int x=0;x<10;x++)
            {
                    in_file >> my_scores[x];
            }
            student1.set_hw_scores(my_scores);
            // INSERT SCREEN OUTPUT STATEMENTS HERE
            cout<<"Your letter grade thus far is a(n): "<<student1.get_letter_grade();
            cout<<"\n\nPercentage: "<<student1.get_total_percent();
            cout<<"\n\nYour highest grade was a: "<<student1.get_max_hw_score();
            cout<<"\n\nYour lowest grade was a:  "<<student1.get_min_hw_score();
    
            in_file.close();
    
            getch();
    
    }
    
    //***********************************************************
    
    int Student::get_max_hw_score()
    {
            int max=hw_scores[0];
    
            for (int y=0; y<10; y++)
            {
                    if (hw_scores[y] > max)
                    max = hw_scores[y];
            }
            return max;
    }
    
    //------------
    
    int Student::get_min_hw_score()
    {
            int min=hw_scores[0];
    
            for (int x=0; x<10; x++)
            {
                    if (hw_scores[x] < min)
                    min = hw_scores[x];
            }
            return min;
    }
    
    //------------
    
    void Student::get_hw_scores(int hw[10])
    {
            for (int g=0;g<10;g++)
            {
                    hw_scores[g] = hw[g];
            }
    
    }
    
    //------------
    
    int Student::get_total_percent()
    {
    
            int sum=0;
    
          for (int q=0; q<10; q++)
            {
                    sum = sum + hw_scores[q];
            }
    
            total_percent=sum/10;
    
            if (total_percent>=90)
            {
                    letter_grade='A';
            }
                    else if (total_percent>=80)
                    {
                            letter_grade='B';
                    }
                    else if (total_percent>=70)
                    {
                            letter_grade='C';
                    }
                    else if (total_percent>=60)
                    {
                            letter_grade='D';
                    }
                    else if (total_percent<60)
                    {
                            letter_grade='F';
                    }
    
            return total_percent;
    }
    
    //------------
    
    char Student::get_letter_grade()
    {
            return letter_grade;
    }
    
    //------------
    
    void Student::set_hw_scores(int hw[10])
    {
            for (int h=0;h<10;h++)
            {
                    hw_scores[h] = hw[h];
            }
    
    }
    
    //------------
    
    Student::Student()
    {
            for (int b=0;b<10;b++)
            {
                    hw_scores[b] = 0 ;
            }
    
    }

  6. #6
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    you did one thing I told ya to do, the for loop. Now do the other thing.
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

  7. #7
    Registered User
    Join Date
    Oct 2005
    Posts
    16
    Alrighty! I could have sworen I changed that also.

    Everything works as intented now.

    Thanks for the quick help! Have a great week!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  3. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  4. Totally confused on assigment using linked lists
    By Uchihanokonoha in forum C++ Programming
    Replies: 8
    Last Post: 01-05-2008, 04:49 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM