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.

http://home.earthlink.net/~craie/122/projects/framed.phrase.html

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.