Like Tree2Likes

String table and printing english equivalent

This is a discussion on String table and printing english equivalent within the C Programming forums, part of the General Programming Boards category; I have a question that is still boggling my mind. Since my lecturer never gives me the answer. So, I ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    6

    String table and printing english equivalent

    I have a question that is still boggling my mind. Since my lecturer never gives me the answer. So, I decided to ask the board about this. Sorry if the post is a bit messy. Im quite new. The question goes like this.


    "Write a C program that creates a string table containing the English words for the digits 0 through 9. Using this table, allow the user to enter a digit and then have your program display the word equivalent. The program has to allow the user to enter again the digit if he enters something other than digits 0 through 9."

    I have created a code. I don't know if I answered the question. Can anyone rectify me if I didn't?

    Code:
    #include <stdio.h>
    
    void num_to_english(char* zero, char* one, char* two, char* three, char* four, 
                        char* five,char* six, char* seven, char* eight, char* nine);
    
    void main()
    {
         char a,b,c,d,e,f,g,h,j,k;
         num_to_english(&a,&b,&c,&d,&e,&f,&g,&h,&j,&k);
         getchar();
    }
    
    void num_to_english(char* zero,char* one, char* two, char* three, char* four, 
                        char* five, char* six, char* seven, char* eight, char* nine)
    {    
        int number,i;
        zero = "zero"; one = "one"; two = "two"; three = "three"; four = " four";
        five = "five"; six = "six"; seven = "seven"; eight = "eight"; nine = "nine";
        printf("Please insert a number from 0-9 : ");
        scanf("%d", &number);
        printf("The %d equivalent in english is : ",number);
        if(number>=0&&number<=9)
         {switch(number)
            {case 0:
             printf("%s", zero);
             break;
             case 1:
             printf("%s", one);
             break;
             case 2:
             printf("%s", two);
             break;
             case 3:
             printf("%s", three);
             break;
             case 4:
             printf("%s", four);
             break;
             case 5:
             printf("%s", five);
             break;
             case 6:
             printf("%s", six);
             break;
             case 7:
             printf("%s", seven);
             break;
             case 8:
             printf("%s", eight);
             break;
             default:
             printf("%s", nine);}}
        else
             {printf("is out of range. Please re-enter.\n\n");
             main();}
    }
    Thank you so much!!

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    No you didn't answer the question and I don't even think that code will compile. Start reading through the C tutorials on this site. Once completed, sit down and think about your problem. Then write up how you would do it. Then code it and test it. Then if you have a problem come back and post.


    EDIT: This code is so broken it isn't even worth pointing out the errors. You essentially came here and asked us to do your homework for you.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,401
    My observations are:
    • The program is too complicated for the task.
    • main should be declared as returning int, not void.
    • You should use a loop instead of recursively calling main.


    I might have written the program along these lines:
    Code:
    #include <stdio.h>
    
    int print_english_word(int num);
    
    int main(void)
    {
        int num;
        do
        {
            /* Request for input and read into num */
            /* ... */
        }
        while (!print_english_word(num))
        return 0;
    }
    
    /* Return 1 if English word equivalent of num in range [0, 9] was printed,
     * otherwise return 0.
    int print_english_word(int num)
    {
        switch (num)
        {
        case 0:
            /* ... */
            break;
        /* ... */
        default:
            return 0;
        }
        return 1;
    }
    EDIT:
    Quote Originally Posted by AndrewHunter
    No you didn't answer the question and I don't even think that code will compile.
    On the contrary, I think the program does answer the question, just that contains too much that is unnecessary. A quick check shows that the program will compile once the void main is fixed to int main, so I posit that it will probably compile on a compiler that accepts void main. I understand the gist of the idea behind this implementation, so saying that jimmyskabez did not do his/her homework is unfair and shows that you did not do your homework in looking at the program presented.
    Last edited by laserlight; 08-12-2011 at 10:33 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Did you miss where he stored "zero" into char a and the like?
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,401
    Quote Originally Posted by AndrewHunter
    Did you miss where he stored "zero" into char a and the like?
    At no point is "zero" stored into a from main.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by laserlight View Post
    At no point is "zero" stored into a from main.
    Haha...I am being stupid again. Thanks Laser.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by jimmyskabez View Post
    I have a question that is still boggling my mind. Since my lecturer never gives me the answer. So, I decided to ask the board about this. Sorry if the post is a bit messy. Im quite new. The question goes like this.

    "Write a C program that creates a string table containing the English words for the digits 0 through 9. Using this table, allow the user to enter a digit and then have your program display the word equivalent. The program has to allow the user to enter again the digit if he enters something other than digits 0 through 9."

    I have created a code. I don't know if I answered the question. Can anyone rectify me if I didn't?
    In deference to my esteemed colleques... your code may give you the right display, but it's not very good at the job...

    1) The correct form of main() in this case is int main (void) and it returns an integer error value to the OS on exit (usually 0).

    2) Never, ever, call main() recursively .. and especially not from within a function!

    3) Moving 90% of your code out of main() does not a good function make. Functions should do one task. In your case, the best function would take the integer value of your user's input (which should be in main()) and print out the word corresponding to the number.

    4) Passing values into a function is NOT how you create variables for use inside a function. Parameters to functions should carry needed data into a function and you should never create more parameters than you need... in your case, following #3, one parameter would suffice...

    5) There is no need --or reason-- to assign 10 strings when you could just as easily use printf() to print the actual word in your switch/case statement.

    Aside from this, there is a far more efficient way of doing this... Have you covered arrays in your course yet? An experienced programmer could probably do this in 20 lines or less...

  8. #8
    Registered User
    Join Date
    Aug 2011
    Posts
    6
    Thanks so much guys for the replies. I'm still going through the tutorials to re-edit my codes. Still a new programmer. And yes, my compiler accepts void main(), that's why I use it. Some of my lecturers even use it. But, now I know its wrong so I'll change it. Thanks! Still editing. If I come up with another solution. Which may take time, I'll post it back here later.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    He needs the string table, however:

    Code:
    Write a C program that creates a string table containing the English words for
    the digits 0 through 9. Using this table, allow the user to enter a digit and then have 
    your program display the word equivalent.
    laserlight likes this.

  10. #10
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    table == array ?
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  11. #11
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Creating a lookup table would be a simple solution for this, yes. Probably the way I would have solved the problem statement.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  12. #12
    Registered User
    Join Date
    Aug 2011
    Posts
    6
    I did an editing to my codes. But, still. I don't think I answered the string table question. It answers all the other parts. Just the part using string table. I just don't know how.
    Code:
    #include <stdio.h>
    
    int print_english_word(int num);
    
    int main()
    {
        int num;
        do
        {
            printf("Please insert a number: ");
            scanf("%d", &num);
            if(num>=0&&num<=9){
            printf("\n%d is ", num);
            print_english_word(num);
            getchar();
            getchar();
            return 0;}
            else
            printf("is out of range!!\n");
            getchar();
        }
        while (!print_english_word(num));
        return 0;
    }
    
    
    int print_english_word(int num)
    {
            switch(num)
            {case 0:
             printf("zero");
             break;
             case 1:
             printf("one");
             break;
             case 2:
             printf("two");
             break;
             case 3:
             printf("three");
             break;
             case 4:
             printf("four");
             break;
             case 5:
             printf("five");
             break;
             case 6:
             printf("six");
             break;
             case 7:
             printf("seven");
             break;
             case 8:
             printf("eight");
             break;
             case 9:
             printf("nine");
             default:
             printf("Try again\n");}
        return 0;
    }
    Sorry about the lots of getchar's. The output just won't pause. I dont want to burden with system pause so I put getchars. Hope to get replies soon.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,401
    Quote Originally Posted by jimmyskabez
    I don't think I answered the string table question.
    Do you know how to declare and initialise an array?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Aug 2011
    Posts
    6
    Yes.

  15. #15
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Then why don't you make an array, e.g. a string table like your assignment asked for?, e.g.

    Code:
    char * stringTable[10];
    And then fill in your array with the values you want.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing a table-like text
    By C_ntua in forum C# Programming
    Replies: 2
    Last Post: 11-16-2010, 03:35 PM
  2. Storing and printing an array table
    By jaja009 in forum C Programming
    Replies: 3
    Last Post: 03-12-2010, 04:13 AM
  3. Printing a colored table , how to ?
    By GSalah in forum C++ Programming
    Replies: 3
    Last Post: 11-17-2006, 06:59 AM
  4. Printing out a data table in different sequences
    By TheDudeAbides in forum C Programming
    Replies: 2
    Last Post: 07-20-2003, 12:06 AM
  5. Printing characters not in the ASCII table
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 01-21-2002, 12:47 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21