word is a private variable.
MAin
#include <iostream>
#include "FrameThePhrase.h"
using namespace std;
int main()
{
Frame_The_Phrase frame;
char in;
ifstream input;
bool a=true;
bool b=true;
bool c=true;
string length;
while(b==true)
{
cout<<"to quit type 'q'\n"
<<"enter 'c' to get from console\n"
<<"enter 'f' to get from file\n";
cin>>in;
frame.set_input(in);
if(in=='q')
{
cout<<"quit program\n";
a=false;
b=false;
}
else if(in=='c')
{
cout<<"enter console\n";
a=true;
c=true;
}
else if(in=='f')
{
cout<<"read from file\n";
a=true;
c=false;
}
if(a==true)
{
if(c==false)
{
cout<<"from f\n";
frame.open_file();
frame.read_data(input);
cout<<"the word from file is"<<frame.get_word()<<"\n";
input.close();
}
else
{
cout<<"from c\n";
frame.read_data(cin);
cout<<"the word from console is"<<frame.get_word()<<"\n";
}
b=true;
}
}
return 0;
}
Implementation
#include "FrameThePhrase.h"
#include<iostream>
#include <fstream>
using namespace std;
void Frame_The_Phrase::set_word(string x)
{
word=x;
}
string Frame_The_Phrase::get_word()
{
return word;
}
void Frame_The_Phrase::set_input(char x)
{
input=x;
}
char Frame_The_Phrase::get_input()
{
return input;
}
string Frame_The_Phrase::read_data(istream& in)
{
if(get_input()=='c')
{
cout<<"enter the line to be read from console\n";
bool d=true;
while(d==true)
{
in>>word;
prev_word=prev_word+word+" ";
if(in.peek()=='\n')
{
d=false;
set_word(prev_word);
break;
}
}
}
else if(get_input()=='f')
{
cout<<"enter here\n";
getline(in,word);
}
return word;
}
bool Frame_The_Phrase::is_space()
{
size_t j=0;
for(j=0; j<word.length(); j++)
{
if(word.find(' ')==j)
{
m=true;
cout<<"true"<<j<<"\n";
}
}
return m;
}
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";
}
}
Interface
#ifndef FRAMETHEPHRASE_H_INCLUDED
#define FRAMETHEPHRASE_H_INCLUDED
#include <iostream>
#include <fstream>
using namespace std;
class Frame_The_Phrase
{
private:
string word;
string prev_word;
bool m;
char input;
public:
void set_input(char x);
char get_input();
ifstream in;
void set_word(string x);
string get_word();
string read_data(istream& in);
bool is_space();
void decide_where_output();
void open_file();
void process();
};
#endif // FRAMETHEPHRASE_H_INCLUDED