Like Tree2Likes
  • 1 Post By ZuK
  • 1 Post By Elysia

Vectors:

This is a discussion on Vectors: within the C++ Programming forums, part of the General Programming Boards category; I am trying to make a grade book and using a vector to get the grades. I am getting errors ...

  1. #1
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    465

    Vectors:

    I am trying to make a grade book and using a vector to get the grades. I am getting errors all over and I figured this would happen because this is the first time I ever used vectors.

    Code:
     
    #ifndef ___2B__Homework__LetterGrade__
    #define ___2B__Homework__LetterGrade__
    
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    class GradeBook {
        
    public:
        void getGrades()
        {
            int numGrades=0;
            int checker;
            
            cout<< " Enter the students Grades and Enter -1 to stop. "<<endl;
            
            while (checker != -1) {
            cin>>checker;
            ++numGrades;
                
            vector<int> grades(numGrades);
                for (int i =0; i<numGrades; ++i) {
                    cin>>grades[i];
                    
                    averageGrades(grades[i],numGrades);
                }
            }
        
        }
        
        double averageGrades(int grades[],int num)
        {
            double total;
            double average;
            
            for (int i=0; i<num; ++i) {
                total += grades[i];
            }
            
            average = total / num;
            
            return average;
        }
        void letterGrade()
        {
            
        
        }
        
    private:
        
        
    };
    
    #endif
    Last edited by jocdrew21; 09-15-2013 at 11:32 AM.

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
                    averageGrades(grades[i],numGrades);
    averageGrades() expects a pointer to int, you're passing an int

    Better would be to pass a const reference to vector<int> to averageGrades(). This way you wouldn't have to pass the size.
    btw: move the call to averageGrades() out of the input loop.

    Kurt
    King Mir likes this.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,442
    How many students?
    How many grades per student?

    Entity–relationship model - Wikipedia, the free encyclopedia
    Before you even reach for the keyboard, you need to grab a pencil and some paper, READ your assignment carefully through several times and start to sketch out some details.

    Such as
    A class is composed of 0 or more students.
    A student has 1 or more grades.
    A department has ....
    A professor has ....

    Only then can you start to assign meaningful class names (in code), along with the appropriate methods and data.
    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.

  4. #4
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    465
    I was calling the function wrong. I decided to change a few things too. Still have a error in main though and the program is not done I was just testing it to make use average is sent to letterGrade(). The error is on line 61 and says "too few arguments to call function.

    Code:
     #include <iostream>
    #include <vector>
    
    using namespace std;
    
    class GradeBook {
        
    public:
        double getGrades()
        {
            int numGrades=0;
            int checker;
            double total=0.0;
            
            cout<< "Enter the students Grades and enter -1 to stop entering. "<<endl;
            
            while (checker != -1) {
                cin>>checker;          //stops program if -1 is entered
                ++numGrades;        // counts the amount of entries for the vector
                
                vector<int> grades(numGrades);
                for (int i =0; i<numGrades; ++i) {
                    cin>>grades[i];
                    
                    total += grades[i];
                }
                averageGrades(total, numGrades);
            }
            return total;
        }
        
        double averageGrades(double total , int num)
        {
            
            double average=0.0;
            
            average = total / num;
            
            letterGrade(average);
            
            return average;
        }
        
        void letterGrade(double average)
        {
            cout<<average<<endl;
            
        }
        
    private:
        
        
    };
    
    int main(int argc, const char * argv[])
    {
        GradeBook GradeTracker;
        
        GradeTracker.getGrades();
        
        GradeTracker.letterGrade();
        
        return 0;
    }

  5. #5
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    465
    This is why i choose to use a vector but didn't know how to do it. So I just write sizeof() in the next function and it will be good to go?

  6. #6
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    465
    Salem, your right and yes I do need to work on that. However this time I knew there was no way for me to tell any of the above. So i decided to let the user tell the program.

  7. #7
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    465
    Code:
     #include <iostream>
    #include <vector>
    
    using namespace std;
    
    class GradeBook {
        
    public:
        double getGrades()
        {
            
            double total=0.0;
            int amount;
            
            cout<<"Enter the amount of grades you need to enter"<<endl;
            cin>>amount;
            cout<< "Enter the students Grades: "<<endl;
            
                for (int i =0; i < amount; ++i) {
                    
                    vector<int> grades(amount);
                    cin>>grades[i];
                    
                    total += grades[i];
                }
                averageGrades(total, amount);
            
            return total;
        }
        
        double averageGrades(double total , int num)
        {
            
            double average = 0.0;
            
            average = total / num;
            
            return average;
        }
        
        void letterGrade()
        {
            double grade=0.0;
            int number=0;
            
            cout<<averageGrades(grade, number)<<endl; // test to see if values are being passed
        }
        
    private:
        
        
    };
    
    int main(int argc, const char * argv[])
    {
        GradeBook GradeTracker;
        
        GradeTracker.getGrades();
        
        GradeTracker.letterGrade();  // this is returning "nan"
        
        return 0;
    }
    I changed it up a little. I am just testing at this point to see is my values are being passed correctly and they are not. What am I doing wrong?

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,442
    > What am I doing wrong?
    You're writing code by trial and error rather than sitting down with pen and paper to come up with some kind of plan.

    Big issues you have at the moment.
    1. Your class has no member data, so there is no information being passed from say.
    GradeTracker.getGrades();
    to
    GradeTracker.letterGrade();

    2. You're returning results, but ignoring those results.

    Stop hacking code by guessing and come up with a plan. At this stage, any plan is better than what you're doing at the moment.
    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.

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    2,021
    On an unrelated note:
    Code:
    #ifndef ___2B__Homework__LetterGrade__
    #define ___2B__Homework__LetterGrade__
    C++ forbids identifiers that contain double underscore in a row; those are reserved fro the implementation. Also, FYI, identifiers that start with an underscore followed by a capital letter are similarly forbidden.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  10. #10
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    on line 21 you are creating vector INSIDE the loop.

    So you create vector for each iteration and then initialize only one item of the vector.

    If you need only one int - use one int and not vector.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  11. #11
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    465
    Thank you Vart. I way so focused on using vectors that I missed the simple solution. All the functions are working but the letterGrade(). I am still testing it right now but the values wont print. This function will eventually convert the average to a letter grade. Another thing about my classes. Is it ok if I do not put any variables in private, since I am not returning a values in main() to the functions?

    Code:
     #include <iostream>
    #include <vector>
    
    using namespace std;
    
    class GradeBook {
        
    public:
        void getGrades()
        {
            
            double total=0.0;
            int amount = 0;
            int grades;
            
            cout<<"Enter the students grades and -1 to stop entering"<<endl;
            
            while (grades != -1) {
            
            ++amount;
                
            cin>>grades;
                
            total += grades;
                
            averageGrades(total, amount);
        }
        }
        double averageGrades(double total , int num)
        {
            
            double average = 0.0;
            
            average = total / num;
            return average;
        }
        
        void letterGrade()
        {
            double grade=0.0;
            int number=0;
            double average;
            average=averageGrades(grade,number);
            
            cout<<average<<endl; // test to see if values are being passed
        }
        
    private:
        
        
    };
    
    int main(int argc, const char * argv[])
    {
        GradeBook GradeTracker;
        
        GradeTracker.getGrades();
        
        GradeTracker.letterGrade();  // this is returning "nan"
        
        return 0;
    }

  12. #12
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    when you get to line 18 for the first time grades is not initialized - so behavior is undefined.

    You need to increase warning level and pay attention to the compiler warnings

    Your letterGrade function calls avareageGrades with amount 0 which results in division by 0...

    You need somehow store the total and amount calculated by GetGrades in the member variable...
    Last edited by vart; 09-16-2013 at 02:29 AM.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,428
    Quote Originally Posted by jocdrew21 View Post
    Is it ok if I do not put any variables in private, since I am not returning a values in main() to the functions?
    You can do that, but that's missing the point of classes. A class is an object and an object encapsulates state.
    If it does not encapsulate state, then it isn't distinguishable from global functions, so what is the point?
    Think of it as a book: the book contains writing. The writing is a state in the book. So your GradeBook should contain grades as state.
    It should not ask for such things because a) it reduces flexibility in using the object and b) it isn't natural (have you met a book that speaks?).
    Instead, the normal approach is that you use setters and getters. The main program asks for a grade and then puts it in the grade book.
    And again, your problem is basically that you remember no state. First you get the grades, then you forget them, then you try to average them and print them. How does that work? You need variables to maintain state such as grades.
    MutantJohn likes this.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vectors in vectors - pointers and iterators
    By fisherking in forum C++ Programming
    Replies: 8
    Last Post: 07-27-2010, 09:34 AM
  2. About vectors.
    By KgNe in forum C++ Programming
    Replies: 23
    Last Post: 09-03-2008, 10:33 PM
  3. 2D Vectors
    By Coding in forum C++ Programming
    Replies: 14
    Last Post: 01-13-2008, 09:56 AM
  4. Help using Vectors
    By earth_angel in forum C++ Programming
    Replies: 3
    Last Post: 07-11-2005, 01:37 PM
  5. help with vectors please.
    By axlton in forum C++ Programming
    Replies: 6
    Last Post: 08-03-2003, 09:58 PM

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