Like Tree1Likes
  • 1 Post By oogabooga

Grade Average passing in functions:

This is a discussion on Grade Average passing in functions: within the C++ Programming forums, part of the General Programming Boards category; My average is failing but I played and played with it and I still keep crashing. Code: #include <iostream> using ...

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

    Grade Average passing in functions:

    My average is failing but I played and played with it and I still keep crashing.
    Code:
     #include <iostream>
    
    using namespace std;
    
    void getScore(int score[], int NumGrades)
    {
        
        cout<<"How many grades do you need to enter?"<<endl;
        cin >> NumGrades;
        
        cout<<"Enter the Students Grade(s)"<<endl;
        for (int i=0; i<NumGrades; i++) {
        cin >> score[i];
        }
    }
    void printGrade()
    {
        int numGrade=NULL;
        int grades[numGrade];
        int average=0;
        int total=0;
        
        getScore(grades, numGrade);
        
        for (int i=0; i<numGrade; i++) {
        total+= grades[i];
        }
        average = total/numGrade;
        
        cout<<"The students average was a "<<average<<endl;
    }
    
    int main(int argc, const char * argv[])
    {
        
        printGrade();
        
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    418
    I did some more testing and now seen I am not passing getscore() to printGrade(). I just tried to print out grades in the printGrade function using a for statement and nothing.

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    you are allocating array of size 0 and expect it to be able to store some values?

    use std::vector instead and resize it before trying to populate it with values
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    418
    I dont event know what std::vector is. Looks like I have some reading to do.

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You can use an array if you want, although it's more C++y to use a vector.

    Your problem is that you don't pass back numGrades from getScore.
    * Take numGrades out of the getScore's arguments.
    * Make getScore return an int.
    * Declare numGrades as a local variable of getScore.
    * And return it at the end of the function.

    Also:
    * You should probably make average a double and cast one of the variables in its calculation to a double to ensure floating-point math.
    * Don't use NULL when you mean 0.
    * In fact, in C++, don't use NULL at all.
    Even for pointers, 0 is fine.
    For C++11 (which you should probably be using unless otherwise directed) use nullptr.
    However, since you're assigning to an int, you simply mean 0.
    Elysia likes this.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  6. #6
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    805
    I highly recommend learning how to use vectors. They are quite awesome and seem to be pretty fast too.

  7. #7
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    418
    Thank you. I will start to vigorously read about them.

    How different is C++11 from C++?

  8. #8
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,529
    It's quite different. C++11 added lots of containers, and new syntax, including: a new kind of for loop (range based), lambdas, initializer lists for containers, auto declarations, and all sorts of other things.

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,182
    It adds new containers too, such as std::array.
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,980
    Actually, C++11 is exactly the same as (standard) C++, since standard C++ currently means C++11, by definition. Perhaps you meant to ask how is C++11 different from C++03.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. average grade program
    By fyu2 in forum C Programming
    Replies: 3
    Last Post: 05-25-2012, 01:11 AM
  2. Help with finding the highest average grade
    By Rockie in forum C++ Programming
    Replies: 4
    Last Post: 11-10-2011, 10:55 AM
  3. Grade program average problem
    By Zaz in forum C Programming
    Replies: 1
    Last Post: 11-20-2009, 06:11 PM
  4. Student Roster & Grade Average
    By Surge in forum C Programming
    Replies: 10
    Last Post: 12-12-2006, 06:32 AM
  5. Drop Lowest Grade then Average
    By cmut in forum C Programming
    Replies: 4
    Last Post: 09-27-2002, 10:19 PM

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