Home > database >  How should I format the output?
How should I format the output?

Time:02-26

For this code I need to be able to print the output but I am not sure how to complete this task. I can't change the main function at all and there is a certain output that I am looking for. The expected output should be formatted as the widget name, ID, then the address of the widget. I am thinking that I could use a string for the output but I'm not sure how to go about implementing it.
The main.cpp is

#include <iostream>
#include <string>
#include "widget.h"

using namespace std;

int main()
{
    //makes three widgets using the regular constructor
    const Widget weather( WEATHER );
    const Widget quote( QUOTE );
    const Widget stock( STOCK );
   
    cout << "weather widget 1: " << weather.getModelName() << endl;
    cout << "quote widget 1: " << quote.getModelName() << endl;
    cout << "stock widget 1: " << stock.getModelName() << endl;
   
    //makes three widgets using the copy constructor
    Widget weather2 = weather;
    Widget quote2 = quote;
    Widget stock2 = stock;
   
    cout << "weather widget 2: " << weather2.getModelName() << endl;
    cout << "quote widget 2: " << quote2.getModelName() << endl;
    cout << "stock widget 2: " << stock2.getModelName() << endl;
   
    cin.get();
   
   return 0;
   
}

The widget.h is

#include <string>

using namespace std;

enum WidgetType
{
    INVALID_TYPE = -1,
    WEATHER,
    QUOTE,
    STOCK,
    NUM_WIDGET_TYPES
};


const string WIDGET_NAMES[NUM_WIDGET_TYPES] = { "Weather2000",
   "Of-The-Day",
   "Ups-and-Downs"
};


class Widget
{
public:
    Widget( WidgetType type );
    //add copy constructor
  Widget( const Widget& rhs );

    string getModelName() const { return wModelName; };
  WidgetType getType() {return wType;};
  
private:
    WidgetType wType;
    int wID;
    string wModelName;
  static int userID;
  //add static data member
  
    void generateModelName();
};

Then the widget.cpp is

#include "iostream"
#include "widget.h"

  int Widget::userID = 1;
  Widget::Widget( WidgetType type )
  {
    wID = userID;
    wType = type;
    wModelName = wType;
    userID  ;
    generateModelName();
  }

  Widget::Widget( const Widget& rhs )
  {
    wID = userID;
    wType = rhs.wType;
    wModelName = wType;
    userID  ;
    generateModelName();
  }

void Widget::generateModelName()
{
  if (getType() == WEATHER)
  {
     wModelName = "Weather2000";
  }
  else if (getType() == QUOTE)
  {
     wModelName = "Of-The-Day";
  }
  else if (getType() == STOCK)
  {
     wModelName = "Ups-and-Downs";
  }
}

Finally the expected out is

weather widget 1:       Weather2000    1  000000145FD3F628
quote widget 1:         Of-The-Day     2  000000145FD3F678
stock widget 1:         Ups-and-Downs  3  000000145FD3F6C8
weather widget 2:       Weather2000    4  000000145FD3F718
quote widget 2:         Of-The-Day     5  000000145FD3F768
stock widget 2:         Ups-and-Downs  6  000000145FD3F7B8

CodePudding user response:

You could change getModelName to generate the output you need with the help of a std::ostringstream.

Old implementation:

string getModelName() const { return wModelName; };

New version:

#include <sstream> // std::ostringstream

string getModelName() const {
    std::ostringstream os;
    os << '\t' << wModelName << '\t' << wID << "  " << std::hex << this;
    return os.str();
};

Demo

  •  Tags:  
  • c
  • Related