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):StCalc(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*/

Recommended Answers

All 10 Replies

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]

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.

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...

//
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*/

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 "

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.

#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;     
}

I corrected the missing "

Thank you...

But still cant get an answer:)

//
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*/

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.

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:

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.

You can either embed a stack within the StCalc or you can write code to create a stack directly. Given you use of an array within the code, then I assume you will be writing code directly.

Here's how I would start writing code. I used public member variables to make the functionality easier. I would use private member variables to make the code more stable.

struct myStack
{
    myStack() {};//write code to initialize member variables
    int sizeOfStack;
    double input;
    double array[20];
    double answer;
    void enter(double i) {}; //write code to put i into input;
};
int main()
{
    StCalc a;
    a.enter(3);

    /*the following 3 lines are debugging code to assure the constructor and enter() worked as expected.  I will be removed when functionality assured.*/
    cout << a.input << endl;
    cout << a.answer << endl;
    cout << a.sizeOfStack << endl;

    cin.get();
    return 0;
}

Only after I got that functionality working would I try to do anything directly with the arrray, like limiting availability of elements to make it functional stack.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.