Jan 17, 2011 at 1:19pm UTC
I input one number. For ex. 6578. What i want to do is to print the entered number with letters. for ex. six five seven eight. I tried something like this:
int i;
int n;
cout<<"Enter value for n: ";
cin>>n;
if (n==0)
i=0;
if (n==1);
i=1.... untill 9;
and with switch (i) {
case 0: cout<<zero;
case 1: cout<<one;
etc...
Thanks for the help ;)
Last edited on Jan 17, 2011 at 1:20pm UTC
Jan 17, 2011 at 1:34pm UTC
Why are you creating the variable
i
? Can't you just use
switch (n)
?
If you're using cout to output text, you have to put the text in inverted commas:
1 2 3 4
case 0: cout<<"zero" ;
break ;
case 1: cout<<"one" ;
break ;
Don't forget the
break
Last edited on Jan 17, 2011 at 1:35pm UTC
Jan 17, 2011 at 1:53pm UTC
Here is a bit of code to help you think about it in a different way:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
#include <string>
int main ()
{
std::string number[] = {"Zero" , "One" , "Two" };
int n = 120;
do
{
int x = n % 10;
n /= 10;
std::cout << number[x] << " " ;
}while (n != 0);
std::cout << std::endl;
}
or with a switch:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#include <iostream>
#include <string>
int main ()
{
int n = 120;
do
{
int x = n % 10;
n /= 10;
switch (x)
{
case 0 : std::cout << "zero" ; break ;
case 1 : std::cout << "one" ; break ;
case 2 : std::cout << "two" ; break ;
default : std::cout << "Oops" ;
}
std::cout << " " ;
}while (n != 0);
std::cout << std::endl;
}
Last edited on Jan 17, 2011 at 2:00pm UTC
Jan 17, 2011 at 2:15pm UTC
just remember it'll be backward Greywolf
Jan 17, 2011 at 2:16pm UTC
thanks man :) i realy appreciate your idea man! but why the number is printed zero two one.
how to print one two zero?
Jan 17, 2011 at 2:28pm UTC
craniumonempty wrote:just remember it'll be backward Greywolf
I know, I wouldn't want to do all the work. :0)
how to print one two zero?
Separate the splitting into digits from the printing. In the splitting use something like a Stack to store the digits.
When you Pop them in the printing section they will come out in the correct order:
I'm' feeling generous 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
#include <iostream>
#include <string>
#include <stack>
int main ()
{
std::string number[] = {"Zero" , "One" , "Two" };
int n = 120;
std::stack<int > mystack;
do
{
int x = n % 10;
n /= 10;
mystack.push(x);
}while (n != 0);
while (!mystack.empty())
{
std::cout << number[ mystack.top() ] << " " ;
mystack.pop();
}
std::cout << std::endl;
}
Last edited on Jan 17, 2011 at 2:44pm UTC
Jan 17, 2011 at 3:00pm UTC
No Zero?
Vnesi broj: 10005
eden pet
Hint: adding a case for zero will also not fix it for somthing like 100
Last edited on Jan 17, 2011 at 3:04pm UTC
Jan 17, 2011 at 6:04pm UTC
:)))) sorry... i case 0: cout<<"zero"; break; :)))
Jan 17, 2011 at 6:13pm UTC
Instead of manually reversing the number you could use recursive post-order.
like in
http://www.cplusplus.com/forum/beginner/34403/#msg185984
Edit: more clear (if n==0 there is no output)
1 2 3 4 5 6
void ToBinary2(unsigned int n){
if (u){
ToBinary2( n/2 );
std::cout<<( (n%2)? '1' : '0' );
}
}
Last edited on Jan 17, 2011 at 6:18pm UTC