Program with a class ends before user can see output

Discussion in 'C++' started by Lief Webster, Nov 6, 2007.

  1. Lief Webster

    Lief Webster New Member

    Joined:
    Oct 16, 2007
    Messages:
    26
    Likes Received:
    0
    Trophy Points:
    0
    Well, like the title says, I have constructed a program, my first one using classes, and my problem is that the program ends right after the user enters two values (the side lengths of a rectangle) and doesn't display the final output: the area of the rectangle.

    It's meant to take in two values from the user (the sides of a rectangle), declared in main() by i and j. It then sends those values into a function in the class CRectangle (the function is get_values( int , int ) ) which subsequently assigns them to values within the class, x and y. Skipping back to main(), the function area() is declared, which multiplies x by y and returns the value to cout.

    Here's the code:

    Code:
    //Rectangle class
    
    #include <iostream>
    
    using namespace std;
    
    class CRectangle 
    {
        private:
        int x , y;
        
        public:
        void get_values( int , int );
        int area() { return ( x * y ); }
    };
    
    void CRectangle::get_values( int a , int b )
    {
        x = a;
        y = b;
    }
    
    int main()
    {
        int i , j;
        
        CRectangle rectangle;
        
        cout << "Enter the lengths of the two sides of a rectangle: ";
        cin >> i , j;
        
        rectangle.get_values( i , j );
    
        cout << "The rectangle's area is " << rectangle.area();
        
        cin.get();
        return 0;
    }
    I just need to get it to finish the program; it always ends too early. Thanks!
     
  2. dharmaraj.guru

    dharmaraj.guru New Member

    Joined:
    Oct 23, 2007
    Messages:
    16
    Likes Received:
    0
    Trophy Points:
    0
    The problem is because of the cin statements.
    Code:
    cin >> i , j; 
    
    is not the correct way of scaning two integers. Instead it fetches i only even though you have passed more of them. The rest are all stored in input buffer and will be used as values when the program expects any in future.
    Code:
    cin.get();
    
    As mentioned above, It reads the character from the input buffer, not from the keyboard though you have entered.
    Also, when you passed i & j, if you have pressed 'Enter' to terminate inputting the values, that 'Enter' is even be taken as input for cin.get(). Thats why you are not able to see the output. It is good to clear the input buffer before doing cin::get() through cin::ignore().

    Hence, the corrections are..
    Code:
    cin >> i >> j;
    cin.ignore(); // Add before calling cin.get();
    
    ||| Dharma |||
     
  3. Lief Webster

    Lief Webster New Member

    Joined:
    Oct 16, 2007
    Messages:
    26
    Likes Received:
    0
    Trophy Points:
    0
    Thank you so much! :eek: I'm glad I found out about that before I started messing with some other random things.
     
  4. dharmaraj.guru

    dharmaraj.guru New Member

    Joined:
    Oct 23, 2007
    Messages:
    16
    Likes Received:
    0
    Trophy Points:
    0

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice