GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 12-Jan-2004, 09:44
Gimei Gimei is offline
New Member
 
Join Date: Jan 2004
Posts: 1
Gimei is on a distinguished road
Angry

C help


Help am a C newbie. Could someone please tell me how to convert an integer into a string and evaluate. like 5 to "five". I am using Turbo C
  #2  
Old 12-Jan-2004, 10:42
Garth Farley Garth Farley is offline
Invalid Email Address
 
Join Date: May 2002
Location: Ireland
Posts: 638
Garth Farley is a jewel in the roughGarth Farley is a jewel in the roughGarth Farley is a jewel in the rough
Takes a bit a work. Are these numbers going to be big? You'll have to get each digit, starting from the right, and maybe using loads of if statements (or more cleverly: switch, or better, use arrays), and slowly stick together a string.

CPP / C++ / C Code:
int number = 4563, temp;
bool = false;
string output;
int position = 0;

string single[10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
string _ies[9] = {"eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
string tens[10] = {"ten", "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninty", "hundred" };

if(number < 0) negative = true;
while(number >= 0){
	temp = (number % pow(10, position));
	number -= temp;
	temp /= pow(10, position); //temp contains the [position] digit.
	if(temp == 0){
		position++;
		continue;
	}else{
		if((position%3) = 1){
			output = single[temp] . output;
		}else if((position%3) = 2){
			output = ten[temp] . output;
		}
		else{
			output = singe[temp] . "hundred" . output;
		}
	}
}

That's very rough, and quite bad really. But you get the idea.
GF
 
 

Recent GIDBlogPython ebook by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 07:42.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.