SSales Tax Calculator Class

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2009
Posts: 5
Reputation: serdengil is an unknown quantity at this point 
Solved Threads: 0
serdengil serdengil is offline Offline
Newbie Poster

SSales Tax Calculator Class

 
0
  #1
Sep 15th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,844
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: SSales Tax Calculator Class

 
0
  #2
Sep 15th, 2009
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]
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,761
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 283
Lerner Lerner is offline Offline
Posting Virtuoso

Re: SSales Tax Calculator Class

 
0
  #3
Sep 15th, 2009
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.
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 5
Reputation: serdengil is an unknown quantity at this point 
Solved Threads: 0
serdengil serdengil is offline Offline
Newbie Poster

Re: SSales Tax Calculator Class

 
0
  #4
Sep 15th, 2009
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...



  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
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: SSales Tax Calculator Class

 
0
  #5
Sep 15th, 2009
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 "
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,844
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: SSales Tax Calculator Class

 
0
  #6
Sep 15th, 2009
Originally Posted by Lerner View Post
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.

  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. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 5
Reputation: serdengil is an unknown quantity at this point 
Solved Threads: 0
serdengil serdengil is offline Offline
Newbie Poster

Re: SSales Tax Calculator Class

 
0
  #7
Sep 15th, 2009
I corrected the missing "

Thank you...

But still cant get an answer



  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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,761
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 283
Lerner Lerner is offline Offline
Posting Virtuoso

Re: SSales Tax Calculator Class

 
0
  #8
Sep 15th, 2009
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.
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 5
Reputation: serdengil is an unknown quantity at this point 
Solved Threads: 0
serdengil serdengil is offline Offline
Newbie Poster

Re: SSales Tax Calculator Class

 
0
  #9
Sep 15th, 2009
Originally Posted by Lerner View Post
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
Untitled.jpg  
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: SSales Tax Calculator Class

 
0
  #10
Sep 16th, 2009
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 430 | Replies: 10
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC