hi this is a course work question and i have tried multiple methods to get it to work so if you can show me where i'm going wrong i would greatly appreciate it.
i have to read a 4 character input and create each of the numbers into a sort of ascii version of it you will see below.
my current output doesn't generate a usable output but if you take away the RETURN j from each of the if statements and the printf(j) at the bottom it produces the correct output but vertically i need them horizontal. thats why i was trying to put them into another array to output them side by side.

/*
Algorithm
scan for input
when user enters characters read characters into variable
check if string is alpha  if so  display message
check the length of the string if greater than 4 characters display message
check if  numeric characters 
loop through checking which characters have been entered
return function output to variable
display contents of variable 
*/


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

int main()
{
char numberString[5];
int i = 0;
int j[5];

scanf("%s", numberString);

if(isalpha(numberString))
{
printf("i can only take numeric values");
}
   else if(strlen(numberString) > 4)
   {
   printf("i can only accept 4 characters sorry");
   }
      else if(isdigit(numberString))
      {
      for ( i = 0; i < 4; ++i )
      {
          if ( numberString[i] == '0' )
          {
                  printf("  000\n");
                  printf(" 0   0\n");
                  printf("0     0\n");
                  printf("0     0\n");
                  printf("0     0\n");
                  printf(" 0   0\n");
                  printf("  000");
                  return j;
          }
          else if ( numberString[i] == '1')
          {
                  printf(" 0\n");
                  printf("00\n");
                  printf(" 0\n");
                  printf(" 0\n");
                  printf(" 0\n");
                  printf("000");
                  return j;
          }
            else if ( numberString[i] == '2')
          {
                  printf(" 000\n");
                  printf("0   0\n");
                  printf("   0\n");
                  printf("  0\n");
                  printf(" 0\n");
                  printf("0\n");
                  printf("0000");
                  return j;
          }  
          else if ( numberString[i] == '3')
          {
                  printf(" 000\n");
                  printf("0   0\n");
                  printf("   0\n");
                  printf(" 00\n");
                  printf("   0\n");
                  printf("0   0\n");
                  printf(" 000");
                  return j;
          }
            else if ( numberString[i] == '4')
          {
                  printf("0\n");
                  printf("0\n");
                  printf("0\n");
                  printf("0\n");
                  printf("0  0\n");
                  printf("000000\n");
                  printf("   0");
                  return j;
          }
            else if ( numberString[i] == '5')
          {
                  printf("000000\n");
                  printf("0\n");
                  printf("0\n");
                  printf("0000\n");
                  printf("    0\n");
                  printf("    0\n");
                  printf("0000");
                  return j;
          }
            else if ( numberString[i] == '6')
          {
                  printf("000000\n");
                  printf("0\n");
                  printf("0\n");
                  printf("000000\n");
                  printf("0    0\n");
                  printf("0    0\n");
                  printf("000000");
                  return j;
          }
            else if ( numberString[i] == '7')
          {
                  printf("000000\n");
                  printf("    0\n");
                  printf("   0\n");
                  printf("  0\n");
                  printf(" 0\n");
                  printf("0\n");
                  printf("0");
                  return j;
          }
            else if ( numberString[i] == '8')
          {
                  printf("  00  \n");
                  printf(" 0  0\n");
                  printf("0    0\n");
                  printf(" 0000  \n");
                  printf("0    0\n");
                  printf(" 0  0\n");
                  printf("  00");
                  return j;
          }
            else if ( numberString[i] == '9')
          {
                  printf("00000\n");
                  printf("0   0\n");
                  printf("0   0\n");
                  printf("00000\n");
                  printf("    0\n");
                  printf("    0\n");
                  printf("    0");
                  return j;
          }
      }
      }
                 printf(j);
}

Recommended Answers

All 4 Replies

I can think of two ways to do this. (Also, get rid of the return j. It returns from the main function and exits the program. Get rid of the printf(j) at the bottom. Why do you have it?)

First: using arrays.
Since you're using characters 7 lines tall, create 7 arrays each about 80 bytes wide. Actually, I would take the width of your longest character (about 5), add 1 for a space (5+1=6), multiply by 3 since you have 3 characters (6*3=18), and add 1 for a trailing 0 (18+1=19). So you could create 7 arrays of 19 characters. Or a 7x19 matrix. Then, instead of doing printf for every character, use strcat. THEN, when you're finished strcat-ing all your characters together, do a printf for each array.

Second. Use a console cursor moving function. If you're using Windows, check out http://www.daniweb.com/software-development/cpp/threads/22498

Otherwise, in Linux (which lets you use ANSI), use something like this:

char escape=27; int y= 1;int x=7; 
//{1,7} will park you at the upper left corner of the second character
printf("%c[%d;%dH",escape,y,x);

I can think of two ways to do this. (Also, get rid of the return j. It returns from the main function and exits the program. Get rid of the printf(j) at the bottom. Why do you have it?)

There's a third possibility that makes even more sense...

