user defined class with overloaded insertion and stream extraction ?

This is a discussion on user defined class with overloaded insertion and stream extraction ? within the C++ Programming forums, part of the General Programming Boards category; This code is using string. How do I change the data type to integer and obtain the same type of ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    90

    user defined class with overloaded insertion and stream extraction ?

    This code is using string. How do I change the data type to integer and obtain the same type of results or near. If shown to me I'm sure I can complete the next three required steps.

    Thanks in advance

    #include <iostream>
    #include <iomanip>
    #include <string>
    using namespace std;

    class POINT
    {
    friend ostream &operator<<( ostream &, const POINT & );
    friend istream &operator>>( istream &, POINT & );
    private:
    string xCoordinate; // 3-digit
    string yCoordinate; }; // 4-digit




    ostream &operator<<( ostream &output, const POINT &var )
    {
    output << var.xCoordinate << "." << var.yCoordinate;
    return output; }




    istream &operator>>( istream &input, POINT &var )
    {
    input >> setw( 3 ) >> var.xCoordinate; // input x
    input.ignore(); // skip period (.)
    input >> setw( 4 ) >> var.yCoordinate; // input y
    return input; // cin >> x >> y;
    }





    int main()
    {
    POINT point;

    cout << "Enter point in the form 456.7890:" << endl;
    cin >> point;

    cout << "The number entered was: ";
    cout << point << endl;

    system("pause");
    system ("CLS");

    }
    Last edited by sharris; 02-02-2011 at 02:00 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you change the data type of the member variables to int (from string), I don't actually see anything that needs to change much. If you want to print things as 037.0009, then you'll have to look in <iomanip> for the flag to left-fill with zeroes

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    90
    Ooooo Thank you tabstop. I was not sure if I even had it had it half way right though it was looking good to me. Now I see I needed to add some of the other I/O manipulators to get this code going before moving into the fun stuff that I do have a clue about.

    I just now included this:
    Code:
    cout << setw(3) << setfill('0') << point << '\n';
    And now I can type 123.1234 and it will output 123.1234 ... but before (type as string) I did not have to type the period. It would pop in automatically. I guest I'll figure out why latter but for now WOW! And to think I was about to change everything and loss it all and not recover. I almost lost faith. Thanks again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overloaded stream insertion study
    By Kayoss in forum C++ Programming
    Replies: 1
    Last Post: 05-03-2006, 12:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21