and how i would go about fixing it?
main

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

int main()
{
    Format format;
    //string word;
    char in;
    ifstream infile;
    bool a=true;
    bool b=true;
    bool c=true;
    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;
        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&&c==false)
        {
          cout<<"from f\n";
                string input;
                cout<<"enter file name which needs to be opened\n";
                cin>>input;
                while(!infile)
                {
                    infile.open(input.c_str());
                    if(!infile.is_open())
                    {
                        infile.close();
                        cout<<"the input textfile failed please reenter the file\n";
                        infile.clear();
                    }
                }
                cout<<"the file opened\n";
                //cout<<format.read_data(infile);
                infile>>format;
                cout<<format;
                b=true;
        }
        else if(a==true&&c==true)
        {
            cin>>format;
            //cout<<format.read_data(cin);
            cout<<format;
            b=true;
        }
    }
    return 0;
}

the infile line doesn't work :(
Implementation

#include "Format.h"
#include <vector>
#include <iostream>
using namespace std;

Format::Format()
{
    width=0;
    height=0;
    border='*';
    justification='l';
}

void Format::setJustification(char x)
{
    //cout<<"the just is"<<x<<"\n";
    justification=x;
}
char Format::getJustification()
{
    return justification;
}
void Format::setborder(char b)
{
    border=b;
}

void Format::add_word(string w)
{
   
    if(width<w.length())
    {
        width=w.length();
    }
    height++;
    words.push_back(w);
}

string Format::get_word(int index)
{
    return words[index];
}
char Format::get_border()
{
    return border;
}

size_t Format::get_width()
{
    return width;
}
size_t Format::get_height()
{
    return height;
}

void Format::add_justif(char x)
{
    justif.push_back(x);
}

char Format::getjustif(size_t z)
{
    //cout<<"justif size"<<justif.size()<<"\n";
    return justif[z];
}

ostream& operator<< (ostream& out,Format a)
{
   
    for(size_t b=0; b<(a.get_width()+2); b++)
    {
        out<<a.get_border();
    }
    out<<"\n";
    for(size_t b=0; b<(a.get_height()); b++)
    {
        //out<<"justification"<<a.getJustification()<<"\n";
        out<<a.get_border();
        if(a.getjustif(b)=='l')
        {
            out.width(a.get_width());
            out.setf(ios::left);
            out<<a.get_word(b);
            out.unsetf(ios::left);
        }
        else if(a.getjustif(b)=='r')
        {
            out.width(a.get_width());
            out.setf(ios::right);
            out<<a.get_word(b);
            out.unsetf(ios::right);
        }
        else if(a.getjustif(b)=='c')
        {
            //out<<"the word is"<<a.get_word(b);
            size_t word_size=a.get_word(b).length();
            out.width(((a.get_width()-(word_size))/2));
            out<<""<<a.get_word(b);
            out.width((a.get_width()-(word_size))-((a.get_width()-(word_size))/2));
            out<<"";
        }
        out<<a.get_border()<<"\n";
    }
    for(size_t b=0; b<(a.get_width()+2); b++)
    {
        out<<a.get_border();
    }
    out<<"\n";
    return out;
}


istream& operator>> (istream& in,Format& b)
{

    //cout<<"enter's format";
    string word;
    char just;
    size_t count=0;
    in>>word;
    //cout<<"get word"<<word<<"\n";
    b.add_word(word);
    count++;
    //cout<<"number"<<count<<"\n";
    while(in.peek()!='\n')
    {
        in>>word;
        //cout<<"last word"<<word<<"\n";
        b.add_word(word);
        count++;
        //cout<<"number"<<count<<"\n";
    }
    cout<<"what justification would you like?"
        <<"pick from 'l','r','c'\n";
        in>>just;
                //cout<<"the value is"<<just<<"\n";
        //b.setJustification(just);
        in.unget();
    for(size_t i=0; i<count; i++)
    {
        b.add_justif(just);
    }
    
    
    
    //count=0;
    //cout<<"all the words are"<<b;
    return in;
}

Interface

#ifndef FORMAT_H_INCLUDED
#define FORMAT_H_INCLUDED
using namespace std;
#include<vector>
class Format
{
    private:
    //max length of frame
    size_t width;
    //max height of frame
    int height;
    //boder character
    char border;
    //store words to be displayed
    vector<string> words;
    vector<char> justif;
    //store alignment
    char justification;
    public:
    Format();
    void add_justif(char x);
    char getjustif(size_t z);
    void setJustification(char x);
    void setborder(char b);
    void add_word(string w);
    size_t get_width();
    size_t get_height();
    char get_border();
    char getJustification();
    string get_word(int index);
    string is_line(int f);
    string print_char();
    Format read_data(istream& in);
    Format read_data_stream(ifstream& in);
};
ostream& operator<< (ostream& out,Format a);
istream& operator>> (istream& in,Format& b);
ifstream& operator>> (ifstream& in,Format& b);



#endif // FORMAT_H_INCLUDED

Recommended Answers

All 5 Replies

most likely you need to use stringstream class

#include <sstream>
...
...
istream& operator>> (istream& in,Format& b)
{

    //cout<<"enter's format";
    string word;
    string line;
    char just;
    size_t count=0;
    getline( in, line);
    stringstream str(line);
    while( str >> word )
    {
          b.add_word(word);
          count++;
    }

// Not sure about the following 
//

    cout<<"what justification would you like?"
        <<"pick from 'l','r','c'\n";
        in>>just;
                //cout<<"the value is"<<just<<"\n";
        //b.setJustification(just);
        in.unget();
    for(size_t i=0; i<count; i++)
    {
        b.add_justif(just);
    }
    
    
    
    //count=0;
    //cout<<"all the words are"<<b;
    return in;
}

so this would work for both the cin and infile?

try it.

i did it doesn't work with the ifstream.

Please help!!!!

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.