Passing Int to be Displayed Properly

Class Shape {

Area = x[1]+x[2];

cout <<"Area is" <<Area;

};

This results returns a value 24.


But when I try to use it in the main method

int main
{

Shape ShapeTwo
cout <<"The name is " <<ShapeTwo.Area;

Result is now 6.95223e-08.

How can I get it to show 24 instead of 6.95223e-08.
Last edited on
You haven't shown the definition for Area.

The following code is bogus:
1
2
3
4
Class Shape {

Area = x[1]+x[2];
cout <<"Hello, what is your name" <<Area<<endl;

You can't have statements outside a function.

What do you mean the "display the int as a proper output"?

Also, the following statement is not valid:
 
Shape.showAre();

You can't call a function by referencing the class name. You must reference an instance of the class.

PLEASE USE CODE TAGS (the <> formattion button) when posting code. It makes it easier to help you.
Class Shape {

Area = x[1]+x[2];

cout <<"Area is" <<Area;

};

This results returns a value 24.


But when I try to use it in the main method

int main
{

Shape ShapeTwo
cout <<"The name is " <<ShapeTwo.Area;

Result is now 6.95223e-08.

How can I get it to show 24 instead of 6.95223e-08.
help anyone
If that code snippet is represenative of the class you sent me via PM, the problem is that Area is uninitialized.

Shape ShapeTwo; invoke's Shape's default constructor. Shape's default constructor in turn invokes an explicit 4 argument constructor which sets x and y, but does not calculate Area. Therefore, when you try and output Area, Area is an uninitialized variable.

Posting rules here are that you need to post enough of your code to demonstrate the problem. The tiny snippets you posted are not sufficient for anybody not seeing your actual code to ascertain the problem.





I edit
Last edited on
The only place I see you calculating Area is inside InputData and then only if name == "Square". Any other logic path, and Area is going to be uninitialized.
There is a calculation inside ComputeArea, but I don't see you call that function.

You also have some logic problems in main.
If option 1 is entered you call inputData, and then call main recursively. You should never call main. Calling main recursively is going to push a new instance of sd and ShapeTwoDD onto the stack. Now, if you select option 2, ShapeTwoDD is uninitialized except for what is set by its default constructor.


Thanks Abstraction.

The problem was you should never call main().

I made the changes and included a do operation for that menu.


Thank you once again Abstraction.

Is there any place I can view codes written by others to learn from them ?

Last edited on

Is there any place I can view codes written by others to learn from them ?

Read the posts on this site. It's a great way to learn.
Topic archived. No new replies allowed.