Computer Hope

Software => Computer programming => Topic started by: timtim41 on February 11, 2010, 12:49:47 PM

Title: Weird C++ program!!!
Post by: timtim41 on February 11, 2010, 12:49:47 PM
Hey guys, I have been trying to learn how to do OOP by using classes in C++, and I made a program that compiles, but the output is "256" and i don't know why and I was just wondering what I did wrong. All comments are appreciated. Thanks :)

#include <iostream>
 
using namespace std;
class CRectangle
{
    int x, y;
        public:
Title: Re: Weird C++ program!!!
Post by: timtim41 on February 11, 2010, 12:54:49 PM
Sorry, the program is:

#include <iostream>

using namespace std;
class CRectangle
{
    int x, y;
        public:
            void values(int, int);
            int area() {return (x*y);}
void CRectangle::shapeformat() {
    x = 3;
    y = 4;
}
};

int main()
{
    CRectangle rect_shape;

    cout<<endl<<rect_shape.area();
    cout<<endl;

    return 0;
}
Title: Re: Weird C++ program!!!
Post by: Geek-9pm on February 11, 2010, 02:10:56 PM
I don't know c++
But I was wondering,
Is it normal to have..
{{{
and
}}}}

Why aren't they paired?
Title: Re: Weird C++ program!!!
Post by: timtim41 on February 11, 2010, 03:13:49 PM
{{{, }}}}? Where do you see that at, cuz I wouldn't consider that normal in C++ unless if you have parameters within parameters, you know what i mean. but i dont see that anywhere in the source code.
Title: Re: Weird C++ program!!!
Post by: Treval on February 11, 2010, 03:49:56 PM
Is the output an error?
I found this:

256 error? (http://www.cplusplus.com/forum/general/13692/)

What is 'shapeformat'?
Title: Re: Weird C++ program!!!
Post by: TheUnixGuy on February 12, 2010, 03:16:05 AM
Hello,

The C++ Compiler built into my brain says that it might be a case of uninitialized variables.
Code: [Select]
#include <iostream>

using namespace std;

class CRectangle
{
    int x, y;
        public:
            void values(int, int);
            int area() {return (x*y);}
void CRectangle::shapeformat() {
    x = 3;
    y = 4;
}
};

int main()
{
    CRectangle rect_shape;

    cout<<endl<<rect_shape.area();
    cout<<endl;

    return 0;
}

In Line No. 10, qualifier `CRectangle::` for the function `shapeformat` isn't necessary. Delete it.
Make a call to `shapeformat` from main().

Make it look something like:

Code: [Select]
#include <iostream>

using namespace std;

class CRectangle
{
            int x, y;

        public:
            void values(int, int);
            int area()
            {
                    return (x*y);
            }
           
            void shapeformat()
            {
                    x = 3;
                    y = 4;
            }
};

int main()
{
            CRectangle rect_shape;

            rect_shape.shapeformat();

            cout<<endl<<rect_shape.area();
            cout<<endl;

            return 0;
}

NOTE: I have modified the code and did some cleanup to avoid confusions. Try to use proper and uniform indentation, alignment and linespacing so that the code is more `readable`.

Secondly, you've called `endl` before and after call to the class member `area()`. The former causes an empty line which may not be desired. It could be something like:
Code: [Select]
cout <<rect_shape.area() << endl;
Title: Re: Weird C++ program!!!
Post by: TheUnixGuy on February 12, 2010, 04:04:26 AM
Geek-9pm,

Maybe you missed this one on Line No. 8:

int area() {return (x*y);}
Title: Re: Weird C++ program!!!
Post by: timtim41 on February 12, 2010, 11:48:14 AM
Ok, thanks for the help and everything, but it still prints out "256." What I wanted it to do is to print out the product of 3 and 4 which is '12'. I know other ways to do it, but I want to know why this code does what it does for educational purposes.
Title: Re: Weird C++ program!!!
Post by: TheUnixGuy on February 12, 2010, 10:35:43 PM
Hello,

prints out "256."

No chance! Did you call `shapeformat`from `main()`? What compiler are you using? Try a Clean Build. Here on my Linux with G++ (The GNU C++ Compiler):
Code: [Select]
$ g++ -o newfile newfile.cpp
$ ./newfile

12
$
I saved your code as `newfile.cpp`(after making the modifications shown in my Relply No. 5) and then compiled it to `newfile`.
Title: Re: Weird C++ program!!!
Post by: BC_Programmer on February 12, 2010, 11:02:46 PM
TheUnixGuy's  revised program works perfectly fine for me as well with Visual Studio 2008.

Title: Re: Weird C++ program!!!
Post by: EEVIAC on February 21, 2010, 12:11:58 AM
Hello,

The C++ Compiler built into my brain says that it might be a case of uninitialized variables.
Code: [Select]
#include <iostream>

using namespace std;

class CRectangle
{
    int x, y;
        public:
            void values(int, int);
            int area() {return (x*y);}
void CRectangle::shapeformat() {
    x = 3;
    y = 4;
}
};

int main()
{
    CRectangle rect_shape;

    cout<<endl<<rect_shape.area();
    cout<<endl;

    return 0;
}

In Line No. 10, qualifier `CRectangle::` for the function `shapeformat` isn't necessary. Delete it.
Make a call to `shapeformat` from main().

Make it look something like:

Code: [Select]
#include <iostream>

using namespace std;

class CRectangle
{
            int x, y;

        public:
            void values(int, int);
            int area()
            {
                    return (x*y);
            }
           
            void shapeformat()
            {
                    x = 3;
                    y = 4;
            }
};

int main()
{
            CRectangle rect_shape;

            rect_shape.shapeformat();

            cout<<endl<<rect_shape.area();
            cout<<endl;

            return 0;
}

NOTE: I have modified the code and did some cleanup to avoid confusions. Try to use proper and uniform indentation, alignment and linespacing so that the code is more `readable`.

Secondly, you've called `endl` before and after call to the class member `area()`. The former causes an empty line which may not be desired. It could be something like:
Code: [Select]
cout <<rect_shape.area() << endl;

If you don't mind me asking..  How do you run this code?  I've got MS Visual C++ 2008 Express ED.

I've only practiced running simple Win32 Console programs..
Title: Re: Weird C++ program!!!
Post by: TheUnixGuy on February 21, 2010, 04:28:41 AM
Hello there,

I've never used that IDE but try copying that into the textbox where you type Win32 programs and try compiling it. It should work. BC_Programmer uses that IDE though, maybe he can help.
Title: Re: Weird C++ program!!!
Post by: EEVIAC on February 21, 2010, 05:53:17 AM
ahh, I see..

If I use "start without debugging" it displays "12...press any key to continue"
Title: Re: Weird C++ program!!!
Post by: Treval on February 21, 2010, 06:43:03 AM
lollll.. I always use debugging and I have that IDE.
I will have a look at it.
Usually main() will tell VS compiler to run it.

Edit: EEVIAC what the lol?
It outputs 12 for me too. =)
Isn't that normal?..
Since 3*4 is 12.. lol...
Title: Re: Weird C++ program!!!
Post by: BC_Programmer on February 21, 2010, 08:47:55 AM
I just replaced the entire "stub" program it created with TheUnixGuy's code. ran fine- but the command window dissapeared instantly, so I cheated and added a call to gets() at the end so I could see the result. There's an option somewhere in the project or debugging options to keep it open- at least, I think there is.