Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Weird C++ program!!!  (Read 6875 times)

0 Members and 1 Guest are viewing this topic.

timtim41

    Topic Starter


    Beginner
    • Yes
  • Computer: Specs
  • Experience: Experienced
  • OS: Linux variant
Weird C++ program!!!
« 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:
T Jewell

timtim41

    Topic Starter


    Beginner
    • Yes
  • Computer: Specs
  • Experience: Experienced
  • OS: Linux variant
Re: Weird C++ program!!!
« Reply #1 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;
}
T Jewell

Geek-9pm


    Mastermind
  • Geek After Dark
  • Thanked: 1026
    • Gekk9pm bnlog
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: Weird C++ program!!!
« Reply #2 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?

timtim41

    Topic Starter


    Beginner
    • Yes
  • Computer: Specs
  • Experience: Experienced
  • OS: Linux variant
Re: Weird C++ program!!!
« Reply #3 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.
T Jewell

Treval



    Hopeful

    Thanked: 14
    Re: Weird C++ program!!!
    « Reply #4 on: February 11, 2010, 03:49:56 PM »
    Is the output an error?
    I found this:

    256 error?

    What is 'shapeformat'?

    TheUnixGuy



      Beginner

      Thanked: 7
      Re: Weird C++ program!!!
      « Reply #5 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;
      « Last Edit: February 12, 2010, 04:02:02 AM by TheUnixGuy »
      When it rains, most birds head for shelter; Eagle is the only bird that flies above the clouds to avoid rain.

      TheUnixGuy



        Beginner

        Thanked: 7
        Re: Weird C++ program!!!
        « Reply #6 on: February 12, 2010, 04:04:26 AM »
        Geek-9pm,

        Maybe you missed this one on Line No. 8:

        int area() {return (x*y);}
        When it rains, most birds head for shelter; Eagle is the only bird that flies above the clouds to avoid rain.

        timtim41

          Topic Starter


          Beginner
          • Yes
        • Computer: Specs
        • Experience: Experienced
        • OS: Linux variant
        Re: Weird C++ program!!!
        « Reply #7 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.
        T Jewell

        TheUnixGuy



          Beginner

          Thanked: 7
          Re: Weird C++ program!!!
          « Reply #8 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`.
          When it rains, most birds head for shelter; Eagle is the only bird that flies above the clouds to avoid rain.

          BC_Programmer


            Mastermind
          • Typing is no substitute for thinking.
          • Thanked: 1140
            • Yes
            • Yes
            • BC-Programming.com
          • Certifications: List
          • Computer: Specs
          • Experience: Beginner
          • OS: Windows 11
          Re: Weird C++ program!!!
          « Reply #9 on: February 12, 2010, 11:02:46 PM »
          TheUnixGuy's  revised program works perfectly fine for me as well with Visual Studio 2008.

          I was trying to dereference Null Pointers before it was cool.

          EEVIAC

          • Guest
          Re: Weird C++ program!!!
          « Reply #10 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..

          TheUnixGuy



            Beginner

            Thanked: 7
            Re: Weird C++ program!!!
            « Reply #11 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.
            When it rains, most birds head for shelter; Eagle is the only bird that flies above the clouds to avoid rain.

            EEVIAC

            • Guest
            Re: Weird C++ program!!!
            « Reply #12 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"

            Treval



              Hopeful

              Thanked: 14
              Re: Weird C++ program!!!
              « Reply #13 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...
              « Last Edit: February 21, 2010, 07:31:49 AM by Treval »

              BC_Programmer


                Mastermind
              • Typing is no substitute for thinking.
              • Thanked: 1140
                • Yes
                • Yes
                • BC-Programming.com
              • Certifications: List
              • Computer: Specs
              • Experience: Beginner
              • OS: Windows 11
              Re: Weird C++ program!!!
              « Reply #14 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.
              I was trying to dereference Null Pointers before it was cool.