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.
I have obviously left out the other functions, but this should give you an idea.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++; } }
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



LinkBack URL
About LinkBacks


