### Numbers to Words

I'm not sure on how to do this where you put in number and your suppose to get a word example: 2 -> two ,100->hundered using arrays

 ``1234567891011121314151617181920212223242526272829`` `````` #include #include #include using namespace std; char Ones[] = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" , "ten", "eleven", "twelve", "thirteen", "fouthteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" }; char Tens[] = { "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety" }; int main() { cout << "Please enter a number: " << endl; system("pause"); return 0; }``````
Last edited on
I think you want to be using`string` and not`char`. char only takes one character not a word
Last edited on
I think this is easier, but is uncomplete you have to complete it.
 ``123456789101112131415161718192021222324252627`` ``````#include using namespace std; int main() { int a; label: cout << "Please enter a number: " << endl; cin >> a; switch (a) { case 0: cout << "zero" << endl; break; case 1: cout << "one" << endl; break; // Here do the same with 2, 3, 4, etc. default: cout << "Unknown character. Try again." << endl;//Or whatever you want to put goto label; } system("pause"); return 0; }``````
Well you are going to need to start with checking if it is 100 if so print 100.
if it is less than 100 and greater than 19 you are going to want to get the first digit eg and output Tens[x%10 - 2] since twenty is at index 0 so 2 - 2 = 0. Then you are going to want to output 1-19 if there is any remaining after checking the left digit  or it is less than 20 to start with[/edit]
Last edited on
i got it but in a differnet way but i need to add point to the code an example would be like 1000.25 -> one thousand point twenty five .Trying to use a function but im lost here.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899`` ``````#include "stdafx.h" #include #include using namespace std; void saySingleDigit(char x) { switch (x) { case '1': cout << "one"; break; case '2': cout << "two"; break; case '3': cout << "three"; break; case '4': cout << "four"; break; case '5': cout << "five"; break; case '6': cout << "six"; break; case '7': cout << "seven"; break; case '8': cout << "eight"; break; case '9': cout << "nine"; break; } } void sayTwoDigits(char a, char b) { if (a == '1' && b == '1') { cout << "eleven"; return; } if (a == '1' && b == '2') { cout << "twelve"; return; } if (a == '1' && b == '3') { cout << "thirteen"; return; } if (a == '1' && b == '4') { cout << "fourteen"; return; } if (a == '1' && b == '5') { cout << "fifteen"; return; } if (a == '1' && b == '6') { cout << "sixteen"; return; } if (a == '1' && b == '7') { cout << "seventeen"; return; } if (a == '1' && b == '8') { cout << "eighteen"; return; } if (a == '1' && b == '9') { cout << "nineteen"; return; } switch (a) { case '2': cout << "twenty "; break; case '3': cout << "thirty "; break; case '4': cout << "fourty "; break; case '5': cout << "fifty "; break; case '6': cout << "sixty "; break; case '7': cout << "seventy "; break; case '8': cout << "eighty "; break; case '9': cout << "ninety "; break; } saySingleDigit(b); } void sayThreeDigits(char a, char b, char c) { saySingleDigit(a); cout << " hundred "; sayTwoDigits(b, c); } void sayFourDigits(char a, char b, char c, char d) { saySingleDigit(a); cout << " thousand "; saySingleDigit(b); cout << " hundred "; sayTwoDigits(c, d); } int main() { char num[7]; int n, shift = 0; cout << "Please enter a number between -9,999.99 to 9,999.99: "; cin >> num; n = strlen(num); if (num[0] == '-') { cout << "minus "; shift = 1; n--; } if (n == 1 && num[0] == '0') cout << "zero"; else switch (n) { case 1: saySingleDigit(num[0 + shift]); break; case 2: sayTwoDigits(num[0 + shift], num[1 + shift]); break; case 3: sayThreeDigits(num[0 + shift], num[1 + shift], num[2 + shift]); break; case 4: sayFourDigits(num[0 + shift], num[1 + shift], num[2 + shift],num[3+shift]); break; } cout << endl; system("pause");``````
Last edited on
#include <string>
#include <sstream>
#include <iostream>
using namespace std ;

int main( )
{
string term = "100" ; // String to convert to int.
int number = 100 ; // Int to convert to string.
string text ; // String variable for converted int.
int num ; // Int variable for converted string
stringstream stream ; // Intermediary stream object.

// Convert string to int...
stream << term ; // Load string into stream.
stream >> num ; // Extract stream to int.
num /= 4 ; // Manipulate the int.
cout << "Integer value: " << num << endl ;

// Reset the stream object back to new...
stream.str("") ; // Reset the stream to an empty string.
stream.clear() ; // Reset the stream bit flags (good,bad,eof,fail).

// Convert int to string...
stream << number ; // Load int into stream.
stream >> text ; // Extract stream to string.
text += " Per Cent" ; // Manipulate the string.
cout << "String value: " << text << endl ;

return 0 ;
}
Topic archived. No new replies allowed.