Here is a link to the actual project. I could easily use getline for the phrase. Don't need to overload it to read in phrase.

Dani AI

Generated

Overloading >> and << is mostly about creating an idiomatic, reusable I/O interface for your class so callers can write cin >> frame and cout << frame (or ifstream >> frame, ostringstream << frame) without duplicating parsing/formatting code in main. hinted at that: the exercise shows you can encapsulate how a phrase is read and how it is printed (the “framing” logic) so the rest of the program only deals with streams and objects.

Practical notes tied to your code: implement operator>> as a non‑member that returns std::istream& and either use the class' set_word() or make the operator a friend so it can store the whole line (phrases contain spaces). Implement operator<< to do the framing/formatting so printing works the same whether the destination is the console or a file. This keeps decide_where_output() and open_file() simple — open the stream and then use in >> frame or out << frame.

Example pattern (adapt to your class: make get_word() a const accessor or make operator<< a friend):

std::istream& operator>>(std::istream& is, Frame_The_Phrase& f) {
    std::string line;
    std::getline(is >> std::ws, line);   // consume leftover newline then read whole line
    f.set_word(line);
    return is;
}

std::ostream& operator<<(std::ostream& os, const Frame_The_Phrase& f) {
    // replace with the actual framing logic required by the project
    os << f.get_word();
    return os;
}

Troubleshooting tips for your current main: reading the menu with cin >> in; leaves a newline in the stream, so a following getline will return an empty string. Fix by using cin.ignore(...) or read the menu with getline and parse the first character, or use the std::ws trick shown above. When reading from files, construct an ifstream infile(name); and then do infile >> frame; after checking if (!infile). Implementing the operators this way makes your code cleaner, testable, and works uniformly for console and file I/O — which is almost certainly what your teacher intends.

Recommended Answers

All 5 Replies

here is my code.

Main

#include <iostream>
#include "FrameThePhrase.h"
using namespace std;

int main()
{
    
    Frame_The_Phrase frame;
    char in;
    
    bool a=true;
    //frame.read_data(cin);
    //bool b=true;
    while(a==true)
    {
        cout<<"to quit type q\n"
            <<"enter c to get from console\n"
            <<"enter f to get from file\n";
        cin>>in;
        if(in=='q')
        {
            cout<<"quit program";
            a=false;
            
        }
        else if(in=='c')
        {
            cout<<"enter console\n";
            a=true;
            frame.read_data(cin);
            frame.decide_where_output();
        }
        else if(in=='f')
        {
            cout<<"read from file";
            a=true;
            frame.open_file();
            frame.decide_where_output();
        }
    }
    return 0;
}

Frame_the_Phrase.h

#ifndef FRAMETHEPHRASE_H_INCLUDED
#define FRAMETHEPHRASE_H_INCLUDED
#include <iostream>
#include <fstream>
using namespace std;
class Frame_The_Phrase
{
    private:
    string word;
    public:
    
    ifstream in;    
    void set_word(string x);
    string get_word();
    void read_data(istream& in);
    void decide_where_output();
    void open_file();
    
};

istream& operator >>(istream& in,Frame_The_Phrase& hh);

#endif // FRAMETHEPHRASE_H_INCLUDED

Framethephrase.ccp

#include "FrameThePhrase.h"
#include<iostream>
#include <fstream>
using namespace std;

istream& operator >>(istream& in, Frame_The_Phrase& hh)
{
    cout<<"override?";
    in.unget();
    return in;
}



void Frame_The_Phrase::set_word(string x)
{
    word=x;
}
string Frame_The_Phrase::get_word()
{
    return word;
}

void Frame_The_Phrase::read_data(istream& in)
{
    cout<<"enter the line to be read from file\n";
    in>>word;
    set_word(word);
    cout<<"the value after is"<<get_word();
    return set_word(word);
}
void Frame_The_Phrase::open_file()
{
    string input;
    
    cout<<"enter file name which needs to be opened\n";
    cin>>input;
    in.open(input.c_str());
    while(!in)
    {
        in.close();
        cout<<"the input textfile failed please reenter the file\n";
        in.clear();
        cin>>input;
        in.open(input.c_str());
    }
    cout<<"the file opened\n";
}



void Frame_The_Phrase::decide_where_output()
{
    char out;
    cout<<"enter s to print to console\n";
    cout<<"enter n to save to output file\n";
    cin>>out;
    if(out=='s')
    {
        cout<<"print to console\n";
    }
    else if(out=='n')
    {
        cout<<"print to output\n";
    }
}

Please help!!!!

I'm not sure what you're asking for help with, but you would want to overload << and >> so you could do something like this:

class A
{
int a,b;
};

int main()
{
A MyA;
cin >> A;
//instead of
/*
int a , b;
cin >> a >> b;
A.a = a;
A.b = b;
*/
return 0;
}

It's just much cleaner/easier to read, and it is a good to way to reuse code (so you don't have to do that little pattern over and over)

Dave

i am really confued as to why he wants me to overload the >> and << when i could just use getline since each line contains a phrase which i have to process.

thanks i think that's what he expects.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.