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

Dani AI

Generated

Two separate problems are visible in the thread. The first (pointed out by ) is undefined behavior from appending into an uninitialized C buffer. The second is a logic/order bug in the digit processing: the tens test looks for exact remainder values instead of the tens digit, and the hundreds logic appends "hundred" without producing the word for the hundreds digit first. That explains why an input like 128 produced only the ones-word.

Fix plan (practical, in order)

  • Prefer using C++ strings (std::string) and append operations instead of raw char* and strcat. It removes manual initialization and bounds-check headaches.
  • Extract digits explicitly: hundreds = abs(n) / 100; remainder = abs(n) % 100; tens = remainder / 10; ones = remainder % 10. Build the output in this order: hundreds (append "<one-word> hundred" if hundreds>0), then tens (handle the teen case when tens==1), otherwise append the tens-word and then the ones-word.
  • Treat zero and negatives as special cases. Always ensure proper spacing when concatenating (e.g., append a space before "hundred" or between words).

Debugging tips and gotchas

  • Add temporary prints of the three computed digits to verify extraction before concatenation.
  • Test edge cases: 0, single digits, teens (10-19), clean tens (20,30,...), numbers like 101 and 115, the max/min of your target range.
  • Watch spelling and spacing (e.g., "forty" not "fourty"; add spaces between "five" and "hundred").

References and next step

  • For safe string handling see the std::string reference: std::string.
  • For how C strcat requires null-terminated source/destination see: strcat.

This complements 's diagnosis: initialize or avoid manual buffers, then fix digit extraction and the build order so the hundreds/tens/ones words appear in the right sequence.

Recommended Answers

All 3 Replies

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?

>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();
  }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.