| | |
SSales Tax Calculator Class
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Sep 2009
Posts: 5
Reputation:
Solved Threads: 0
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*/
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.
•
•
Join Date: Jan 2008
Posts: 3,844
Reputation:
Solved Threads: 503
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]
Code tags are like this:
[code=C++]
// paste code here
[/code]
•
•
Join Date: Sep 2009
Posts: 5
Reputation:
Solved Threads: 0
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...
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)
// 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 1:17 pm. Reason: Reply to Vernon Dozier
•
•
Join Date: Jan 2008
Posts: 3,844
Reputation:
Solved Threads: 503
•
•
•
•
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)
#include <iostream> using namespace std; class myClass { public: myClass () { } myClass enter () { return *this; } myClass push () { return *this; } private: int someAttribute; }; int main() { myClass a; a.enter ().push (); cin.get (); return 0; }
•
•
Join Date: Sep 2009
Posts: 5
Reputation:
Solved Threads: 0
I corrected the missing "
Thank you...
But still cant get an answer
[/QUOTE]
Thank you...
But still cant get an answer

C++ Syntax (Toggle Plain Text)
// 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 1:33 pm.
•
•
Join Date: Jul 2005
Posts: 1,761
Reputation:
Solved Threads: 283
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.
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
•
•
Join Date: Sep 2009
Posts: 5
Reputation:
Solved Threads: 0
•
•
•
•
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.
![]() |
Similar Threads
- Read This Before Posting (C++)
- GUI Jslider gummin up my calculator... (Java)
- Need help with code for calculator (VB.NET)
- GPA calculator c++ (C++)
- class within a class (Java)
- Fraction Calculator (Java)
- Creating a GUI that accepts user input help (Java)
Other Threads in the C++ Forum
- Previous Thread: Memory allocation error that doesn't make sence!?
- Next Thread: Problem opening file using system() on Windows.
Views: 430 | Replies: 10
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll dynamiccharacterarray email encryption error file format forms fstream function functions game givemetehcodez graph homeworkhelp iamthwee ifstream input int java lib lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort sorting spoonfeeding string strings struct studio temperature template templates text tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






