Question

Here is a program written in c++ from this Write code to convert given number into words (eg 1234 as input should output one thousand two hundred and thirty four) question I modified to convert number into words.

In my program, instead of repeated using cout, I created an ostream object out and placed the return value in out.

Here is the program

#include<iostream>
using namespace std;
ostream & expand(int);
int main()
{
    int num;
    cin>>num;

   cout<<expand(num);
}
ostream & expand(int value)
{
ostream   &out;
out<<"";
    const char * const ones[21] = {"zero", "one", "two", "three","four","five","six","seven",
    "eight","nine","ten", "eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen",
    "eighteen","nineteen"};
    const char * const tens[10] = {"", "ten", "twenty", "thirty","forty","fifty","sixty","seventy",
    "eighty","ninety"};

    if(value<0)
    {
        out<<"minus "<<expand(-value);

    }
else if(value>=1000000){
out<<expand(value/1000000)<<" million";
if(value % 1000000)
{
out<<" "<<expand(value % 1000000);
}

}
    else if(value>=1000)
    {
        out<<expand(value/1000)<<" thousand";

        if(value % 1000)
        {
            if(value % 1000 < 100)
            {
                out << " and";
            }
            out << "  " <<expand(value % 1000);
        }
    }
    else if(value >= 100)
    {
        out<<expand(value / 100)<<" hundred";

        if(value % 100)
        {
            out << " and "<<expand (value % 100);
        }
    }
    else if(value >= 20)
    {
        out << tens[value / 10];
        if(value % 10)
        {
            out << " " << expand(value % 10);
        }
    }
    else
    {
        out << ones[value];
    }
    return &out;
}

However, I get the following error while compiling.

In function 'std::ostream& expand(int)':
Line 13: error: 'out' declared as reference but not initialized
compilation terminated due to -Wfatal-errors.

Please help me.

I tried setting ostream &out=cout; and at the end return out. But I get the following result for cout<<expand(111234).

one0x8050884 hundredeleven and 0x80508840x8050884 thousandtwo0x8050884 hundredthirtyfour 0x8050884 and 0x8050884 0x80508840x8050884

Was it helpful?

Solution 2

ostream   &out; // Error

You must initialize out by an object. Because references always must refer to something.

ostream   &out = cout;

OTHER TIPS

The problem is here:

ostream   &out;

As the compiler is telling you, references must be initialized. They cannot be default-constructed. Per Paragraph 8.3.5/1 of the C++11 Standard:

A variable declared to be a T& or T&&, that is, “reference to type T” (8.3.2), shall be initialized by an object, or function, of type T or by an object that can be converted into a T.

If you meant to bind out to the standard output, then you could do this:

ostream& out = std::cout;

Alternatively, you could make out a parameter of your function, binding to std::cout by default:

ostream& expand(int value, ostream& out = std::cout)
{
    out << "";
    // ...
}

This way, clients can call expand() and let it output to a specified stream (e.g. an std::ostringstream or an std::ofstream), or to the standard output if no stream is specified.

If you want to chain output like that, you should use the method given in Andy's answer. Currently, the system is trying to output your ostream pointer as a value, it's not able to chain properly as you don't supply the stream as an input parameter.

#include<stdio.h>
#include<conio.h>

void two_dgt(int);
void three_dgt(int);   //Function Declarations
void four_dgt(int);
long int num,hd_place,th_place;
char single[10][6] = {"","one","two","three","four",
        "five","six","seven","eight","nine"};

char tens_multi[10][8] = {"","ten","twenty","thirty","forty",
        "fifty","sixty","seventy","eighty","ninety"};

char dual[10][10] = {"ten","eleven","twelve","thirteen","fourteen",
         "fifteen","sixteen","seventeen","eighteen","nineteen"};

void main()
{
   clrscr();
   printf("Enter the number upto 5 digits: ");
   scanf("%ld",&num);
   if(num == 0)
     printf("Zero");
   else
   {
       clrscr();
       printf("\n\nEntered number: %ld",num);
       printf("\nIn words:       ");
       if(num < 10)
          printf("%s",single[num]); //use to print single digit number
       else if(num < 100)
          two_dgt(num);          //call a function to print two digits
       else if(num < 1000)
          three_dgt(num);      //call a function to print three digits
       else if(num < 10000)
          four_dgt(num);        //call a function to print four digits
       else if(num < 100000)
       {
          th_place = num/1000;
          two_dgt(th_place);
          printf(" thousand ");
          num = num - (th_place*1000);
          hd_place = num/100;
          if(hd_place == 0)
              two_dgt(num);
          else
              three_dgt(num);
       }
       else if(num > 99999)
       {
          clrscr();
          printf("\nError: number range should be upto 5 digits");
        }     
    }
    getch();

}

void two_dgt(int num)
{
if(num%10 == 0)
  printf("%s",tens_multi[num/10]);
else
{
  if(num/10 == 1)
     printf("%s",dual[num%10]);
  else
     printf("%s %s",tens_multi[num/10],single[num%10]);
}
}

void three_dgt(int num)
{
 hd_place = num/100;
 printf("%s hundred ",single[hd_place]);
 num = num - (hd_place*100);
 two_dgt(num);
}

void four_dgt(int num)
{
  th_place = num/1000;
  printf("%s thousand ",single[th_place]);
  num = num - (th_place*1000);
  hd_place = num/100;
  if(hd_place == 0)
     two_dgt(num);
  else
     three_dgt(num);
 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top