PDA

View Full Version : print text


truviet911
10-14-2009, 02:49 AM
Hi guys. i wrote this little program . and been search online for help but no luck. anyways i want it work when a user input some number it give me words for output. example.

input: 123
output: one hundred and three...and so fourth

here is my code.



#include <iostream>
#include <fstream>
using namespace std;


int main() {

int number = 0.0 ;

cout << "enter a number from 1 to 10: ";
cin >>number;


//cout <<"number:"<< endl;

switch (number){
case 1:
cout << "one" << endl;
break;

case 2:
cout << "two" << endl;
break;

case 3:
cout << "three" << endl;
break;

case 4:
cout << "four" << endl;
break;

case 5:
cout << "five" << endl;
break;

case 6:
cout << "six" << endl;
break;

case 7:
cout << "seven" << endl;
break;

case 8:
cout <<"eight" << endl;
break;
case 9:
cout <<"nine" << endl;
break;
case 10:
cout <<"ten" << endl;
break;
default:
cout <<"nothing is enter" << endl;
break;

system("pause");

return 0;
}
}


is there a better way to do it. what happend if a user input something like
40298---the output should be fourty thousand, two hunder and ninety eight
how am i going to do this? i can't just sit there and do the switch statement forever. lolz anyways im really new at c++. hope you guys can help me out

tomws
10-14-2009, 03:54 AM
You don't need infinite numbers. You could limit it to place names (hundred, thousand, etc.), plus digits, teens, and the tens (twenty, thirthy, etc.). Modulus can help determine the place location to convert into a name. That's how I'd try it, anyway.

truviet911
10-14-2009, 06:56 AM
You don't need infinite numbers. You could limit it to place names (hundred, thousand, etc.), plus digits, teens, and the tens (twenty, thirthy, etc.). Modulus can help determine the place location to convert into a name. That's how I'd try it, anyway.

didn't get anything from this:D im still a noob man

tomws
10-14-2009, 03:06 PM
Then read it again and study your course textbook. I've given one way to solve the problem. The course text will provide the means to implement something like that.

oracleguy
10-14-2009, 05:34 PM
didn't get anything from this:D im still a noob man
Then read up on the modulus operator because you'll need it to solve this problem. My solution would be along the same lines as tomws' solution.