-1

Saluton Mundo, I've been working on this function that prints the lettering on a check based upon the numbers. The only problem is that I made it into a void, and I have no clue as to how to convert from void-> string. Basically impossible if you ask me. In short, how can I rewrite this so that it returns a string.

My code below:

void numALet(string &can)
{
    int conNum= atoi(can.c_str());
    float conNumF= atof(can.c_str());

    //Snippet to find the Cents.
    float cen= (int)((conNumF- conNum)* 100);

    string basic[20]= { " ", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten",
                        "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"},

           diez[9]= { "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};

    if(conNum< 20)
        cout<< basic[conNum]<< " and "<< (cen+ 1) << " Cents.";
    else if(conNum< 100 && conNum> 19)
    {
        int num= conNum% 100;
        if(num% 10!= 0)
            cout<< diez[num/10- 1]<< " "<< basic[num% 10]<< " and "<< (cen+ 1) << " Cents.";
        else
            cout<< diez[conNum/ 10- 1]<< " and "<< (cen+ 1)<< " Cents.";
    }
    else if(conNum> 99 && conNum< 1000)
    {
        int num1= conNum% 1000;
        cout<< basic[num1/ 100]<< " "<< "Hundred ";
        int num2= conNum% 100;

        if(conNum< 20)
            cout<< basic[num2]<< " and "<< (cen+ 1) << " Cents.";
        else if(num2< 100 && num2> 19)
        {
            if(num2% 10!= 0)
                cout<< diez[num2/ 10- 1]<< " "<< basic[num2% 10]<< " and "<< (cen+ 1) << " Cents.";
            else
                cout<< diez[num2/ 10- 1]<< " and "<< (cen+ 1) << " Cents.";
        }
    }
    else if(conNum> 999 && conNum< 10000)
    {
        cout<< basic[conNum/ 1000]<< " "<< "Thousand ";
        int num1= conNum% 1000;
        cout<< basic[num1/ 100]<< " "<< "Hundred ";
        int num2= conNum% 100;

        if(conNum< 20)
            cout<< basic[num2]<< " and "<< cen << " Cents.";
        else if(num2< 100 && num2> 19)
        {
            if(num2% 10!= 0)
                cout<< diez[num2/ 10- 1]<< " "<< basic[num2% 10]<< " and "<< (cen) << " Cents.";
            else
                cout<< diez[num2/ 10- 1];
        }
    }
}

Output:

Nine Thousand Nine Hundred Ninety Nine and 99 Cents.

P.S: Out of the blue it returns 99, 100 when cen is called, which is why I added + 1.

Thank you for your help.

Birdie
  • 47
  • 6
  • Use a [`stringstream`](http://en.cppreference.com/w/cpp/io/basic_ostringstream) instead of `cout`, and either pass the stream as a function parameter, or have the function return the result of calling `str()` on the stream. – user657267 Aug 17 '15 at 02:59

1 Answers1

2

Firstly, include the lib:

#include <sstream>

Then, instantiate a std::stringstream:

std::stringstream ss;

Replace cout<< to ss<<.

Finally, return the string stream as a string.

return ss.str();
csguth
  • 569
  • 3
  • 18