lotrsimp12345 37 Posting Pro in Training

so is the file not opening?

lotrsimp12345 37 Posting Pro in Training

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

lotrsimp12345 37 Posting Pro in Training

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 …
lotrsimp12345 37 Posting Pro in Training

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

lotrsimp12345 37 Posting Pro in Training

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

lotrsimp12345 37 Posting Pro in Training

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

lotrsimp12345 37 Posting Pro in Training
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;
}
lotrsimp12345 37 Posting Pro in Training

input = frame.open_file();

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

lotrsimp12345 37 Posting Pro in Training

k thanks for help.

lotrsimp12345 37 Posting Pro in Training

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

lotrsimp12345 37 Posting Pro in Training

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.

lotrsimp12345 37 Posting Pro in Training

i will just make 2 functions.

lotrsimp12345 37 Posting Pro in Training

and also the in returns a pointer.

lotrsimp12345 37 Posting Pro in Training

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

lotrsimp12345 37 Posting Pro in Training

is 256 the max number of characters in a line?

lotrsimp12345 37 Posting Pro in Training

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

lotrsimp12345 37 Posting Pro in Training

problem in read_data method i believe.

lotrsimp12345 37 Posting Pro in Training

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 …
lotrsimp12345 37 Posting Pro in Training

thanks i think that's what he expects.

lotrsimp12345 37 Posting Pro in Training

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.

lotrsimp12345 37 Posting Pro in Training

Please help!!!!

lotrsimp12345 37 Posting Pro in Training

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";
    }
}
lotrsimp12345 37 Posting Pro in Training

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

lotrsimp12345 37 Posting Pro in Training

so basically here i want the buffer to continue to read until it hits the \n character?

lotrsimp12345 37 Posting Pro in Training

i have tried it just not sure why he wants me to overload the >> operator.

lotrsimp12345 37 Posting Pro in Training

i have a project which i am working on. I am supposed to read in a phrase then every time it encounters a space the word is supposed to go to a newline in my output. The phrase can take up only one line. The phrase can be read from a file or from cin. The phrase contains spaces so i am guessing you are supposed to overload input >> to make it take a space containing line when they enter cin>>string word. by overloading with getline()???

lotrsimp12345 37 Posting Pro in Training

I understand the concept but i don't know when to use and what my teacher expects when he says overload certain operators.

lotrsimp12345 37 Posting Pro in Training

basically i am trying to use constructors with operator overloading. I think i understand. You can call these constructors in other methods.

EX: maybe a set_x() and set_y() in the constructor which accepts two double values. The set_x(double x). You can call the input method and call this particular constructor.

I am getting really confused on when to use operator overloading.
Can someone explain?

lotrsimp12345 37 Posting Pro in Training

Please explain.
Does it automatically include the default constructor?

lotrsimp12345 37 Posting Pro in Training

would i have to overload the function?

lotrsimp12345 37 Posting Pro in Training

but i want it to be between 2 numbers. can i change rand_max number?

lotrsimp12345 37 Posting Pro in Training

IT is defined in the back of my book but can't be found in my program.

lotrsimp12345 37 Posting Pro in Training

When you use getline(input,inputline) and you delete part of the inputline does it also ignore from the input file?

lotrsimp12345 37 Posting Pro in Training

k. thanks for help will look into that.

lotrsimp12345 37 Posting Pro in Training

they enter a file which contains labeled data.
the labels are name, id, gpa and gender. i have to make sure then line contains only those labels. Then i have to make sure there is an equal to after the label. And finally make sure after the equal to sign if it is name it can be a space containing string, if id can only be a long.

so i use the find method to find out the conditions. I think i am using my ignore command the wrong way?

lotrsimp12345 37 Posting Pro in Training

they enter a file which contains labeled data.
the labels are name, id, gpa and gender. i have to make sure then line contains only those labels. Then i have to make sure there is an equal to after the label. And finally make sure after the equal to sign if it is name it can be a space containing string, if id can only be a long.
so i use the find method to find out the conditions. I think i am using my ignore command the wrong way?

lotrsimp12345 37 Posting Pro in Training

ITS ALL THE WAY AT THE BOTTOM OF MY IMPLEMENTATION FILE.

MAIN

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

