943,865 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 672
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Sep 15th, 2009
0

SSales Tax Calculator Class

Expand Post »
Hey guys,

I do have a main function and need to write the class. I just need to where should I start. I am a new in C++ but the prof. asked us to write the class code of this calculator. He already gave the main function and the purpose of the class is an
HP calculator has a screen and a push down stack that can hold up to 20 entries. The calculations of multiplication addition subtraction and division are all performed. if more then 20 entries are pushed, it will crash.

[code.c++]

int main(){
StCalc a;
a.enter(2); //screen has 2
a.push() .enter(3); //2 on stack screen has 3
a.push() .add() .pop(); // 2 3 on stack, add, pop to screen which has 5
cout<<"Expect StCalc (5): "<<a<<endl; // test "<<"
cout<<"Expect 5: "<<a.getScreen()<<endl; //test getScreen()
a.push() .enter(4); //5 on stack 4 on screen
cout<<"Expect StCalc (20): "<<a.push() .mul() .pop<<endl;
a.push() .enter(8); //stack has 20 screen has 8
cout<<"Expect StCalc(2.5): '<<a.push() .sub() .pop() <<endl;
a.push() .enter(3); //2.5 on stack, 3 on screen
cout<<"Expect StCalc(-0.5): <<a.push() .sub() .pop()<<endl;
a<<42; //test overload "<<", which stores 42 on screen
cout<< "expect 10: ""<<(a==42)<<(==3)<<endl; //test boolen ==
cout<<"Expect StCalc (42): "<<a<<endl;
a.enter(3);
a+=2; //test overloaded +=
cout<<"Expect StCalc(5): "<<a<<endl;
a.push() .enter(0);
//a.push() div(); //should crash
a.push() .push() .push() .push() .push() .push() .push();
a.push() .push() .push() .push() .push() .push() .push();
a.push() .push() .push() .push() .push() .push() .push();
}

void check(bool b, char *mess, int line, char *filename) {
if(!b){
cerr<<"ERROR(line "<<line<<" in file '""<<filename<<"'): "<<mess<<endl;
exit(1);
}
}

/* the OUTPUT

Expect StCalc (5): StCalc (5)
Expect 5: 5
Expect StCalc (20)tCalc(20)
Expect StCalc (2.5): StCalc(2.5)
Expect StCalc (-0.5): StCalc (-0.5)
Expect 10: 10
Expect StCalc (42): StCalc(42)
Expect StCalc (5): StCalc (5)
ERROR (line 86 in file 'fullp0.cc') : (Stcalc ::push()) stack overflow*/
Last edited by serdengil; Sep 15th, 2009 at 11:38 am.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
serdengil is offline Offline
5 posts
since Sep 2009
Sep 15th, 2009
0

Re: SSales Tax Calculator Class

Start by making sure you understand what exactly you are supposed to do. Start by making sure you understand what all of these functions are supposed to do and why he has main the way he has it. Start by spending a considerable amount of time doing all of these problems with pencil and paper till you know what everything represents. Then write some functions and test them with a simpler main to make sure it all works appropriately. When it does, go back to his main and try it.

Code tags are like this:


[code=C++]
// paste code here
[/code]
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008
Sep 15th, 2009
0

Re: SSales Tax Calculator Class

I've never seen sequential dot operators used like this:

myClass a;
a.enter().push();

but I like learning new things, so if someone can confirm line two above as valid syntax assuming enter() and push() are valid methods for a myClass object, I'll be wiser than I am now.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Sep 15th, 2009
0

Re: SSales Tax Calculator Class

Thank you Vernon Dozier,

I just understand that,

There should be a class named StCalc and it also has enter, push, mul, div, sub, pop functions. The enter function help me to keep the new number and push is kind a storing this number . And all the other functions like math functions mul is multiplying new number and keepin number.

right?

Am I on the righ way...



