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

Recommended Answers

All 25 Replies

problem in read_data method i believe.

PLEASE HELP! I DON'T UNDERSTAND WHAT IS WRONG, why it works with cin but not input.

I don't understand this code

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";
}

try to type this

ifstream& Frame_The_Phrase::open_file()
{
    string input;
    cout<<"enter file name which needs to be opened\n";
    while(!in)
    {
       cin>>input;
       in.open(input.c_str());
       if(!in.is_open())
       {
          in.close();
          cout<<"the input textfile failed please reenter the file\n";
          in.clear();
       }
    }
    cout<<"the file opened\n";
    return in;
}

You must return a descriptor of file stream to able read from that file.
P.S. function read_data takes an argument of type istream& but you try to give it ifstream&
type a new function, which takes argument of file streram descriptor.

new function must be like this

string Frame_The_Phrase::read_data(ifstream& 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";
        in.getline(word, 256, '\n'); //reading line from the file
    }
    return word;
}

check this code in your programm

is 256 the max number of characters in a line?

it can't be type ifstream for parameter if they are reading from stream.

and also the in returns a pointer.

i will just make 2 functions.

i am trying to make one function that will accept both my values from cin and a file. One paramter would be cin and other paramter would be what every ifstream name is.

you can make 2 functions, which differs types of arguments - overloads the function.
first function - reading data from input stream
second function - reading data from file stream
in addition you call each function in it's own case
type 2 different functions, and call it right
e.g.

//function which reading data from input stream
string ReadInputStream() // no needs for argument - we know, that input stream is stdin
{
    //your code for reading data from stdin
}

//function which reading data from file stream
string ReadFile(ifstream &inFile) //needs descriptor of file stream
{
    //your code for reading data from file stream
}

And calling

if(a==true)
        {
            if(c==false)
            {
                cout<<"from f\n";
                input = frame.open_file(); //return descriptor of file stream
                frame.ReadFile(input); //this
                cout<<"the word from file is"<<frame.get_word()<<"\n";
                input.close();
            }
            else
            {
                cout<<"from c\n";
                frame.ReadInputStream(); //and this
                cout<<"the word from console is"<<frame.get_word()<<"\n";
            }
            b=true;

i am trying to do it with one function. Just to make my life easier by making one function which does both.

k thanks for help.

in one function it's hard, because you must define a type of argument

you have 2 ways - overloads the function read_data, or type 2 different functions

input = frame.open_file();

that gives me an error. its says = operator is private.

if(a==true)
        {
            if(c==false)
            {
                cout<<"from f\n";
                input=frame.open_file();
                cout<<"the word from file is"<<frame.get_word()<<"\n";
            }
            else
            {
                cout<<"from c\n";
                cout<<"the word from console is"<<frame.get_word()<<"\n";
            }
            b=true;
        }

function

ifstream& Frame_The_Phrase::open_file()
{
    string input;
    cout<<"enter file name which needs to be opened\n";
    cin>>input;
    while(!in)
    {
        in.open(input.c_str());
        if(!in.is_open())
        {
            in.close();
            cout<<"the input textfile failed please reenter the file\n";
            in.clear();
        }
  
    }
    cout<<"the file opened\n";
    return in;
}

OK, than you need to keep file stream descriptor into your class
But, you can't assign one descriptor to another, so use next construction

if(a==true)
        {
            if(c==false)
            {
                cout<<"from f\n";
                frame.ReadFile(frame.open_file();); //this
                cout<<"the word from file is"<<frame.get_word()<<"\n";
                input.close();
            }
            else
            {
                cout<<"from c\n";
                frame.ReadInputStream(); //and this
                cout<<"the word from console is"<<frame.get_word()<<"\n";
            }
            b=true;

so basically, make a new function which contains the code you listed above?

oh nvm you want me to pass a frame object to my ReadFile method?

no you passing reference, returned by a function open_file()

in my cout statement it still doesn't print out the line in the file.

Revised code

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";
                cout<<frame.read_data(frame.open_file());
                cout<<"the word from file is"<<frame.get_word()<<"\n";
            }
            else
            {
                cout<<"from c\n";
                frame.readFromConsole();
                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;
}

Frame_The_Phrase::Frame_The_Phrase()
{
    j=0;
    
}

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;
}






ifstream& Frame_The_Phrase::open_file()
{
      
    string input;
    cout<<"enter file name which needs to be opened\n";
    cin>>input;
    while(!in)
    {
        in.open(input.c_str());
        if(!in.is_open())
        {
            in.close();
            cout<<"the input textfile failed please reenter the file\n";
            in.clear();
        }
    }
    cout<<"the file opened\n";
    return in;
}

string Frame_The_Phrase::readFromConsole()
{
    if(get_input()=='c')
    {
        cout<<"enter the line to be read from console\n";
        bool d=true;
        while(d==true)
        {
            cin >> word;
            prev_word = prev_word+word+" ";
            if(cin.peek()=='\n')
            {
                d=false;
                set_word(prev_word);
                break;
            }
        }
    }
    return word;

}
string Frame_The_Phrase::read_data(ifstream& in)
{
    cout<<"enter here\n";
    getline(in,word); //reading line from the file
    return word;
}




bool Frame_The_Phrase::is_space()
{
    for(j=0; j<word.length(); j++)
    {
        if(word.find(' ')==j)
        {
            m=true;
            cout<<"true"<<j<<"\n";
        }
    }
    return m;
}
    
void Frame_The_Phrase::process()
{
    cout<<"value of j is"<<j<<"\n";
    if(m==true)
    {
        word1=word.substr(0,j);
        cout<<"word1"<<word1<<"\n";
        //cout<<word1<<"\n";
        word.substr(j,word.length()-1);
        cout<<"word after is"<<word<<"\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;
    string word1;
    bool m;
    char input;
    size_t j;
    public:
    Frame_The_Phrase();
    void set_input(char x);
    char get_input();
    ifstream in;  
    void set_word(string x);
    string get_word();
    string readFromConsole();
    string read_data(ifstream& in);
    bool is_space();
    void decide_where_output();
    ifstream& open_file();
    void process();
};


#endif // FRAMETHEPHRASE_H_INCLUDED

type this and you'll see the problem

string Frame_The_Phrase::read_data(ifstream& in)
{
    if(!in.is_open())
    {
         cout << "Error, file not open!\n";
         system("pause");
         eixt(1);
    }
    cout<<"enter here\n";
    getline(in,word); //reading line from the file
    return word;
}

that doesn't seem to work. It enters the if statment, even after the file opened.

so is the file not opening?

i will just put the open function in my main and then call it.
Thanks for trying.

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.