int main()
{
    //open up file to be read 
    string input;
    ifstream in;
    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";
    //open up file to copy to
    string output;
    ofstream out;
    cout<<"enter file name which to copy to\n";
    cin>>output;
    out.open(output.c_str());
    while(!out)
    {
        out.close();
        cout<<"output file opening failed. Please reenter output file name\n";
        out.clear();
        cin>>output;
        out.open(output.c_str());
    }
    //processing of file
    line_test a;
    out<<a.getfirst3(in);
    
    //string inputline;
    
    while(!in.eof())
    {
        a.iscomment(in,out);
        a.islabel(in,out);
        a.isequal(in,out);
        a.isvalidname(in,out);
    }
    in.close();
    out.close();
    return 0;

}

IMPLEMENTATION

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

//retrieve first 3 lines
string line_test::getfirst3(ifstream& input)
{
    string newline="\n";
    getline(input,title_comments);
    getline(input,title_comments1);
    getline(input,title_comments2);
    title_comments=title_comments+newline;
    title_comments1=title_comments1+newline;
    title_comments2=title_comments2+newline;
    first_three_lines=title_comments+title_comments1+title_comments2;
    return first_three_lines;
}
//function used to check if there are comments

void line_test::iscomment(ifstream& input,ofstream& output)
{
    getline(input,inputline);
    inputline2=inputline;
    if(inputline.find('#')==0)
    {
        int length=inputline.length();
        inputline.erase(0,length);
        inputline2.erase(0,length);
        input.ignore(0,length);
    }
    else
    {
        //output<<inputline<<"\n";
    }
}

//checks if data is labeled correctly
void line_test::islabel(ifstream& input,ofstream& output)
{
    if(inputline.find("name")==0)
    {
        inputline.erase(0,4);
        input.ignore(0,4);
        //output<<inputline<<"\n";
        //cout<<"name"<<inputline2<<"\n";
    }
    
    else if(inputline.find("ID")==0)
    {
        inputline.erase(0,2);
        input.ignore(0,2);
        //output<<inputline<<"\n";
        //cout<<"id"<<inputline2<<"\n";
    }
    else if(inputline.find("GPA")==0)
    {
        inputline.erase(0,3);
        input.ignore(0,3);
        //output<<inputline<<"\n";
        //cout<<"gpa"<<inputline2<<"\n";
    }
    else if(inputline.find("gender")==0)
    {
        inputline.erase(0,6);
        input.ignore(0,6);
        //output<<inputline<<"\n";
        //cout<<"gender"<<inputline2<<"\n";
    }
    else
    {
        int length=inputline.length();
        inputline.erase(0,length);
        //inputline2.erase(0,length);
        input.ignore(0,length);
        //output<<"";
    }
}

