Joshua Alcott-Griffin
Joshua Alcott-Griffin

Reputation: 107

C++ Returning a string through an accessor

I am trying to use an accessor to output information from my class to the console through the main(). However, I am unsure how to return my output without a return value or a return value that contains my output. Any help would be appreciated..

string WorkTicket::showWorkTicket(int ticketNumber, string clientID, int day, int month, int year, string description) const
 {


system("cls");
cout << setw(10) << "Ticket # : " << ticketNumber << endl;
cout << setw(10) << "Client ID : " << clientID << endl;
cout << setw(8) << "Date : "<< day << "/" << month << "/" << year << endl;
cout << setw(10) << "Description : " << description << endl;


}

Upvotes: 1

Views: 276

Answers (1)

melak47
melak47

Reputation: 4850

You can use a string stream like std::ostringstream instead of std::cout to build your string in memory instead of printing it. For example:

std::string make_string_from_stuff(int x, float y, const std::string& name) {
    std::ostringstream oss;
    oss << "[" << x << ", " << y << "] : '" << name << "'";
    return oss.str();
}

will build a string like [1, 2.5] : 'Joe'

Upvotes: 1

Related Questions