Random numbers appear and i have no idea where they are appearing from. Problem is in << operator. Not sure how to fix it.
Main
#include <iostream>
#include "Format.h"
using namespace std;
int main()
{
Format format;
format.setJustification('l');
format.add_word("one");
format.add_word("two");
format.add_word("is");
cout<<format;
return 0;
}
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)
{
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;
}
int Format::get_width()
{
return width;
}
size_t Format::get_height()
{
return height;
}
ostream& operator<< (ostream& out,Format a)
{
//out<<a.get_width();
//out<<a.get_height();
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++)
{
if(a.getJustification()=='l')
{
out<<a.get_border();
out<<ios_base::left;
out<<a.get_word(b);
out<<out.width(a.get_width()-1);
out<<a.get_border()<<"\n";
}
}
for(size_t b=0; b<(a.get_width()+2); b++)
{
out<<a.get_border();
}
return out;
}
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;
//store alignment
char justification;
public:
Format();
void setJustification(char x);
void setborder(char b);
void add_word(string w);
int get_width();
size_t get_height();
char get_border();
char getJustification();
string get_word(int index);
string is_line(int f);
string print_char();
};
ostream& operator<< (ostream& out,Format a);
#endif // FORMAT_H_INCLUDED