Fantasy Calculator

This is a discussion on Fantasy Calculator within the C++ Programming forums, part of the General Programming Boards category; I am trying to create a program to calculate the Fantasy performances of my players, but I am comming across ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    15

    Fantasy Calculator

    I am trying to create a program to calculate the Fantasy performances of my players, but I am comming across some problems. Here is my Code so far.
    Code:
    #include <iostream>
    
    using namespace std;
    
    class RunningBack
    {
          public:
            int GetRush(int);
            void SetRush(int rush);
          private:
            int itsRush;
    };
    
    int RunningBack::GetRush(int rush)
    {
        cin >> rush;
        return itsRush;
    }
    
    void RunningBack::SetRush(int rush)
    {
         itsRush = rush;
    }
    
    int main()
    {
        RunningBack Back1;
        cout<< "Back number 1 ran for " ;
        cout << Back1.GetRush() << " yards today\n";
          cin.get();
          return 0;
    }
    The bolded portion gives me the error "no matching function to call RunningBack::GetRush(int)"

    I can't get it to display the yards that you input. Also how do I convert these yards to points. The points work like this: for every 20 yards you get 1 point. So if the user inputs 97 it should round off to 4 points.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >The bolded portion gives me the error "no matching function to call RunningBack::GetRush(int)"
    GetRush takes an argument, but you call it as if it doesn't. Most likely you'll want to change your declaration to take no arguments and then do this:
    Code:
    int RunningBack::GetRush()
    {
        int rush;
        cin >> rush;
        return itsRush;
    }
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    15
    Code:
    #include <iostream>
    
    using namespace std;
    
    class RunningBack // Class RunningBack Defined
    {
          // Setting up "Rush"
          public:  
            int GetRush();
            void SetRush(int rush);
            int itsRush;
          private:
            
    };
    
    int RunningBack::GetRush()
    {
        int rush 
        cin >>  rush; //Error 'rush' does not name a type
        return itsRush;
    }
    
    void RunningBack::SetRush(int rush)
    {
         itsRush = rush;
    }
    
    int main()
    {
        
        RunningBack Back1;
        cout<< "Back number 1 ran for " ;
        cout << Back1.itsRush << " yards today\n";
          cin.get();
          return 0;
    }
    With that error it won't let me compile, sorry for not knowing much, but I'm tryin to build my first helpful program and I read about how classes work and worked with them but it allways gave examples when you input the values through the compiler and not from a user input.

    Also if anyone has any suggestions in how to work the math I posted above it would be great.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    You are missing a semi-colon.

    For the math, you just use integer division.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    int rush
    cin >> rush;


    put a semicolon after rush in first line.

    However, both of the above lines are superfluous and can be removed without penalty.

    A more sophisticated approach would be to have itsRush be private and then call GetRush() when you want to display itsRush outside of of a class method, for example, like where you want to display it in main(). Have user enter value for itsRush for any given RunningBack object by calling SetRush() from within main(), or any other function other than a method of the class.

    Code:
    int main()
    {
       RunningBack Back1;
       int rush;
        cout << "how many yards did Back1 have?" << endl;
        cin >> rush;
        Back1.SetRush(rush);
    
        cout << Back1.GetRush(); 
    }
    Last edited by elad; 09-25-2006 at 02:46 PM.
    You're only born perfect.

  6. #6
    Registered User
    Join Date
    Aug 2006
    Posts
    15
    I know its integer devision, but whats the full line of code to make it so 'rush' is devided by 20 and rounded down.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    Just divide it by 20. Integer division rounds down (towards zero) automatically.

  8. #8
    Registered User
    Join Date
    Aug 2006
    Posts
    15
    Allright heres what I got so far:
    Code:
    #include <iostream>
    
    using namespace std;
    
    class RunningBack // Class RunningBack Defined
    {
          // Setting up "Rush"
          public:  
            int GetRush();
            void SetRush(int rush);
            int GetPass();
            void SetPass(int pass);
            int GetTD();
            void SetTD(int TD);
            int GetFumb();
            void SetFumb(int fumb);
          private:
            int itsRush;
            int itsPass;
            int itsTD;
            int itsFumb;
    };
    
    int RunningBack::GetFumb()
    {
        return itsFumb;
    }
    
    int RunningBack::GetTD()
    {
        return itsTD;
    }
    
    int RunningBack::GetPass()
    {
        return itsPass;
    }
    
    int RunningBack::GetRush()
    {
        return itsRush;
    }
    
    void RunningBack::SetFumb( int fumb )
    {
         itsFumb = fumb;
    }
    
    void RunningBack::SetTD( int TD )
    {
         itsTD = TD;
    }
    
    void RunningBack::SetPass( int pass )
    {
         itsPass = pass;
    }
    
    void RunningBack::SetRush( int rush )
    {
         itsRush = rush;
    }
    
    
    int main()
    {
        
        RunningBack Back1;
        int rush;
        int x;
        int pass;
        int TD;
        int total;
        int fumb;
        cout<< "How many yards did back number one have? " << endl;
        cin >> rush;
        rush = rush / 20;
        Back1.SetRush(rush);
        cout << "Back number one rushed for " << Back1.GetRush() << " point(s) rushing today\n";
        cout << "------------------------------------------------------------\n\n\n" << endl;
        cout << "How many yards receiving did back number one have? " << endl;
        cin >> pass;
        pass = pass / 20;
        Back1.SetPass(pass);
        cout << "Back number one caught for " << Back1.GetPass() << " point(s) receiving today\n";
        cout << "-------------------------------------------------------------\n\n\n" << endl;
        cout << "How many touchdowns did back number one have? " << endl;
        cin >> TD;
        TD = TD * 6;
        Back1.SetTD(TD);
        cout << "Back number one scored " << Back1.GetTD() << " point(s) in touchdowns today\n";
        cout << "-------------------------------------------------------------\n\n\n" << endl;
        cout << "How many fumbles did back number one have? " << endl;
        cin >> fumb;
        fumb = fumb * 2;
        Back1.SetFumb(fumb);
        cout << "Back number lost " << Back1.GetFumb() << " point(s) on fumbles today\n";
        cout << "-------------------------------------------------------------\n\n\n" << endl;
        total = rush + pass + TD - fumb;
        cout << "Total points for back number one is " << total << endl;
        cin >> x;
          return 0;
          }
    So what my question is, are there any sub classes that I can make so that Runningback is a subclass to a larger category like "Players". Then I can make subclasses like WideReceivers and other sub classes.

    Also if you see any bad programming practices or bad code please tell me.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    The SetRush should set the number of yards, and then you should have a GetRushPoints() that does the calculation inside the class.

    Yes, a Player class is good, especially if all players share the same traits (like Rushing yards, Receiving yards, touchdowns, fumbles, etc). If they don't share the same trait, you can still give them a base class with an interface they do share, like GetTotalPoints, GetName, etc.

  10. #10
    Registered User
    Join Date
    Aug 2006
    Posts
    15
    This may seem like I'm just asking for answers, but I tried a couple of times and don't understand what you mean by making a playerclass and putting them in. Do i rename "class RunningBack" to player or do something else?

  11. #11
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    This type of communication is not the best for discussing topics that take up full chapters (or more) in books. If you don't have a good book on C++ and you want to continue working with the language I strongly encourage you to get one.

    The process of endowing one class with the properties of another is called inheritance. Say all players have a name, position, and points. Each position derives points from different activities. You could then delcare a base class containing members called name, position, and points. Derive separate classes for each position allowed. That means each position will inherit the same three members from the base class. In addition, within each derived class you can detail how many points come from what activities. Then declare a container of pointer to base class to allow the program to evaluate different derived classes based on the base class members (taking advantage of a process called polymorphism)---for example, who had the most points, etc.
    You're only born perfect.

  12. #12
    Registered User
    Join Date
    Aug 2006
    Posts
    15
    I cleaned up some code up to make it easier for me to post it here, but I came up with a problem when I wanted to ask the user if he wanted to check Running Back number two's stats. I don't know what I did wrong(might just be a mistake that I just cant catch) but I have done many for loops but none like this. Dev C++ gave me some error messages.
    Code:
    #include <iostream>
    
    using namespace std;
    
    class RunningBack // Class RunningBack Defined
    {
          // Setting up "Rush"
          public:  
            int GetRush() const { return itsRush; }
            void SetRush(int rush) { itsRush = rush; }
            
            int GetPass() const { return itsPass; }
            void SetPass(int pass) { itsPass = pass; }
            
            int GetTD() const { return itsTD; };
            void SetTD(int TD) { itsTD = TD; }
            
            int GetFumb() const { return itsFumb; }
            void SetFumb(int fumb) { itsFumb = fumb; }
          private:
            int itsRush;
            int itsPass;
            int itsTD;
            int itsFumb;
    };
    
    int main()
    {
        
        RunningBack Back1;
        RunningBack Back2;
        int rush;
        int x;
        int pass;
        int TD;
        int total;
        int fumb;
        cout<< "How many yards did back number one have? " << endl;
        cin >> rush;
        rush = rush / 20;
        Back1.SetRush(rush);
        cout << "Back number one rushed for " << Back1.GetRush() << " point(s) rushing today\n";
        cout << "------------------------------------------------------------\n\n\n" << endl;
        cout << "How many yards receiving did back number one have? " << endl;
        cin >> pass;
        pass = pass / 20;
        Back1.SetPass(pass);
        cout << "Back number one caught for " << Back1.GetPass() << " point(s) receiving today\n";
        cout << "-------------------------------------------------------------\n\n\n" << endl;
        cout << "How many touchdowns did back number one have? " << endl;
        cin >> TD;
        TD = TD * 6;
        Back1.SetTD(TD);
        cout << "Back number one scored " << Back1.GetTD() << " point(s) in touchdowns today\n";
        cout << "-------------------------------------------------------------\n\n\n" << endl;
        cout << "How many fumbles did back number one have? " << endl;
        cin >> fumb;
        fumb = fumb * 2;
        Back1.SetFumb(fumb);
        cout << "Back number lost " << Back1.GetFumb() << " point(s) on fumbles today\n";
        cout << "-------------------------------------------------------------\n\n\n" << endl;
        total = rush + pass + TD - fumb;
        cout << "Total points for back number one is " << total << endl;
        cout << "-------------------------------------------------------------\n\n\n" << endl;
        cout << "Do you want to view back number two's points?\nTo continue press 1\n If not press any key to exit";
        cin >> x;
    
    for ( x == 1 ); //expected '(' before ';' token
      { // expected primary-expression before '{' token. //expected ')' before '{' token
        
        cout<< "How many yards did back number two have? " << endl;
        cin >> rush;
        rush = rush / 20;
        Back2.SetRush(rush);
        cout << "Back number one rushed for " << Back2.GetRush() << " point(s) rushing today\n";
        cout << "------------------------------------------------------------\n\n\n" << endl;
        cout << "How many yards receiving did back number two have? " << endl;
        cin >> pass;
        pass = pass / 20;
        Back2.SetPass(pass);
        cout << "Back number two caught for " << Back2.GetPass() << " point(s) receiving today\n";
        cout << "-------------------------------------------------------------\n\n\n" << endl;
        cout << "How many touchdowns did back number two have? " << endl;
        cin >> TD;
        TD = TD * 6;
        Back2.SetTD(TD);
        cout << "Back number two scored " << Back2.GetTD() << " point(s) in touchdowns today\n";
        cout << "-------------------------------------------------------------\n\n\n" << endl;
        cout << "How many fumbles did back number two have? " << endl;
        cin >> fumb;
        fumb = fumb * 2;
        Back2.SetFumb(fumb);
        cout << "Back number two lost " << Back2.GetFumb() << " point(s) on fumbles today\n";
        cout << "-------------------------------------------------------------\n\n\n" << endl;
        total = rush + pass + TD - fumb;
        cout << "Total points for back number two is " << total << endl;
        cin >> x;
    }
        
             return 0;
          }
    I looked up inheritance and derivation and will change "total" to GetTotal when I understand it all. If you have any other easier ways to ask for the stats of running back number two rather than my crude way of doing it please tell.

    Also this problem where I'm not sure if I should make Back1's total a pointer or how to add the back1 with back2's total.

    Code:
    #include <iostream>
    
    using namespace std;
    
    class RunningBack // Class RunningBack Defined
    {
          // Setting up "Rush"
          public:  
            int GetRush() const { return itsRush; }
            void SetRush(int rush) { itsRush = rush; }
            
            int GetPass() const { return itsPass; }
            void SetPass(int pass) { itsPass = pass; }
            
            int GetTD() const { return itsTD; };
            void SetTD(int TD) { itsTD = TD; }
            
            int GetFumb() const { return itsFumb; }
            void SetFumb(int fumb) { itsFumb = fumb; }
            
            int GetTotal() const { return itsTotal; }
            void SetTotal(int total) { itsTotal = total; }
          
          private:
            int itsRush;
            int itsPass;
            int itsTD;
            int itsFumb;
            int itsTotal;
            
    };
    
    int main()
    {
        
        RunningBack Back1;
        RunningBack Back2;
        int rush;
        int x;
        int pass;
        int TD;
        int total;
        int fumb;
        int totalPoints;
        int * ptotal;
        cout<< "How many yards did back number one have? " << endl;
        cin >> rush;
        rush = rush / 20;
        Back1.SetRush(rush);
        cout << "Back number one rushed for " << Back1.GetRush() << " point(s) rushing today\n";
        cout << "------------------------------------------------------------\n\n\n" << endl;
        cout << "How many yards receiving did back number one have? " << endl;
        cin >> pass;
        pass = pass / 20;
        Back1.SetPass(pass);
        cout << "Back number one caught for " << Back1.GetPass() << " point(s) receiving today\n";
        cout << "-------------------------------------------------------------\n\n\n" << endl;
        cout << "How many touchdowns did back number one have? " << endl;
        cin >> TD;
        TD = TD * 6;
        Back1.SetTD(TD);
        cout << "Back number one scored " << Back1.GetTD() << " point(s) in touchdowns today\n";
        cout << "-------------------------------------------------------------\n\n\n" << endl;
        cout << "How many fumbles did back number one have? " << endl;
        cin >> fumb;
        fumb = fumb * 2;
        Back1.SetFumb(fumb);
        cout << "Back number lost " << Back1.GetFumb() << " point(s) on fumbles today\n";
        cout << "-------------------------------------------------------------\n\n\n" << endl;
        total = rush + pass + TD - fumb;
        Back1.SetTotal(total);
        total = *ptotal;
        cout << "Total points for back number one is " << Back1.GetTotal()<< endl;
        cout << "-------------------------------------------------------------\n\n\n" << endl;
        
        
        cout<< "How many yards did back number two have? " << endl;
        cin >> rush;
        rush = rush / 20;
        Back2.SetRush(rush);
        cout << "Back number one rushed for " << Back2.GetRush() << " point(s) rushing today\n";
        cout << "------------------------------------------------------------\n\n\n" << endl;
        cout << "How many yards receiving did back number two have? " << endl;
        cin >> pass;
        pass = pass / 20;
        Back2.SetPass(pass);
        cout << "Back number two caught for " << Back2.GetPass() << " point(s) receiving today\n";
        cout << "-------------------------------------------------------------\n\n\n" << endl;
        cout << "How many touchdowns did back number two have? " << endl;
        cin >> TD;
        TD = TD * 6;
        Back2.SetTD(TD);
        cout << "Back number two scored " << Back2.GetTD() << " point(s) in touchdowns today\n";
        cout << "-------------------------------------------------------------\n\n\n" << endl;
        cout << "How many fumbles did back number two have? " << endl;
        cin >> fumb;
        fumb = fumb * 2;
        Back2.SetFumb(fumb);
        cout << "Back number two lost " << Back2.GetFumb() << " point(s) on fumbles today\n";
        cout << "-------------------------------------------------------------\n\n\n" << endl;
        total = rush + pass + TD - fumb;
        Back2.SetTotal(total);
        cout << "Total points for back number two is " << Back2.GetTotal() << endl;
        *ptotal + total = totalPoints; //non-lvalue in assignment
        cout << "Total Points for both backs: " << totalPoints << endl;
        
             return 0;
          }
    Last edited by Syllable; 09-27-2006 at 05:34 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. GUI Calculator - Critique
    By The Brain in forum Windows Programming
    Replies: 1
    Last Post: 02-25-2006, 03:39 AM
  2. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  3. Need help with calculator program
    By Kate in forum C# Programming
    Replies: 1
    Last Post: 01-16-2004, 09:48 AM
  4. Replies: 2
    Last Post: 05-10-2002, 04:16 PM
  5. c++ Reverse Polish Calculator Help
    By knight101 in forum C++ Programming
    Replies: 5
    Last Post: 11-12-2001, 08:31 AM

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