Hey all

I'm trying to create a program that takes a predetermined number (monthly salary in this case) and rather than printing as a number it prints it as text. However when I input a number all I get is random stuff that makes no sence.

I will include the code and also a link to a screenshot of what I see.

Link to picture of the problem

#include <cstring>
#include <cstdlib>
#include <iostream>
#include <string.h>
using namespace std;

int main(int argc, char *argv[])
{
int inum;
cout <<"enter the number";
cin >> inum;
int iquo, irem;

iquo = inum / 100;
irem = inum % 100;
char *cdisp;
cdisp = new char[200];
if( inum < 0)
   {
       strcat(cdisp, "minus");
       inum = 0 - inum;
   }
if(irem >=10 && irem < 20)
{
         switch (irem)
         {
               case 11:
                     strcat(cdisp," eleven");

                     break;
                case 12:
                     strcat(cdisp," twelve");
                   
                     break;
                case 13:
                     strcat(cdisp," thirteen");
                    
                     break;
                case 14:
                     strcat(cdisp," fourteen");

                     break;
                case 15:
                     strcat(cdisp," fifteen");
                      
                     break;
                case 16:
                     strcat(cdisp," sixteen");
                     
                     break;
                case 17:
                     strcat(cdisp," seventeen");
                      
                     break;
                case 18:
                     strcat(cdisp," eighteen" );
                    
                     break;
                case 19:
                     strcat(cdisp," nineteen");
                      
                     break;
          }
   }
   if(irem >= 20)
   {
         switch (irem)
         {
               case 20:
                     strcat(cdisp," twenty");

                     break;
                case 30:
                    strcat(cdisp," thirty");
                   
                     break;
                case 40:
                     strcat(cdisp," fourty");
                    
                     break;
                case 50:
                     strcat(cdisp," fifty");

                     break;
                case 60:
                     strcat(cdisp," sixty");
                      
                     break;
                case 70:
                     strcat(cdisp," seventy");
                     
                     break;
                case 80:
                     strcat(cdisp," eighty");
                      
                     break;
                case 90:
                     strcat(cdisp,"ninety" );
                    
                     break;
            }
		 
            irem = irem %10;
            switch (irem)
            {
               case 1:
                    strcat(cdisp," one");

                     break;
                case 2:
                    strcat(cdisp," two");
                   
                     break;
                case 3:
                     strcat(cdisp," three");
                    
                     break;
                case 4:
                     strcat(cdisp," four");

                     break;
                case 5:
                     strcat(cdisp," five");
                      
                     break;
                case 6:
                     strcat(cdisp," six");
                     
                     break;
                case 7:
                     strcat(cdisp," seven");
                      
                     break;
                case 8:
                     strcat(cdisp," eight" );
                    
                     break;
                case 9:
                     strcat(cdisp," nine" );
                    
                     break;
            }
            
      }
      irem = inum%1000;
      if( irem >= 100)
      {
          int itemp = irem/100; 
          switch (itemp)
            {
                 case 1:
                     strcat(cdisp," hundred");

                     break;
                case 2:
                    strcat(cdisp," twohundred");
                   
                     break;
                case 3:
                     strcat(cdisp," threehundred");
                    
                     break;
                case 4:
                     strcat(cdisp," fourhundred");

                     break;
                case 5:
                     strcat(cdisp," fifvehundred");
                      
                     break;
                case 6:
                     strcat(cdisp," sixhundred");
                     
                     break;
                case 7:
                     strcat(cdisp," sevenhundred");
                      
                     break;
                case 8:
                     strcat(cdisp," eighthundred" );
                    
                     break;
                case 9:
                     strcat(cdisp," ninehundred" );
                    
                     break;
                }
       }
      
  strcat(cdisp,"\n");
  cout<<"the word is " << cdisp;
   
 
 
  system("PAUSE");
  return EXIT_SUCCESS;
}

Link to picture of the problem

strcat expects a valid C-style string (ie. an array of char terminated by '\0'). In your case, you failed to meet the requirement that the array is terminated by '\0', so you've invoked undefined behavior. This should help:

char *cdisp;
cdisp = new char[200];

// Add the following line
cdisp[0] = '\0';

if( inum < 0)

Your algorithm is mixed up too. Expect the words to be jumbled.

strcat expects a valid C-style string (ie. an array of char terminated by '\0'). In your case, you failed to meet the requirement that the array is terminated by '\0', so you've invoked undefined behavior. This should help:

char *cdisp;
cdisp = new char[200];

// Add the following line
cdisp[0] = '\0';

if( inum < 0)

Your algorithm is mixed up too. Expect the words to be jumbled.

Thanks that helped me with that problem, however a new one has presented itself.

when I enter a number to be converted i.e. 128 only the eight is displayed as text. Any ideas?

Edited 6 Years Ago by XxWARGASMxX: Added content

>128 only the eight is displayed as text. Any ideas?
I already told you that your algorithm is broken. It'[s pretty easy to brute force the algorithm if you're limited to a range of [-999, 999], you just have to be careful about the order in which you process digits.

Here's an example to give you an idea of how it might be done:

#include <iostream>
#include <sstream>
#include <string>
#include <cstdlib>

namespace {
  const std::string ones[] = {
    "",     "one", "two",   "three", "four",
    "five", "six", "seven", "eight", "nine"
  };

  const std::string teens[] = {
    "ten",     "eleven",  "twelve",    "thirteen", "fourteen",
    "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"
  };

  const std::string tens[] = {
    "",      "",      "twenty",  "thirty", "forty",
    "fifty", "sixty", "seventy", "eighty", "ninety"
  };

  std::string int_to_word ( int value )
  {
    std::ostringstream out;
    bool neg = value < 0;

    value = std::abs ( value );

    int digits[3] = {0};

    for ( int i = 2; i >= 0 && value != 0; i-- ) {
      digits[i] = value % 10;
      value /= 10;
    }

    if ( neg )
      out<<"negative ";

    if ( digits[0] == 0 && digits[1] == 0 && digits[2] == 0 )
      out<<"zero";
    else {
      if ( digits[0] != 0 )
        out<< ones[digits[0]] <<" hundred ";

      if ( digits[1] != 0 ) {
        if ( digits[1] == 1 )
          out<< teens[digits[2]];
        else {
          out<< tens[digits[1]];

          if ( digits[2] != 0 )
            out<<' '<< ones[digits[2]];
        }
      }
      else
        out<< ones[digits[2]];
    }

    return out.str();
  }
}

int main()
{
  for ( int i = -999; i < 1000; i++ ) {
    std::cout<< int_to_word ( i ) <<'\n';
    std::cin.get();
  }
}
This question has already been answered. Start a new discussion instead.