Hi every one, I am a doctor have a interest in programing ,actually i have been trying to practice on switch statements but i was unable to display numbers like 365 in words
"Three hundred and sixty five", i actually faced problem, all what i was able was to display number 1 to 9 only . I need to display tens, hundreds .
If any one has an idea please help;
i love c++, language

using namespace std;
int main()
    int a;
nxt:    cout<<"\n Please enter a number(99 to stop)..";
    if(a==99) goto xt;
    switch (a)
    case 1: cout<<"One\n";
    case 2: cout<<"Two\n";
    case 3: cout<<"Three\n";
    case 4: cout<<"four\n";
case 5: cout<<"five\n";
case 6: cout<<"six\n";
case 7: cout<<"seven\n";
case8: cout<<"eight\n";
case 9: cout<<"nine\n";

goto nxt;
return 0;   

Recommended Answers

All 13 Replies

And here the complaining starts all over: Don't use void main(), it's evil!!
Migrate to standard C++

>but i was unable to display numbers like 365 in words
Well, where do you think the "hundred" will be inserted ?

Why using goto if you can easily use a while loop ?

The problem isn't strange, but your code is incomplete :)

It's harder than it seems at first. 1-9 is easy, but 10-19 are different and need special cases. Then you need to do 20-99 and can reuse 1-9 inside, then 100-999 reusing everything with a prefix of n hundred. The higher the numbers you allow, the harder it gets to keep things short and simple.

I think switch statements are crazy for this problem. :) An array based approach is going to be much much shorter. You can store the string for each carefully selected part of a number, then break the number up and pick that string out of the array.

Stack Overflow had a cool discussion on the ways to do it.

no actually, i have been told that switch can be used easier than array,,,,,,,,,,,
that is ok,

A switch is easier until you have more than 1-9. Just try to write 0-999 with switches and you'll see how tedious and huge it gets. ;)

no actually, i have been told that switch can be used easier than array,,,,,,,,,,,
that is ok,

Easier? Are you kidding? No!! Never!!
The problem just gets worse when you have to deal with large numbers like 1475012523 for example...
Wow, and you're going to implement all that using a switch?
Good luck then! I'd be glad to receive a copy of your code when it's finished :P

ok, being a doctor i don't have a sufficient idea about this problem so help;

ok, being a doctor i don't have a sufficient idea about this problem so help;

Maybe you could start by taking a look at the link in Tom Gunn's post ?
What help do you expect, where are you having problems with?
Don't expect that we'll write the program for you, under no circumstances, but we can always help you with the parts that you find tricky to implement :)

Was the link I gave to Stack Overflow not good enough? You sure read it and absorbed the whole thing fast. It took me the better part of a slow work day to get it all.

Maybe you should check out this as well :)
(But change the void main() to int main() )

You need to have a loop where you extract a digit at a time. You need to know the place (i.e. hundreds, thousands) and the digit (0 through 9). 10 through 19 are special cases, and you'll have to deal with "twenty", 'thirty", "forty", etc., so you'll have some if or switch statements in there, but regardless, you need to be able to peel off a digit at a time and cut down the work as much as possible. Otherwise you have twenty zillion case statements to your switch statement, or twenty zillion if statements. You need a loop:

int digits[10];

int number = 3498742;

for (int i = 0; i < 10; i++)
     digits[i] = 0;

int place = 0;
int tempNumber = number;

while (tempNumber > 0)
     int digit = tempNumber % 10;
     tempNumber =  tempNumber / 10;
     digits[place] = digit;
     if (tempNumber > 0)

for (int i = place; i >= 0; i--)
     cout << digits[i];

Didn't mean to give you the whole digit-peeling code, but I couldn't think of a way to give you half and have it make any sense. There's a lot more to your task, but this isolates the digits for you and tells you the highest digit place (i.e. 3 for thousands, 6 for millions, etc.), which will come in handy in the rest of your program.

To the OP: Take a look at VernonDozier's post, it will help you much, one thing to mention is that his code will store the number in the reversed way, fro example when you have the following number: 895, it will be stored as "598", there's nothing wrong with this, you only have to take this into account, I say this because otherwise you might not have seen this pitfall and your code would have been wrong from the beginning then :)

Thank you all very much, you have helped me alot,
you all are very kind professionals.

Just to add one thing, before you continue coding in C++, and in fact any programming language. Remember that you want to make things so that they work for EVERY possible case. That is the reason for making programs. For Example: It would be pointless for a web browser to ONLY go to daniwebs.com. It would be pointless for a sorter only to work for 10 different strings and no more. This is where you start using pointers arrays and loops. Switch statements you will probably only use for things like menu's and events.
I hope you continue to explore C++ and remember to come back to daniwebs for any help :).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.