First: using arrays.
Since you're using characters 7 lines tall, create 7 arrays each about 80 bytes wide. Actually, I would take the width of your longest character (about 5), add 1 for a space (5+1=6), multiply by 3 since you have 3 characters (6*3=18), and add 1 for a trailing 0 (18+1=19). So you could create 7 arrays of 19 characters. Or a 7x19 matrix. Then, instead of doing printf for every character, use strcat. THEN, when you're finished strcat-ing all your characters together, do a printf for each array.

There's too much computation going on for such an easy problem.

Second. Use a console cursor moving function. If you're using Windows, check out http://www.daniweb.com/software-development/cpp/threads/22498

Otherwise, in Linux (which lets you use ANSI), use something like this:

char escape=27; int y= 1;int x=7; 
//{1,7} will park you at the upper left corner of the second character
printf("%c[%d;%dH",escape,y,x);

Completely non-portable and non-Standard therefore not recommended.

Make a 2D array for each letter. Rather than

printf(" 000\n");
printf("0 0\n");
printf(" 0\n");
printf(" 0\n");
printf(" 0\n");
printf("0\n");
printf("0000");

make an array for each letter:

char *letter1[] = { " 000  ",
                    "0 0   ",
                    " 0    ",
                    " 0    ",
                    " 0    ",
                    "0     ",
                    "0000  ",
                   };

Now you output
1) the first line for each letter printf("%s", letter1[line]) 2) newline
3) second line + newline
4) third line + newline
etc.

An extension to this is a little harder to visualize but easier to implement code-wise. Make letter a 3D ( **letter[] ) array and the inputs can be converted (or simply used) as the first index to get to the proper letter: printf("%s", letter[inputval][line])

thank you for the pointers guys ive put the numbers into arrays but im getting an error
Line 23 ERROR: excess elements in char array initializer

is that because the array is too big.

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

int main()
{
char numberString[5];
int i = 0;
char j[]={"  000",
          " 0   0",
          "0     0",
          "0     0",
          "0     0",
          " 0   0",
          "  000",};

char k[]= {" 0",
           "00",
           " 0",
           " 0",
           " 0",
           "000",};

char l[]={" 000",
          "0   0",
          "   0",
          "  0",
          " 0",
          "0",
          "0000",} ;

char m[]={" 000",
          "0   0",
          "   0",
          " 00",
          "   0",
          "0   0",
          " 000",};

char n[]={"0",
          "0",
          "0",
          "0",
          "0  0",
          "000000",
          "   0",};

char o[]={"000000",
          "0",
          "0",
          "0000",
          "    0",
          "    0",
          "0000",};

char p[]={"000000",
          "0",
          "0",
          "000000",
          "0    0",
          "0    0",
          "000000",};

char q[]={"000000",
          "    0",
          "   0",
          "  0",
          " 0",
          "0",
          "0",};

char r[]={" 00" ,
         " 0  0",
         "0    0",
         " 0000 ",
         "0    0",
         " 0  0",
         "  00 ",};

char s[]={"00000",
          "0   0",
          "0   0",
          "00000",
          "    0",
          "    0",
          "    0",};

scanf("%s", numberString);

if(isalpha(numberString))
{
printf("only numeric characters please");
}
else if( strlen(numberString) < 4 || strlen(numberString) >= 5)
{
printf("only 4 characters please");
}
      for ( i = 0; i < 4; ++i )
      {
          if ( numberString[i] == '0' )
          {
              printf("%s\n", j[0]);   

          }
          else if ( numberString[i] == '1')
          {
                 printf("%s\n", k);


          }
            else if ( numberString[i] == '2')
          {
                  printf("%s\n", l);


          } 
          else if ( numberString[i] == '3')
          {
                  printf("%s\n", m);


          }
            else if ( numberString[i] == '4')
          {
                  printf("%s\n", n);

          }
            else if ( numberString[i] == '5')
          {
               printf("%s\n", o);


          }
            else if ( numberString[i] == '6')
          {
             printf("%s\n", p);

          }
            else if ( numberString[i] == '7')
          {
             printf("%s\n", q);

          }
            else if ( numberString[i] == '8')
          {
             printf("%s\n", r);

          }
            else if ( numberString[i] == '9')
          {
             printf("%s\n", s);
          }
      }

}

OK, let's back up and explain a few things.
When you create an array with, let's say, char x[]="I'm an array!"; C sees this as an array of characters.
Now let's look at this example: char y[]={'I',' ','a', 'm', ' ', 't', 'o', 'o', '!', 0}; //Note the trailing 0. That signals the end of the string.
Both are arrays of characters. And they are single-dimensional.
Let's look at this. char z[][]={"I'm ", "an ", "array ", "of ", "arrays!"};
This is a two dimensional array. Notice that there are two []'s behind the z.

Let's go back to your code.

You have multidimensional arrays instead of single dimension arrays. You should change your array initialization from char j[]= to either char j[][]= or char *j[]=.
Also, you have trailing commas in your {}'s. Do a search for ,}; and change it to };
The compiler may see the commas and be expecting more data. Or not. Different compilers are different.
You'll be all set.

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.