Thread: String table and printing english equivalent

  1. #16
    Registered User
    Join Date
    Aug 2011
    Posts
    6
    ok. Trying now. thanks!

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by AndrewHunter
    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.
    I suggest this instead:
    Code:
    const char *numerals[] = {"one", /* ... */};
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #18
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Yeah, good point. I thought about that but decided to be lazy with my example.
    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.

  4. #19
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Instead of
    Code:
    char zero = {"zero"};
    or
    char *zero = "zero";
    Think bigger - a two dimension char array, (or an array of pointers to strings), where the word can be associated with it's index (it's row in this case), in the table.

    This is an array of char pointers, which is not the same as a true two dimension char array (like char numbers[][] would be), but it's what you want, here.

    Code:
    char *numbers[] = { "zero", "one", "two"};
    Now numbers[0] will be zero, numbers[1] will be one. The numbers array of pointers, keeps track of where the next string (word) is. So you can just increment the index here, to go to the next word.

    Code:
    for(i=0;i<3;i++)
      printf("%s \n", numbers[i]);
    
    zero
    one
    two
    Note that since these are strings, no mention needs to be made of the column(s). That is all handled for you.

  5. #20
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Try this on for size... (Solution in 16 lines)
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main (void)
      { int  number = -1;
        char *asword[] = {"zero","one","two","three","four","five","six","seven","eight","nine"};
        
        printf("Enter a number from 0 to 9 : ");
        scanf("%d",&number);
    
        if ((number < 0) || (number > 9))
          { printf("Error: Only numbers 0 - 9 are allowed\n");
            exit(0); }
    
      printf("You entered the number %s\n",asword[number]);
    
      return 0; }

  6. #21
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by CommonTater View Post
    Try this on for size... (Solution in 16 lines)
    How about this one:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    int main(void){
    
    	char input[5] = {0};
    	char *table[]={"zero","one","two","three","four","five","six","seven","eight","nine"};
    	do{
    		printf("\nEnter a number(0-9): ");
    		scanf("%4[^\n]s",input);
    		while(getchar()!= '\n');
    	}while(strlen(input)>1 || !isdigit(input[0]));
    
    	printf("You entered %s",table[input[0]-'0']);
    	
    	return(0);
    }
    Last edited by AndrewHunter; 08-15-2011 at 05:28 PM. Reason: I am a moron. :p
    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. #22
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by AndrewHunter View Post
    How about this one:
    Code:
    	char *table[]={"one","two","three","four","five","six","seven","eight","nine"};
    	do{
    		printf("\nEnter a number(0-9): ");
    ...
    	printf("You entered %s",table[input[0]-'1']);
    Quote Originally Posted by AndrewHunter View Post
    Did you miss where he stored "zero"
    I missed where you stored it!


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #23
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by quzah View Post
    I missed where you stored it!
    Quzah.
    Haha.....good catch quzah. Fixed it, thanks.
    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.

  9. #24
    Registered User
    Join Date
    Aug 2011
    Posts
    6
    I did it! A fully-functional code using a string table as suggested. Although not as short, and use of getchars to pause the output. It is working fine and accepted. Thanks to all who commented and gie advice. Especially laserlight, AndrewHunter, Adak, CommonTater and quzah! Final Code.

    Code:
    #include <stdio.h>
    int main()
    {
    char *table[]={"zero","one","two","three","four","five","six","seven","eight","nine"};
    int num;
    do
    {
        printf("Please insert a number between 0-9: ");
        scanf("%d", &num);
        if(num>=0&&num<=9){
           printf("The %d equivalent in english is %s\n", num, table[num]);
           getchar();
           getchar();
           return 0;}
        else
           printf("is out of range!!\n");
           getchar();
    }
        while ((num<0||num>9));
        return 0;
    getchar();
    getchar();
    return 0;
    }

  10. #25
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Couple of comments:
    Quote Originally Posted by jimmyskabez View Post
    Code:
    #include <stdio.h>
    int main()
    {
    char *table[]={"zero","one","two","three","four","five","six","seven","eight","nine"};
    int num;
    do
    {
        printf("Please insert a number between 0-9: ");
        scanf("%d", &num);
        if(num>=0&&num<=9){
           printf("The %d equivalent in english is %s\n", num, table[num]);
           getchar();
           getchar();
           return 0;}
        else
           printf("is out of range!!\n");
           getchar(); /* this isn't part of the else */
    }
        while ((num<0||num>9));
        return 0; /* your program stops right here */
    getchar(); /* none of this actually happens */
    getchar();
    return 0;
    }
    Also your indentation leaves a lot to be desired. Indent on an opening braces, un-indent on a close.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #26
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Why do I suddenly think I should have waited for the thread to hit page 4 instead of half way down page 2?

  12. #27
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by CommonTater View Post
    Why do I suddenly think I should have waited for the thread to hit page 4 instead of half way down page 2?
    Haha.....oops. You are too logical Tater, at least there was no way my solution could have been reasonably used.
    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.

  13. #28
    Registered User
    Join Date
    Aug 2011
    Posts
    1
    Code:
    #include <stdio.h>
    
    int check_digit(int n1);
    
    int main(){
        
        int r,n;
        char *e[10]={"zero","one","two","three","four","five","six","seven","eight","nine"};
        
       
        printf("enter a digit:");
        scanf("%d",&n);
        
        while ((n<0)||(n>9)){
        
        printf("PLEASE ENTER THE DIGIT(0~9):");
        scanf("%d",&n);
        r=check_digit(n);
        
        if(r==0)
          break;
         else
          continue;    
    }
        
        printf("%s",e[n]);
        
        getchar();
        getchar();
        return 0;
    }
    
    int check_digit(int n1){
        if((n1>=0)&&(n1<=9)) return 0;
          else return 1;    
    }

  14. #29
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Renyu... no matter how many times you try to disguise it it's still going to look like my code...

  15. #30
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by AndrewHunter View Post
    Haha.....oops. You are too logical Tater, at least there was no way my solution could have been reasonably used.
    Hey there's a plan... I'm gonna follow your lead and start posting code with errors in it!
    (Like I don't already do that sometimes...)

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, 04:35 PM
  2. Storing and printing an array table
    By jaja009 in forum C Programming
    Replies: 3
    Last Post: 03-12-2010, 05:13 AM
  3. Printing a colored table , how to ?
    By GSalah in forum C++ Programming
    Replies: 3
    Last Post: 11-17-2006, 07: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, 01:47 PM

Tags for this Thread