C++ Syntax (Toggle Plain Text)
  1. //
  2. int main(){
  3. StCalc a;
  4. a.enter(2); //screen has 2
  5. a.push() .enter(3); //2 on stack screen has 3
  6. a.push() .add() .pop(); // 2 3 on stack, add, pop to screen which has 5
  7. cout<<"Expect StCalc (5): "<<a<<endl; // test "<<"
  8. cout<<"Expect 5: "<<a.getScreen()<<endl; //test getScreen()
  9. a.push() .enter(4); //5 on stack 4 on screen
  10. cout<<"Expect StCalc (20): "<<a.push() .mul() .pop<<endl;
  11. a.push() .enter(8); //stack has 20 screen has 8
  12. cout<<"Expect StCalc(2.5): '<<a.push() .sub() .pop() <<endl;
  13. a.push() .enter(3); //2.5 on stack, 3 on screen
  14. cout<<"Expect StCalc(-0.5): <<a.push() .sub() .pop()<<endl;
  15. a<<42; //test overload "<<", which stores 42 on screen
  16. cout<< "expect 10: ""<<(a==42)<<(==3)<<endl; //test boolen ==
  17. cout<<"Expect StCalc (42): "<<a<<endl;
  18. a.enter(3);
  19. a+=2; //test overloaded +=
  20. cout<<"Expect StCalc(5): "<<a<<endl;
  21. a.push() .enter(0);
  22. //a.push() div(); //should crash
  23. a.push() .push() .push() .push() .push() .push() .push();
  24. a.push() .push() .push() .push() .push() .push() .push();
  25. a.push() .push() .push() .push() .push() .push() .push();
  26. }
  27.  
  28. void check(bool b, char *mess, int line, char *filename) {
  29. if(!b){
  30. cerr<<"ERROR(line "<<line<<" in file '""<<filename<<"'): "<<mess<<endl;
  31. exit(1);
  32. }
  33. }
  34.  
  35. /* the OUTPUT
  36.  
  37. Expect StCalc (5): StCalc (5)
  38. Expect 5: 5
  39. Expect StCalc (20) tCalc(20)
  40. Expect StCalc (2.5): StCalc(2.5)
  41. Expect StCalc (-0.5): StCalc (-0.5)
  42. Expect 10: 10
  43. Expect StCalc (42): StCalc(42)
  44. Expect StCalc (5): StCalc (5)
  45. ERROR (line 86 in file 'fullp0.cc') : (Stcalc ::push()) stack overflow*/
  46.  
Last edited by serdengil; Sep 15th, 2009 at 1:17 pm. Reason: Reply to Vernon Dozier
Reputation Points: 10
Solved Threads: 0
Newbie Poster
serdengil is offline Offline
5 posts
since Sep 2009
Sep 15th, 2009
0

Re: SSales Tax Calculator Class

Look at all the pretty colours in your post.
Blue means it's a string - is it really meant to be a string?

Consider looking for the mis-placed "
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Sep 15th, 2009
0

Re: SSales Tax Calculator Class

Click to Expand / Collapse  Quote originally posted by Lerner ...
I've never seen sequential dot operators used like this:

myClass a;
a.enter().push();

but I like learning new things, so if someone can confirm line two above as valid syntax assuming enter() and push() are valid methods for a myClass object, I'll be wiser than I am now.

The syntax is fine, depending on what everything returns. If the methods return myClass objects, you're good to go. Here's a valid, though completely meaningless implementation.

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class myClass
  5. {
  6. public:
  7. myClass ()
  8. {
  9. }
  10.  
  11. myClass enter ()
  12. {
  13. return *this;
  14. }
  15.  
  16. myClass push ()
  17. {
  18. return *this;
  19. }
  20.  
  21. private:
  22. int someAttribute;
  23. };
  24.  
  25.  
  26.  
  27. int main()
  28. {
  29. myClass a;
  30. a.enter ().push ();
  31. cin.get ();
  32. return 0;
  33. }
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008
Sep 15th, 2009
0

Re: SSales Tax Calculator Class

I corrected the missing "

Thank you...

But still cant get an answer



