Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Basic Arrays 1

Status
Not open for further replies.

TFfan

Programmer
Jan 18, 2002
192
CA
I get a bus error. It's clear my knowledge of arrays is at teh most basic level, but I'm at wit's end. Any help is appreciated.

#include <stdio.h>
#include <string.h>

main(){

char onetoteens[][19] = {&quot;one &quot;, &quot;two &quot;, &quot;three &quot;, &quot;four &quot;, &quot;five &quot;, &quot;six &quot;, &quot;seven &quot;, &quot;eight &quot;, &quot;nine &quot;, &quot;ten &quot;, &quot;eleven &quot;, &quot;twelve &quot;, &quot;thirteen &quot;, &quot;fourteen &quot;, &quot;fifteen &quot;, &quot;sixteen &quot;, &quot;seventeen &quot;, &quot;eighteen &quot;, &quot;nineteen &quot;};

char tys[][9] = {&quot; twenty&quot;, &quot; thirty&quot;, &quot; forty&quot;, &quot; fifty&quot;, &quot; sixty&quot;, &quot; seventy&quot;, &quot; eighty&quot;, &quot; ninety&quot;};

char english[50][50];

int num, first, second, third;

printf (&quot;Enter a three digit number: &quot;);
scanf (&quot;%d&quot;, &num);

third=num%10;
second=num/10%10;
first=num/100;

strcat (english[50], onetoteens[first-1]);
strcat (english[50], tys[second-2]);
strcat (english[50], onetoteens[third-1]);


printf (&quot;%s&quot;, english);

/*printf(&quot;The number entered is: %shundred%s %s&quot;, onetoteens[first-1], tys[second-2], onetoteens[third-1]);*/

}
 
1) With 2D arrays, the 2nd dimension is the size of the string: the first is the size of the array.
2) Arrays start at 0 so english[50] is out of range.

Try this: I've made some corrections to your code but I haven't tried running it.


main(){

char* onetoteens[] = {&quot;one &quot;, &quot;two &quot;, &quot;three &quot;, &quot;four &quot;, &quot;five &quot;, &quot;six &quot;, &quot;seven &quot;, &quot;eight &quot;, &quot;nine &quot;, &quot;ten &quot;, &quot;eleven &quot;, &quot;twelve &quot;, &quot;thirteen &quot;, &quot;fourteen &quot;, &quot;fifteen &quot;, &quot;sixteen &quot;, &quot;seventeen &quot;, &quot;eighteen &quot;, &quot;nineteen &quot;};

char* tys[] = {&quot; twenty&quot;, &quot; thirty&quot;, &quot; forty&quot;, &quot; fifty&quot;, &quot; sixty&quot;, &quot; seventy&quot;, &quot; eighty&quot;, &quot; ninety&quot;};

char english[50];

int num, first, second, third;

printf (&quot;Enter a three digit number: &quot;);
scanf (&quot;%d&quot;, &num);

third=num%10;
second=num/10%10;
first=num/100;

strcpy (english, onetoteens[first-1]);
strcat (english, tys[second-2]);
strcat (english, onetoteens[third-1]);


printf (&quot;%s\n&quot;, english);

/*printf(&quot;The number entered is: %shundred%s %s&quot;, onetoteens[first-1], tys[second-2], onetoteens[third-1]);*/

}

 
Status
Not open for further replies.

Similar threads

Replies
1
Views
387
Replies
4
Views
322
Replies
2
Views
53
Replies
2
Views
107
Replies
1
Views
142

Part and Inventory Search

Sponsor

Back
Top