variable value not being set

This is a discussion on variable value not being set within the C++ Programming forums, part of the General Programming Boards category; according to the output expected my program works fine except on the last line where the limit is supposed to ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    134

    variable value not being set

    according to the output expected my program works fine except on the last line where the limit is supposed to be 3. My output shows 0, where am I going wrong???
    The output is here http://www.cs.uh.edu/~svenkat/lib/as...ASSIGN1/output

    the main.cpp for the .h and its accompanying .cpp is here http://www.cs.uh.edu/~svenkat/lib/as...N1/assign1.cpp .

    The .h file
    Code:
    class ModuloCounter
    {
    public:
    	ModuloCounter(int );
    	ModuloCounter(const ModuloCounter& theCounter);
    	~ModuloCounter();
    	void increment (void);
    	void decrement (void);
    	static int getNumberofModuloCounters(void);
    	int getCounterLimit(int)const;
    	int getCounterValue(void)const;
    
    private:
    	int modLim;
    	int currVal;
    	static int modCount;
    };
    .cpp file
    Code:
    #include "ModuloCounter.h"
    
    int ModuloCounter::modCount=0;
    
    
    
    ModuloCounter::ModuloCounter(int set)
    {
    	
    	modLim=set;
    	currVal=1;
    	modCount++;
    
    }
    
    ModuloCounter::~ModuloCounter()
    {
    	modCount--;
    }
    
    void ModuloCounter::increment(void)
    {
    	if(currVal==modLim)currVal=1;
    	else currVal++;
    }
    
    void ModuloCounter::decrement(void)
    {
    	if(currVal==1)currVal=modLim;
    	else currVal--;
    }
    
    int ModuloCounter:: getNumberofModuloCounters(void)
    {
    	
    	return modCount;
    }
    
    int ModuloCounter:: getCounterLimit(int limit)const
    {
    	limit=modLim;
    	return limit;
    }
    
    int ModuloCounter:: getCounterValue(void)const
    {
    	return currVal;
    }
    
    ModuloCounter::ModuloCounter(const ModuloCounter& theCounter)
    {
    	currVal=theCounter.currVal;
    	modCount++;
    	modLim=theCounter.modLim;
    
    }

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    Code:
    int ModuloCounter:: getCounterLimit(int limit)const
    {
    	limit=modLim;
    	return limit;
    }

    See the problem?

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    11
    See the problem?
    Clearly he doesn't or he wouldn't be asking. The problem is that limit in this context is a copy of the limit that was passed to the function. It needs to be a reference for any changes to take effect.
    Code:
    int ModuloCounter:: getCounterLimit(int& limit)const
    {
    	limit=modLim;
    	return limit;
    }

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    134
    aah thanks alot guys!!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pong is completed!!!
    By Shamino in forum Game Programming
    Replies: 11
    Last Post: 05-26-2005, 11:50 AM
  2. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 01:34 PM
  3. Set Classes
    By Nicknameguy in forum C++ Programming
    Replies: 13
    Last Post: 10-31-2002, 02:56 PM
  4. Set Classes
    By Nicknameguy in forum C++ Programming
    Replies: 3
    Last Post: 10-21-2002, 08:40 PM
  5. constructor to set variable to an empry string
    By wireless in forum C++ Programming
    Replies: 1
    Last Post: 03-29-2002, 11:29 AM

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