C++ Syntax (Toggle Plain Text)
  1. //
  2. int main(){
  3. StCalc a;
  4. a.enter(2); //screen has 2
  5. a.push() .enter(3); //2 on stack screen has 3
  6. a.push() .add() .pop(); // 2 3 on stack, add, pop to screen which has 5
  7. cout<<"Expect StCalc (5): "<<a<<endl; // test "<<"
  8. cout<<"Expect 5: "<<a.getScreen()<<endl; //test getScreen()
  9. a.push() .enter(4); //5 on stack 4 on screen
  10. cout<<"Expect StCalc (20): "<<a.push() .mul() .pop<<endl;
  11. a.push() .enter(8); //stack has 20 screen has 8
  12. cout<<"Expect StCalc(2.5): "<<a.push() .sub() .pop() <<endl;
  13. a.push() .enter(3); //2.5 on stack, 3 on screen
  14. cout<<"Expect StCalc(-0.5): "<<a.push() .sub() .pop()<<endl;
  15. a<<42; //test overload "<<", which stores 42 on screen
  16. cout<< "expect 10: "<<(a==42)<<(==3)<<endl; //test boolen ==
  17. cout<<"Expect StCalc (42): "<<a<<endl;
  18. a.enter(3);
  19. a+=2; //test overloaded +=
  20. cout<<"Expect StCalc(5): "<<a<<endl;
  21. a.push() .enter(0);
  22. //a.push() div(); //should crash
  23. a.push() .push() .push() .push() .push() .push() .push();
  24. a.push() .push() .push() .push() .push() .push() .push();
  25. a.push() .push() .push() .push() .push() .push() .push();
  26. }
  27.  
  28. void check(bool b, char *mess, int line, char *filename) {
  29. if(!b){
  30. cerr<<"ERROR(line "<<line<<" in file '"<<filename<<"'): "<<mess<<endl;
  31. exit(1);
  32. }
  33. }
  34.  
  35. /* the OUTPUT
  36.  
  37. Expect StCalc (5): StCalc (5)
  38. Expect 5: 5
  39. Expect StCalc (20) tCalc(20)
  40. Expect StCalc (2.5): StCalc(2.5)
  41. Expect StCalc (-0.5): StCalc (-0.5)
  42. Expect 10: 10
  43. Expect StCalc (42): StCalc(42)
  44. Expect StCalc (5): StCalc (5)
  45. ERROR (line 86 in file 'fullp0.cc') : (Stcalc ::push()) stack overflow*/
[/QUOTE]
Last edited by serdengil; Sep 15th, 2009 at 1:33 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
serdengil is offline Offline
5 posts
since Sep 2009
Sep 15th, 2009
0

Re: SSales Tax Calculator Class

VernonDozier, thanks. I knew that explained why the << and the + operator can be used sequentially, but I've never seen the dot operator used that way. I have used the dot operator sequentially to get infromation or do things for encapsulated class/struct members, but have never seen this use of it before.

serdengil, post the code you have written for the StCalc class, not the driver program. It looks to me like you will need several variable members in the class in addition to the methods necessary to be able to run the code. It looks like you will need at least one variable to store input, one variable to store an answer and one stack variable. Only you know if you can use code for a prewritten stack or whether you will need to write your own.

Start out by commenting all of the program in main() except the declaration of the StCalc variable and a line to display either or both of the member variables. Once you can do that then write code to do each method you need to create for the program one at a time. If you have to write code to be sure the method is working correctly before it's actually used in the program as written, good for you, just be sure you remove it after the function has been thouroughly tested.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Sep 15th, 2009
0

Re: SSales Tax Calculator Class

Click to Expand / Collapse  Quote originally posted by Lerner ...
VernonDozier, thanks. I knew that explained why the << and the + operator can be used sequentially, but I've never seen the dot operator used that way. I have used the dot operator sequentially to get infromation or do things for encapsulated class/struct members, but have never seen this use of it before.

serdengil, post the code you have written for the StCalc class, not the driver program. It looks to me like you will need several variable members in the class in addition to the methods necessary to be able to run the code. It looks like you will need at least one variable to store input, one variable to store an answer and one stack variable. Only you know if you can use code for a prewritten stack or whether you will need to write your own.

Start out by commenting all of the program in main() except the declaration of the StCalc variable and a line to display either or both of the member variables. Once you can do that then write code to do each method you need to create for the program one at a time. If you have to write code to be sure the method is working correctly before it's actually used in the program as written, good for you, just be sure you remove it after the function has been thouroughly tested.
I just wrote:
Attached Thumbnails
Click image for larger version

Name:	Untitled.jpg
Views:	15
Size:	57.6 KB
ID:	11633  
Reputation Points: 10
Solved Threads: 0
Newbie Poster
serdengil is offline Offline
5 posts
since Sep 2009
Sep 16th, 2009
0

Re: SSales Tax Calculator Class

Paste your CODE, not some fuzzy bitmap image.
How the hell are we supposed to quote text from that?

Oh, and the for loop in your ctor is broke.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Memory allocation error that doesn't make sence!?
Next Thread in C++ Forum Timeline: Problem opening file using system() on Windows.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC