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 worked fine.
    {
            inputline.erase(0,1);
            input.ignore(0,1);
    }
    output<<inputline<<"\n";
}
void line_test::isvalidname(ifstream& input,ofstream& output)
{
    if(input>>first_name>>ws>>last_name)
    {
        output<<"name               "<<inputline2<<"\n";//"name    "<<inputline2<<"\n";
    }
    else if(input>>idnumber)
    {
        output<<"id                  "<<inputline2<<"\n";//"id     "<<inputline2<<"\n";
    }
    else if(input>>gpa)
    {
        output<<"gpa                 "<<input<<inputline2;//"gpa    "<<inputline2<<"\n";
    }
    else if(input>>gender)
    {
        output<<"gender              "<<inputline2<<"\n";//"gender    "<<inputline2<<"\n";
    }
    else
    {
    output<<"none"<<"\n";
    }
}

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 isequal(ifstream& input,ofstream& output);
    void isvalidname(ifstream& input,ofstream& output);
    string inputline;
    //string inputline3;
    string inputline2;
    string first_name;
    char ws;
    string last_name;
    long idnumber;
    double gpa;
    char gender;
};



#endif // LINE_TEST_H_INCLUDED

Recommended Answers

All 6 Replies

void line_test::isvalidname(ifstream& input,ofstream& output)
{
    if(input>>first_name>>ws>>last_name)
    {
        output<<"name               "<<inputline2<<"\n";//"name    "<<inputline2<<"\n";
    }
    else if(input>>idnumber)
    {
        output<<"id                  "<<inputline2<<"\n";//"id     "<<inputline2<<"\n";
    }
    else if(input>>gpa)
    {
        output<<"gpa                 "<<input<<inputline2;//"gpa    "<<inputline2<<"\n";
    }
    else if(input>>gender)
    {
        output<<"gender              "<<inputline2<<"\n";//"gender    "<<inputline2<<"\n";
    }
    else
    {
    output<<"none"<<"\n";
    }
}

Don't know what you are trying to do here, but I almost positive the code isn't doing what you intend it to. You are continually trying to read from a stream and checking whether the read was successful in your if and else if statements. If the read was NOT successful, you try to read MORE from the stream? Once the stream fails, it's failed. Don't try to read more from the stream until you know WHY it's failed and you've reset the bits. I think you need to explain what you are attempting to do here.

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?

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?

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?

There is no find command in the function you are asking about and that I posted. You are testing the eofbit, badbit, and failbit for your ifstream. I forget the exact criteria where >> returns true and false. I think it may be if any of these three bits are set, >> fails, but don't quote me.

If that's not what you are INTENDING to do, you need to do a rewrite of this function. Your if and else if statements are going to return false when you hit the end of the file or if you try to read a letter into an integer or something. If you have GOOD data, you'll never get past the first if statement:

if(input>>first_name>>ws>>last_name)
    {
        output<<"name               "<<inputline2<<"\n";//"name    "<<inputline2<<"\n";
    }

All else-if and else statements are ignored when the if-statement is true. This if-statement will be true if the read was SUCCESSFUL.

k. thanks for help will look into that.

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.