delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/09/19/15:24:49

From: "Kilgore Trout" <kevind AT netcore DOT ca>
Newsgroups: comp.os.msdos.djgpp
Subject: Problem with strcat
Lines: 202
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 5.00.2314.1300
X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2314.1300
X-Original-NNTP-Posting-Host: c-lvii.wincom.net
Message-ID: <37e52e96@news.wincom.net>
Date: Sun, 19 Sep 1999 18:29:10 GMT
NNTP-Posting-Host: news.wincom.net
NNTP-Posting-Date: Sun, 19 Sep 1999 14:29:10 EST
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Hello,  I am working on a school assignment and have run into a small
problem with djgpp.  The purpose of the program is to take a number less
than 80 typed out in words (such as 'twenty two'), convert it to and
integer, add seventeen, and convert the integer back into words.  I run into
a problem if I run the program and input a number greater than or equal to
'seventy four'.  I have tried compiling with two other compilers (lcc and
cc) and the program has worked fine.  I think that the problem may be in the
line
  strcat(output_string, integer_to_words(number));
Any help is greatly appreciated.

Thanks,
Kevin

/*Start Of Program*/
#include <stdio.h>
#include <string.h>

int words_to_integer(char *);
char *integer_to_words(int);

int main(void)
{
 char  input_string[80], first_word[40], second_word[40];
 char *output_string;
 int number, word_count;

 printf("Type a number which is greater than zero and less than eighty:\n");
 fgets(input_string, 80, stdin);
 word_count = sscanf(input_string, "%s%s", first_word, second_word);

 number = words_to_integer(first_word);
 if(word_count == 2) number += words_to_integer(second_word);
 number += 17;

 output_string = integer_to_words(number);
 if(number > 19 && (number % 10) != 0) {
  number %= 10;
  strcat(output_string, " ");
  /*I think the problem is in the following line */
  strcat(output_string, integer_to_words(number));
 }

 printf("The number plus seventeen is ");
 printf("%s\n", output_string);

 return 0;
}

int words_to_integer(char *word)
{
 if( strcmp(word, "one") == 0 )
  return 1;
 else if( strcmp(word, "two") == 0 )
  return 2;
 else if( strcmp(word, "three") == 0 )
  return 3;
 else if( strcmp(word, "four") == 0 )
  return 4;
 else if( strcmp(word, "five") == 0 )
  return 5;
 else if( strcmp(word, "six") == 0 )
  return 6;
 else if( strcmp(word, "seven") == 0 )
  return 7;
 else if( strcmp(word, "eight") == 0 )
  return 8;
 else if( strcmp(word, "nine") == 0 )
  return 9;
 else if( strcmp(word, "ten") == 0 )
  return 10;
 else if( strcmp(word, "eleven") == 0 )
  return 11;
 else if( strcmp(word, "twelve") == 0 )
  return 12;
 else if( strcmp(word, "thirteen") == 0 )
  return 13;
 else if( strcmp(word, "fourteen") == 0 )
  return 14;
 else if( strcmp(word, "fifteen") == 0 )
  return 15;
 else if( strcmp(word, "sixteen") == 0 )
  return 16;
 else if( strcmp(word, "seventeen") == 0 )
  return 17;
 else if( strcmp(word, "eighteen") == 0 )
  return 18;
 else if( strcmp(word, "nineteen") == 0 )
  return 19;
 else if( strcmp(word, "twenty") == 0 )
  return 20;
 else if( strcmp(word, "thirty") == 0 )
  return 30;
 else if( strcmp(word, "forty") == 0 )
  return 40;
 else if( strcmp(word, "fifty") == 0 )
  return 50;
 else if( strcmp(word, "sixty") == 0 )
  return 60;
 else if( strcmp(word, "seventy") == 0 )
  return 70;

 return 0;
}

char *integer_to_words(int num)
{
 char *output;

 if(num <= 19) {
  switch(num) {
   case 1:
    output = "one";
    break;
   case 2:
    output = "two";
    break;
   case 3:
    output = "three";
    break;
   case 4:
    output = "four";
    break;
   case 5:
    output = "five";
    break;
   case 6:
    output = "six";
    break;
   case 7:
    output = "seven";
    break;
   case 8:
    output = "eight";
    break;
   case 9:
    output = "nine";
    break;
   case 10:
    output = "ten";
    break;
   case 11:
    output = "eleven";
    break;
   case 12:
    output = "twelve";
    break;
   case 13:
    output = "thirteen";
    break;
   case 14:
    output = "fourteen";
    break;
   case 15:
    output = "fifteen";
    break;
   case 16:
    output = "sixteen";
    break;
   case 17:
    output = "seventeen";
    break;
   case 18:
    output = "eighteen";
    break;
   case 19:
    output = "nineteen";
    break;
  }
 }
 else {
  switch(num / 10) {
   case 2:
    output = "twenty";
    break;
   case 3:
    output = "thirty";
    break;
   case 4:
    output = "forty";
    break;
   case 5:
    output = "fifty";
    break;
   case 6:
    output = "sixty";
    break;
   case 7:
    output = "seventy";
    break;
   case 8:
    output = "eighty";
    break;
   case 9:
    output = "ninety";
  }
 }
 return output;
}



- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019