Pass by reference

This is a discussion on Pass by reference within the C++ Programming forums, part of the General Programming Boards category; Ok, I am really stuck, and need major help. Ths assignment i am working on is as follows: Its american ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    6

    Pass by reference

    Ok, I am really stuck, and need major help. Ths assignment i am working on is as follows:

    Its american idol. Design a function getJudgeData that asks the user for a judge's score, stores it in a reference parameter variable, and validates it. This function should be called by your function main() once for each of the 5 judges.

    I have my class built i am just working on the main.cpp file...

    I would REALLY appiciated ANY help. I am having trouble passing it by reference by using the Class Stat. I cant get the getJudgeData to put the data and store it into the class. Does anybody have ANY suggestions PLEASE? I can post the class if you need to see it I want to take the s.getNum(); and store the scores into there.
    Code:
    #include <iostream>
    #include "Stat.h"
    
    using namespace std;
    
    void getJudgeData(int &);
    
    double calcScore();
    
    
    int main()
    {
        int score1 = 0, score2 = 0, score3=0, score4=0, score5=0;
        Stat s;
    
    	cout << "This program assignment number 4 was created by Jeremy Rice" << endl;
    	cout<<  "The Next American Idol!!!"<<endl;
        cout<< "Please enter your scores below!"<<endl; 
    
    
    cout<<"Judge #1 what is your score"<<endl;
    getJudgeData(score1);
    cout<<"Judge #2 what is your score"<<endl;
    getJudgeData(score2);
    cout<<"Judge #3 what is your score"<<endl;
    getJudgeData(score3);
    cout<<"Judge #4 what is your score"<<endl;
    getJudgeData(score4);
    cout<<"Judge #5 what is your score"<<endl;
    getJudgeData(score5);
    
    
    
    system("pause");
    	return 0;
    }
    
    void getJudgeData(int &score)
    {
         Stat s;
        s.getNum("");
        
        
    }
    
    // The average is the min and max subtracted
    // the remaining three numbers are calculated for average
    /*double calcScore()
    {
           Stat s;
           int average = 0;
           average(s.getSum - s.getMin - s.getMax)/3;
    }
    */

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Sorry, but I don't understand the problem. You're having trouble passing by reference? Looks relatively fine so far to me.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    What does Stat look like (show us the stat.h header file)? What is its purpose?
    Code:
    void getJudgeData(int &score)
    {
         Stat s;
        s.getNum("");
        
        
    }
    
    // The average is the min and max subtracted
    // the remaining three numbers are calculated for average
    /*double calcScore()
    {
           Stat s;
           int average = 0;
           average(s.getSum - s.getMin - s.getMax)/3;
    }
    */
    Those are locals, they get destroyed once the functions they are used in are finished. If your Stat class needs to get passed around to the getJudgeData function they will need to be declared in a broader scope, such as main... and depending on what you are trying to do, you may need one of them, or one for each judge's score. That's also not the way to set the average variable. Finally, those functions do not either return a value or do anything with the passed in reference.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    6
    Yeah the double calc function i am not working on atthe moment, mainly on the getJudgeData heres the stat.h file.

    What i want to do is use the stat class to keep track of the 5 judges scores. I cant modify my stat class any further, he has a copy now and said if its modified i get a 0... lol gay.


    Code:
    // FILENAME:        Stat.h
    //
    // CLASS PROVIDED:  Stat
    
    #ifndef STAT_H
    #define STAT_H
    
    #include <string>
    
    using namespace std;
    
    class Stat {
    
      // ATTRIBUTES /////////////////////////////////////////////////////////
    
      private:
         int      thenum; // last integer entered
         unsigned count;  // number of integers entered so far
         int      sum;    // sum of all integers entered so far
         long     prod;   // product of all integers entered so far
         int      min;    // minimum of all integers entered so far
         int      max;    // maximum of all integers entered so far
         unsigned evens;  // count of evens of all integers entered so far
         unsigned odds;   // count of odds  of all integers entered so far
    
      // OPERATIONS /////////////////////////////////////////////////////////
    
      public:
         Stat( );
      // - default constructor
         void setNum ( int newNum );
      // - sets the last integer entered to newNum
         void getNum ( string prompt );
      // - displays prompt and reads in another integer
         void reset (  );
      // - resets all statistical values
         int getNum (  );
      // - returns the last integer entered
         unsigned getCount (  );
      // - returns the number of integers entered so far
         int getSum (  );
      // - returns the sum of all integers entered so far
         long getProd (  );
      // - returns the product of all integers entered so far
         int getMin (  );
      // - returns the minimum of all integers entered so far
         int getMax (  );
      // - returns the maximum of all integers entered so far
         float getAverage (  );
      // - returns the average of all integers entered so far
         unsigned getEvens (  );
      // - returns the number of even integers entered so far
         unsigned getOdds (  );
      // - returns the number of odd integers entered so far
    
    };
    
    #endif

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    6
    these are the requirements of the getJudgeData function:

    Design and implement a function void getJudgeData() that asks the user for a judge's score, stores it in a reference parameter variable, and validates it. This function should be called by your function main() once for each of the 5 judges.

    Use the Stat class to keep track of the five judges' scores. Do NOT modify the Stat class --- you should be able to use it as is!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. C OpenGL Compiler Error?
    By Matt3000 in forum C Programming
    Replies: 12
    Last Post: 07-07-2006, 04:42 PM
  4. how can i pass by reference by "malloc 2d array"?
    By Mathsniper in forum C Programming
    Replies: 10
    Last Post: 05-22-2005, 02:23 PM
  5. Ask about function parameters and pass a reference.
    By ooosawaddee3 in forum C++ Programming
    Replies: 1
    Last Post: 11-04-2002, 11:14 AM

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