strcat problem

I

Ian Stanley

Hi,
Having not done any C programming for a while I am trying to get back into
it by converting an old java assignment into C.
The problem is I am getting a segmentation fault at runtime which I am
having trouble fixing(program below)
The idea of the program is to convert input(integers) into words eg:
# 101
returns
#one hundred one
I have found the problem is with the strcat of the strings & literals --
tried redef
strcat to allow for enough memory--
#define STRCAT(d,s) d=( char *)malloc(d, strlen(d)+strlen(s)+1);strcat(d,s)
then replaced strcat with STRCAT but end up with more errors? Also tried
using a buffer with enough memory and strcat to that - doesn't seem to work
either.
Not sure how to fix it from here.
Any help appreciated, regards
Ian

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STRCAT(d,s) d=( char *)malloc(d, strlen(d)+strlen(s)+1);strcat(d,s)
char* convertLessThanOneThousand(int number);
char* convert(int number);
static int num;
static char *numNames[] = {
"",
" one",
" two",
" three",
" four",
" five",
" six",
" seven",
" eight",
" nine",
" ten",
" eleven",
" twelve",
" thirteen",
" fourteen",
" fifteen",
" sixteen",
" seventeen",
" eighteen",
" nineteen"
};

static char *tensNames[] = {
"",
" ten",
" twenty",
" thirty",
" fourty",
" fifty",
" sixty",
" seventy",
" eighty",
" ninety"
};

char *majorNames[] = {
"",
" thousand",
" million",
" billion",
" trillion",
" quadrillion",
" quintillion"
};
int main(){
printf("integer to convert: ");
scanf("%d", &num);
printf("converted to words: %s\n", convert(num));
return 0;
}

char* convertLessThanOneThousand(int number) {
char* soFar;
/*char buffer[10000];
char* soFar;
soFar = buffer;*/
char* hundred = "hundred";
if (number % 100 < 20){
soFar = numNames[number % 100];
number /= 100;
}
else {
soFar = numNames[number % 10];
number /= 10;

soFar = strcat(tensNames[number % 10], soFar);
number /= 10;
}
if (number == 0)
return soFar;
/*return numNames[number] + " hundred" + soFar;*/
return strcat(numNames[number], strcat(hundred, soFar));
}

char* convert(int number) {

char* zero = "zero";
if (number == 0) {
return zero; }

char* prefix = "";

/*if (number < 0) {
number = -number;
prefix = "negative";
}*/

char* soFar = "";
int place = 0;

do {
int n = number % 1000;
if (n != 0){
char* s = convertLessThanOneThousand(n);
/*soFar = s + majorNames[place] + soFar;*/
soFar = strcat(s, strcat(majorNames[place], soFar));
}
place++;
number /= 1000;
} while (number > 0);
/*return (prefix + soFar).trim();*/
return (strcat(prefix, soFar));
}
 
T

Tom Zych

Ian Stanley wrote [almost exactly what he wrote in
alt.comp.lang.learn.c-c++]

Did you read my reply in the other group? You're using strcat() to
append to string literals. It doesn't work. You can't change
literals.

BTW, it's "forty", not "fourty".
 
J

Joe Wright

Tom said:
Ian Stanley wrote [almost exactly what he wrote in
alt.comp.lang.learn.c-c++]

Did you read my reply in the other group? You're using strcat() to
append to string literals. It doesn't work. You can't change
literals.

BTW, it's "forty", not "fourty".
Yes, but it's only a spilling error. :)
 
I

Ian

Joe Wright said:
Tom said:
Ian Stanley wrote [almost exactly what he wrote in
alt.comp.lang.learn.c-c++]

Did you read my reply in the other group? You're using strcat() to
append to string literals. It doesn't work. You can't change
literals.

BTW, it's "forty", not "fourty".
Yes, but it's only a spilling error. :)

Thanks Tom,
Is there a way to fix it?
I have never used sprintf before- are there any good examples out there?
Thanks again for your reply
Ian
 
T

Tom Zych

Ian said:
Is there a way to fix it?
I have never used sprintf before- are there any good examples out there?

It's just like printf except the output goes into a string instead
of to stdout. So it's handy for building up a string from other
strings:

char a[] = "Like ";
char b[] = "this, ";
char c[] = "see?";
char result[30];

sprintf(result, "%s%s%s", a, b, c);
// check return value
 
I

Irrwahn Grausewitz

Tom Zych said:
Ian said:
Is there a way to fix it?
I have never used sprintf before- are there any good examples out there?

It's just like printf except the output goes into a string instead
of to stdout. So it's handy for building up a string from other
strings:

char a[] = "Like ";
char b[] = "this, ";
char c[] = "see?";
char result[30];

sprintf(result, "%s%s%s", a, b, c);
// check return value

My two cents: make sure that 'result' is big enough to hold the
resulting string to protect against buffer overrun.

In C99 one could use snprintf() which takes the size of the buffer
as an additional argument.

Irrwahn
--
do not write: void main(...)
do not use gets()
do not cast the return value of malloc()
do not fflush( stdin )
read the c.l.c-faq: http://www.eskimo.com/~scs/C-faq/top.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top