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

try to use debug. Place breakpoint at the lines you thought you have error(s) and check the varibles values.

but why would it get numbers? when i am not asking it to? and can ios_base only be used with cout?

nvm it I think i know why the numbers were printing out, because i was asking for them to be printed out. with the out<<

thanks for 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.