//check if their an equal sign 
void line_test::isequal(ifstream& input,ofstream& output)
{
    
    while(inputline.find(' ')==0||inputline.find('=')==0) //I changed inputline2 to inputline and it …
lotrsimp12345 37 Posting Pro in Training

nvm i think i know what the problem is.

lotrsimp12345 37 Posting Pro in Training

why do i get undefined reference to error in my main with regards to isvalidname. It says undefined reference to...


in my isvalidname i check if they enter in first name, char ws and then last name. Then i want to print out everything that i have deleted on that line. i guess i could use getback() or putback() or store my string at the beginning.

MAIN

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

int main()
{
    //open up file to be read 
    string input;
    ifstream in;
    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";
    //open up file to copy to
    string output;
    ofstream out;
    cout<<"enter file name which to copy to\n";
    cin>>output;
    out.open(output.c_str());
    while(!out)
    {
        out.close();
        cout<<"output file opening failed. Please reenter output file name\n";
        out.clear();
        cin>>output;
        out.open(output.c_str());
    }
    //processing of file
    line_test a;
    out<<a.getfirst3(in);
    
    //string inputline;
    
    while(!in.eof())
    {
        a.iscomment(in,out);
        a.islabel(in,out);
        a.isequal(in,out);
        a.isvalidname(in,out);
        /*
        getline(in,inputline);
        a.iscomment(in);
        a.islabel(in);
        a.isequal(in);
        a.is_space();
        a.isvalidname(in);*/
    }
    in.close();
    out.close();
    return 0;

}

INTERFACE

#ifndef LINE_TEST_H_INCLUDED
#define LINE_TEST_H_INCLUDED
#include <fstream>
#include <iostream>
using namespace std;
class line_test
{
    public:
    //void iscomment(ifstream& input,ofstream& output);
    //string inputline;
    //string inputline1;
    //void islabel(ifstream& input,ofstream& output);
    
    
    //first line of file
    string title_comments;
    //second line of file
    string title_comments1;
    //third line of file
    string title_comments2;
    //first three lines of code
    string first_three_lines;
    string getfirst3(ifstream& input);
    
    void iscomment(ifstream& input,ofstream& output);
    void islabel(ifstream& input,ofstream& output);
    void …
lotrsimp12345 37 Posting Pro in Training

thanks it works now!! wasted 4 hours on this. :(.

lotrsimp12345 37 Posting Pro in Training

all my files end in the correct ending should i rename the file?

lotrsimp12345 37 Posting Pro in Training

codeblocks

lotrsimp12345 37 Posting Pro in Training

thanks for help. I will just ask teacher tom.

lotrsimp12345 37 Posting Pro in Training

already have
here is modified code
main

/* 
The purpose of this lab is to open a text file and then make 
a copy of it reading in name, id number, gpa and grade
*/


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

int main()
{
//open up file to be read from
    string input;
    ifstream in;
    cout<<"enter file name which needs to be openedn";
    cin>>input;
    in.open(input.c_str());
    while(!in)
    {
        in.close();
        cout<<"the input textfile failed please reenter the filen";
        in.clear();
        cin>>input;
        in.open(input.c_str());
    }
    cout<<"the file openedn";
    //open up file to copy to
    string output;
    ofstream out;
    cout<<"enter file name which to copy ton";
    cin>>output;
    out.open(output.c_str());
    while(!out)
    {
        out.close();
        cout<<"output file opening failed. Please reenter output file namen";
        out.clear();
        cin>>output;
        out.open(output.c_str());
    }

    copyname a;
    //read in data
        a.readinput(in);
    //
    return 0;
}

interface

#ifndef INTERFACE_H_INCLUDED
#define INTERFACE_H_INCLUDED
#include <iostream>
#include <fstream>
using namespace std;
class copyname
{
    public:
    void readinput(ifstream& inputfile);
};

#endif // INTERFACE_H_INCLUDED

implementation

#include "Interface.h"
#include <fstream>
using namespace std;
void copyname::readinput(ifstream& inputfile)
{
    string name;
    getline(inputfile,name);
    cout<<name;
}
lotrsimp12345 37 Posting Pro in Training

undefiined reference to 'copyname::readinput(std::basic_ifstream<char,std::char_traits...'

lotrsimp12345 37 Posting Pro in Training

still gives me same error. but works if i have it in int main()

lotrsimp12345 37 Posting Pro in Training

thanks for the help.

lotrsimp12345 37 Posting Pro in Training

it works if i have it in my int main so i must be doing something wrong with the implementation and interface files.

lotrsimp12345 37 Posting Pro in Training

doesn't like a.readinput(in)

lotrsimp12345 37 Posting Pro in Training

gives me error

main

/* 
The purpose of this lab is to open a text file and then make 
a copy of it
*/


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

int main()
{
//open up file to be read from
    string input;
    ifstream in;
    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";
    //open up file to copy to
    string output;
    ofstream out;
    cout<<"enter file name which to copy to\n";
    cin>>output;
    out.open(output.c_str());
    while(!out)
    {
        out.close();
        cout<<"output file opening failed. Please reenter output file name\n";
        out.clear();
        cin>>output;
        out.open(output.c_str());
    }
    copyname a;
//read in data
    a.readinput(in);
//   use f.getline for Cstring and >> for rest...
    return 0;
}

interface

#ifndef INTERFACE_H_INCLUDED
#define INTERFACE_H_INCLUDED
#include <iostream>
using namespace std;
class copyname
{
    public:
    void readinput(ifstream& input);

};

#endif // INTERFACE_H_INCLUDED

implementation

#include "Interface.h"
#include <fstream>

void readinput(ifstream& input)
{
    string name;
    getline(input,name);
    cout<<name; 
}

error in main not sure why though i declare a class than try to call the function.