Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Struggling with operator overloading C++

Options
  • 25-11-2012 4:29pm
    #1
    Registered Users Posts: 1,374 ✭✭✭


    Hi everyone. I'm currently working on a project.

    I am having troubles with the overloaded >>. This is my definition:

    istream & operator>> (istream & is, Postal & post)
    {
    is >> post.name;
    is >> post.street;
    is >> post.town;
    is >> post.county;
    is >> post.country;
    return is;
    }

    Basically, the code is supposed to take in a postal address. When I enter a name with two names(first name _ last name) with a space between them, the last name gets stored into street. Any solutions to this?

    I would appreciate all the help as I am a beginner programmer. Thank you guys. :)


Comments

  • Registered Users Posts: 27,161 ✭✭✭✭GreeBo


    You probably need to surround the name with quotes (") so that they are grouped.
    Remember the computer has no idea what a name is, so
    John Smith Dublin means the same to it as
    John Dublin Ireland

    it just sees 3 inputs
    but "John Smith" Dublin it will see as 2 inputs.


  • Registered Users Posts: 1,374 ✭✭✭LeakingLava


    hhmm. Is there any other way of doing it? Because I don't think that asking a user to input the details surrounded with quotes is a good solution. If it was just me using the program, it would be okay. But unfortunately this program will be checked by my lecturer :(

    Btw, I just tried your way of doing it and it still doesn't work. It seems like the program doesn't recognise the quotes at all. It recognises the quotes as a character included in the string too. The space still terminates one input.


  • Registered Users Posts: 27,161 ✭✭✭✭GreeBo


    I havent used C++ in what feels like a thousand years, but how about separating the inputs onto each line (with a prompt for what each one is)?

    std::cout >> "Enter Name"
    getline(std::cin, name);
    std::cout >> "Enter Street"
    getline(std::cin, street);
    std::cout >> "Enter Town"
    getline(std::cin, town);


  • Registered Users Posts: 1,374 ✭✭✭LeakingLava


    GreeBo wrote: »
    I havent used C++ in what feels like a thousand years, but how about separating the inputs onto each line (with a prompt for what each one is)?

    std::cout >> "Enter Name"
    getline(std::cin, name);
    std::cout >> "Enter Street"
    getline(std::cin, street);
    std::cout >> "Enter Town"
    getline(std::cin, town);

    What do you mean exactly? Do you want me to put this into the definition of the operator or just use this instead of the operator? Unfortunately, if you meant that I should just use this instead of the operator overloading, I can't do that :( I need to use iostream overloaders :(.


    Btw, I do not fully understand the operator overloading but as I see it, 'is' is the input of the user and the 'post.name' is where it goes(?). I tried to use strcpy in there by:

    strcpy(post.name, is);


    And that gives me an error which most likely means that my understanding of it is completely off.


  • Registered Users Posts: 1,374 ✭✭✭LeakingLava


    Some progress.

    I changed all the 'is >> whatever' lines into:

    istream & operator>> (istream & is, Postal & post)
    {
    cin.getline(post.name, 30);
    cin.getline(post.street, 30);
    cin.getline(post.town, 12);
    cin.getline(post.county, 12);
    cin.getline(post.country, 15);
    return is;
    }

    This now makes my program work but my worry is, would this still be considered as operator overloading? I really have no idea what the 'is' things are. Maybe that's what I should look at first.. o.o


  • Advertisement
  • Registered Users Posts: 27,161 ✭✭✭✭GreeBo


    Some progress.

    I changed all the 'is >> whatever' lines into:

    istream & operator>> (istream & is, Postal & post)
    {
    cin.getline(post.name, 30);
    cin.getline(post.street, 30);
    cin.getline(post.town, 12);
    cin.getline(post.county, 12);
    cin.getline(post.country, 15);
    return is;
    }

    This now makes my program work but my worry is, would this still be considered as operator overloading? I really have no idea what the 'is' things are. Maybe that's what I should look at first.. o.o

    "is" is the input stream (istream) that is being passed to the >> operator, the same way you are passing post which is an instance of your Postal class.

    so you should be using is.getLine and not cin.getLine as "is" is the istream you are passing.

    Overloading means that you are making an operator do something that it doesnt/didnt ordinarily do out of the box.
    In this case you are saying that when someone uses ">>" with your Postal class they will end up with an instance of Postal, if you dont do this then library has no idea how to read in a Postal object from a user.
    You could just create your own function called "readInAPostalObjectFromStdIn()" or something and have it do the same thing, but the convention is to overload the ">>" or input operator as everyone will automatically know what it does.


    /disclaimer
    Its been *years* since I did C++ so maybe better to wait for an expert to answer!


  • Registered Users Posts: 1,374 ✭✭✭LeakingLava


    GreeBo wrote: »
    "is" is the input stream (istream) that is being passed to the >> operator, the same way you are passing post which is an instance of your Postal class.

    so you should be using is.getLine and not cin.getLine as "is" is the istream you are passing.

    Overloading means that you are making an operator do something that it doesnt/didnt ordinarily do out of the box.
    In this case you are saying that when someone uses ">>" with your Postal class they will end up with an instance of Postal, if you dont do this then library has no idea how to read in a Postal object from a user.
    You could just create your own function called "readInAPostalObjectFromStdIn()" or something and have it do the same thing, but the convention is to overload the ">>" or input operator as everyone will automatically know what it does.


    /disclaimer
    Its been *years* since I did C++ so maybe better to wait for an expert to answer!

    This is amazing help. I've just changed all the cin's into is'. Do you think this is now a proper operator overloading function? It is all working by the way. Thank you so much.

    istream & operator>> (istream & is, Postal & post)
    {
    is.getline(post.name, 30);
    is.getline(post.street, 30);
    is.getline(post.town, 12);
    is.getline(post.county, 12);
    is.getline(post.country, 15);
    return is;
    }


  • Registered Users Posts: 27,161 ✭✭✭✭GreeBo


    Np, glad to help.

    Yep, looks good to me.

    Shout if you have any questions about your solution, nothing worse than submitting an answer that you dont understand, you will just fall further and further behind.


  • Registered Users Posts: 1,374 ✭✭✭LeakingLava


    Sorry if I'm bothering you further, how do I store the details of a certain declared object of this class onto an ascii file?


  • Registered Users Posts: 27,161 ✭✭✭✭GreeBo


    Sorry if I'm bothering you further, how do I store the details of a certain declared object of this class onto an ascii file?

    well you need to do a few things
    1) Figure out what details you want to write and in what format
    2) Output that onto the console/screen
    3) Figure out how to output to a file
    4) Join 2 and 3 :)


    Its always easier to deal with these problems when you break them down into smaller steps.

    Take a stab at the above and come back with questions if/when you get stuck; but make sure you try first!


  • Advertisement
  • Registered Users Posts: 1,374 ✭✭✭LeakingLava


    GreeBo wrote: »
    well you need to do a few things
    1) Figure out what details you want to write and in what format
    2) Output that onto the console/screen
    3) Figure out how to output to a file
    4) Join 2 and 3 :)


    Its always easier to deal with these problems when you break them down into smaller steps.

    Take a stab at the above and come back with questions if/when you get stuck; but make sure you try first!

    1) I would like all of the details into the file. Not sure what you mean by 'format' but it's an 'ascii file' I want to store into.
    2) I can output the details onto the console
    3) Here's a bit of a problem:

    I've put this into main and I get an error on 'fout' saying that it's an incomplete type:

    ofstream fout("output.txt");

    4) I have an idea how to put the details it into the file. Seems really simple. But I'm kind of stuck at part 3.


    Got the error fixed:

    I had #include<fstream.h> instead of just fstream.

    Seems like I have everything done. Thanks a lot again. :)


  • Registered Users Posts: 27,161 ✭✭✭✭GreeBo


    good job.
    by format i was thinking that you will want some function to output the values and some description of them in a nicely formatted string.this
    function would be part of the object so you could use it anytime you needed to output the details.


  • Registered Users Posts: 1,374 ✭✭✭LeakingLava


    GreeBo wrote: »
    good job.
    by format i was thinking that you will want some function to output the values and some description of them in a nicely formatted string.this
    function would be part of the object so you could use it anytime you needed to output the details.

    I could do that actually. I'll try it out tomorrow. Again, thanks a lot man. You helped so much :)


  • Registered Users Posts: 1,374 ✭✭✭LeakingLava


    Another question. Is there a way for me to be able to ask--...erm. Hard to explain. Here is what I want to do:

    if(-an object is of class tracking-)
    {

    do this to the postage cost

    }

    Basically, I want to have an if statement which says that if an object is of tracking class(which means it's registered because it has a tracking number), then the postage cost should be 50% more.

    The only difference between tracking class and postal class is that the tracking class has a tracking id. It inherits everything else from postal.


  • Registered Users Posts: 2,022 ✭✭✭Colonel Panic


    Give your postal class a CalcPostage method. Make it virtual. Do standard calculations in it. In the tracking class give it an overrided CalcPostage method, do different postage calculations in there. Now you don't need to worry about identifying the class type at runtime.


Advertisement