ok. Trying now. thanks! :)
Printable View
ok. Trying now. thanks! :)
I suggest this instead:Quote:
Originally Posted by AndrewHunter
Code:const char *numerals[] = {"one", /* ... */};
Yeah, good point. I thought about that but decided to be lazy with my example. :rolleyes:
Instead of
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.Code:char zero = {"zero"};
or
char *zero = "zero";
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.
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:char *numbers[] = { "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.Code:for(i=0;i<3;i++)
printf("%s \n", numbers[i]);
zero
one
two
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; }
How about this one: :rolleyes::tongue:
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);
}
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;
}
Why do I suddenly think I should have waited for the thread to hit page 4 instead of half way down page 2?
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;
}
Renyu... no matter how many times you try to disguise it it's still going to look like my code...