Thread: Some help for a newbie!!!

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    2

    Some help for a newbie!!!

    I am new to this site and new to the C++ language, so pls bare with me. I am sure there is a very simple solution to this problem but it has got me stumped!!! I had to create a Counter class that has four counters. Think of it like this: [][][][]...it's not an array, each one is a separate variable that keeps a running total that is controlled by it's own function. Each one separately represents ones, tens, hundreds, and thousands(from right to left obviously) so when they reach 10 the 1 is carried over to the next one. Now, I have everything working except for the carry over.
    Code:
    #include <iostream.h>
    
    class Counter
    {
    	private:
    				int total1, total2, total3, total4, grandTotal;
    				int max;
    				int price1, price2, price3, price4;
    	public:
    				Counter();
    				void reset() { total1=0, total2=0, total3=0, total4=0, grandTotal; }
    				void incrl1();
    				void incrl10();
    				void incrl100();
    				void incrl1000();
    				void overflow() { cout << "You have exceeded your limit!"; }
    				void display();
    };
    
    Counter::Counter()
    {
    	max=9999, total1=0, total2=0, total3=0, total4=0, grandTotal=0;
    	price1=0, price2=0, price3=0, price4=0;
    }
    
    void Counter::incrl1()
    {
    	cout << "Enter num: ";
    	cin >> price1;
    	grandTotal += price1;
    	if(grandTotal>max)
    		overflow();
    	total1 += price1;
    	if(total1>9)
    	{
    		total1=total1-10;
    		total2++;
    	}
    }
    
    void Counter::incrl10()
    {
    	cout << "Enter num: ";
    	cin >> price2;
    	grandTotal += price2*10;
    	if(grandTotal>max)
    		overflow();
    	total2 += price2;
    	if(total2>9)
    	{
    		total2=total2-10;
    		total3++;
    	}
    }
    I have obviously left out the other functions, but this should give you an idea.
    as you can see if the total becomes>9 I am incrementing the next total, but this only works when it is less than 9 because otherwise it becomes 10 and that's not what I want and I can't figure out how else to do it. The only thing I can think of to solve the problem would be to call a function that has a long rediculous nested 'if' statement, but I feel there has to be another way!! Any suggestions would be much appreciated...thnks

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2
    thnks alot for your help Salem...I figured I needed to pass it out somehow but I just couldn't figure where to pass it to! Thnks for clearing it up for me

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie with Very Newbie Question
    By Jedi_Mediator in forum C++ Programming
    Replies: 18
    Last Post: 07-01-2008, 08:00 AM
  2. Some help for a newbie?
    By Ilmater in forum C++ Programming
    Replies: 23
    Last Post: 04-19-2004, 07:44 PM
  3. Resource ICONs
    By gbaker in forum Windows Programming
    Replies: 4
    Last Post: 12-15-2003, 07:18 AM
  4. C++ newbie / linux not so newbie question
    By goldmonkey in forum C++ Programming
    Replies: 7
    Last Post: 12-13-2003, 12:27 PM
  5. Newbie Game Develpoers Unite!
    By Telenosis in forum Game Programming
    Replies: 10
    Last Post: 06-22-2002, 02:02 PM