Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello!!!
Well, I want to make a programme in C which will do this:

I will type 1 and it will appear "one" on the screen.
If I had a small number of variables I could do it like this:

C#
switch (x)

{
  case 0:printf("Zero.\n");
         break;
  case 1:printf("One.\n"); etc...
         break;
}
But in my programme i wanna do this with the numbers 0 to 99!!
I don't think that I should write a hundred cases after the switch!(at list I hope so...)

Thank you in advance!! :-)
Posted
Updated 14-Nov-11 9:00am
v2
Comments
Sergey Alexandrovich Kryukov 14-Nov-11 14:59pm    
What did you try? Forget your switch... any serious ideas? This is so simple.
--SA
MG2011 14-Nov-11 15:02pm    
I did't think of anything else... :-(

This is quite simple.
If all you want is [0-99] then all you need is the GetHundredsWords below, but this code will print any number you can put in an integer.

For every order of 1000, the numbering is the same, for example: 123 is one hundred twenty three and 123,000 is one hundred twenty three thousand, so we only really need code which can go up to 1000 (GetHundredsWords()), then just do that for every order of 1000, adding in the correct magnitude (GetWord())

C++
#include <stdio.h>

static size_t GetHundredsWords(char *buffer, int n, bool &bAppended) {
	//Shorthand to make sure spaces are added when needed
	#define APPEND(fmt, ...) \
		if (bAppended) { \
			*ptr++ = ' '; \
		} else { \
			bAppended = true; \
		} \
		ptr += sprintf(ptr, fmt, __VA_ARGS__);

	const char *szOnesWords[] = { "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
	const char *szTeenWords[] = { "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
	const char *szTensWords[] = { "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };
	char *ptr = buffer;
	if (n >= 100) {
		APPEND("%s hundred", szOnesWords[n / 100 - 1]);
		n %= 100;
	}
	if (n >= 20) {
		APPEND(szTensWords[n / 10 - 2]);
		n %= 10;
	}
	if (n >= 10) {
		APPEND(szTeenWords[n - 10]);
	} else if (n > 0) {
		APPEND(szOnesWords[n - 1]);
	}
	return ptr - buffer;
}

char *GetWord(char *buffer, int n) {
	const char *szMagnitudes[] = { " billion", " million", " thousand", "" };
	char *ptr = buffer;
	if (n == 0) {
		return "zero"; //Special case
	}
	if (n < 0) { //Is this a negative number
		ptr += sprintf(ptr, "negative ");
		n *= -1;
	}
	bool bAppended = false;
	int nMagnitude = 1000 * 1000 * 1000; //1 billion
	for (int i = 0; nMagnitude > 0; ++i) {
		if (n >= nMagnitude) {
			ptr += GetHundredsWords(ptr, n / nMagnitude, bAppended);
			ptr += sprintf(ptr, szMagnitudes[i]);
			n %= nMagnitude;
		}
		nMagnitude /= 1000;
	}
	//GetHundredsWords(ptr, n, bAppended, bAnd);
	return buffer;
}

#define TEST(n) printf("%d is \'%s\'\n", n, GetWord(buffer, n)) //Save having to type this for every test

int main(int argc, char *argv[]) {
	char buffer[64];
	TEST(0);
	TEST(1);
	TEST(5);
	TEST(9);
	TEST(10);
	TEST(11);
	TEST(12);
	TEST(19);
	TEST(20);
	TEST(31);
	TEST(42);
	TEST(100);
	TEST(101);
	TEST(109);
	TEST(110);
	TEST(115);
	TEST(120);
	TEST(151);
	TEST(546);
	TEST(1000);
	TEST(1001);
	TEST(1024);
	TEST(1564);
	TEST(1000000);
	TEST(1000000000);
	TEST(1561981328);
	TEST(-2147483647);
	return 0;
}
 
Share this answer
 
Comments
MG2011 15-Nov-11 9:24am    
Thank you so much for your answer!!
I tried to compile it but the compiler has some problems with it, which I can't solve...! :-(
Anyway I pretty much understood how it works, and I wanna make a programme that works like that but there are things that I don't know how to use. actualy I don't even know what they are... For example what is this "static size_t GetHundredsWords"?
I will use that as a compass to make sth on my own! :-)
Thank you very much, again, for spending so much time for me..! :-)
Andrew Brock 15-Nov-11 9:28am    
The keyword "static" in this place means that the function GetHundredsWords() is only usable from within the .cpp file where the code is. In such a simple program it has no effect, since there is only 1 source file.
I use this notation for defining internal functions which should not be used by external code, in this example you would not use this function directly, you would just use GetWord()
You'll probably want to break it down into ranges, for example anything between 20 and 29 will start with "twenty", so something like:
C++
if (number >= 20 && number <= 29)
{
    printf("Twenty");
}


Of course, you'd need something similar for 30, 40, and so on

Then you'll want to print something after that depending on the last digit, there are a few different ways to do that, like the modulus operator, or if you're breaking down into ranges like this you can use subtraction (e.g. if its in the range of 20's, subtract twenty and all you'll have left if the last digit, 21 goes to 1, 22 goes to 2, etc.) So after printing the first word (if there is one) you can then print the remaining word (using a switch for this last part could work).

Don't forget special cases, for example if it's just 0 you'd want it to print "Zero" but if it's 20 you wouldn't want "Twenty Zero"!
 
Share this answer
 
Comments
enhzflep 14-Nov-11 21:02pm    
Precisely!
One needs only add 28 strings to a program to print from 0 - 99.
I.e "zero", "one" ... "nineteen" and "twenty", "thirty" ... "ninety"

Two arrays of strings, a quick modulo and division to get the ones digit and the tens digit, some simple logic to determine which string(s) to display.
An array of 99 strings?? Pffft!!
MG2011 15-Nov-11 9:18am    
That sounds ok, I mean I have learnt how to do it so I will try it first, and then I'll try and make it with arrays, (solution 1).
Thank you very much!! (I think I should have thought of that myself!! :-()
MG2011 15-Nov-11 10:44am    
I did it that way!! It's working veeeeeeeeery good... Now i'm gonna try it with arrays...God help me...lol
Never ever use a switch which contains just a few cases. And in your sample, even two cases would be a total abuse.

You need just one function which accepts an integer in certain range and returns a string. If a value is outside the allowed range, return, for example, null. Do the following: create an array of strings 0 to 99. The function should first check if input value is in the range and return null of something to indicate invalid input. If the value is in the valid range, return the array element by its index.

Simple as that.

—SA
 
Share this answer
 
Comments
MG2011 14-Nov-11 15:13pm    
I haven' learnt how to make arrays yet... It sounds simple, but it's still theory to me.
Thanks anyway, though!
Sergey Alexandrovich Kryukov 14-Nov-11 19:19pm    
You are welcome.

So, go to next step! It will work, believe me.
If you agree it makes sense, please formally accept the answer (green button) -- thanks.
--SA
MG2011 15-Nov-11 9:16am    
OK I will try and make an array and I will post the outcome here, so that you can correct my mistakes. Oooh I'm anxious, this will be my first try! Anyway, it is definitely simplier with arrays, that the other way(That creates a loop for every ten numbers) so it's worth trying!
Sergey Alexandrovich Kryukov 15-Nov-11 11:23am    
Great. Please don't post it as a solution; instead, use "Improve question" above and add a section to the body of your question (where you can format code as <pre lang="c">....</pre>). Post a comment here as well.
--SA
MG2011 19-Nov-11 9:34am    
Well I tried sth..it's working but still I have to learn more thingsabout arrays to work with them easier and quicker..!
Here is the code:(I don't know how to make "eleve, twelve...nineteen" show up if they are in the array...I get some very funny results, like tentwo for twelve..lol!!!)
#include <stdio.h>
#include <stdlib.h>

main()
{
char ar1[11][12]={ "","ten","twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };
char ar2[11][10]={ "", "one", "two", "three", "four", "five", "six","seven", "eight", "nine", "ten"};
int x, y, z;
printf("Give number: \n");
scanf("%d", &x);
z=x/10;
y=x%10;
if (z==0 && y==0)
{
printf("Zero.\n");
goto end;
}
if (y==1 && z==1)
{
printf("Eleven.\n");
goto end;
}
if (z==1 && y==2)
{
printf("Twelve.\n");
goto end;
}
if (z==1 && y==3)
{
printf("Thirteen.\n");
goto end;
}
if (z==1 && y==4)
{
printf("Fourteen.\n");
goto end;
}
if (z==1 && y==5)
{
printf("Fifteen.\n");
goto end;
}
if (z==1 && y==6)
{
printf("Sixteen.\n");
goto end;
}
if (z==1 && y==7)
{
printf("Seventeen.\n");
goto end;
}
if (z==1 && y==8)
{
printf("Eighteen.\n");
goto end;
}
if (z==1 && y==9)
{
printf("Nineteen.\n");
goto end;
}
printf("%s%s!\n", ar1[z],ar2[y]);
end:
system("pause");
}
C#
void printnum(int num)
{
char *iOnesTens[] = { "zero", "one", "two", "three", "four", "five", "six",   "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
char *iTens[] = { "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };

if(num==0)
 printf(" %s",iOnesTens[num]);
else
{
 if( num / 20)
 {
  printf("\n %s",iTens[num/10]);
  num=num%10;
 }
 if(num!=0)
  printf(" %s",iOnesTens[num]);
}
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900