Switch

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
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
not working pall :(
Works on mine.
closed account (z05DSL3A)
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
just remember it'll be backward Greywolf
thanks man :) i realy appreciate your idea man! but why the number is printed zero two one.
how to print one two zero?
closed account (z05DSL3A)
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
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
using namespace std;

int main ()
{

	int broj;
	int sprotiven;
	int cifra;


	cout<<"Vnesi broj: ";
	cin>>broj;
	sprotiven=0;
while(broj>0){
cifra=broj%10;
sprotiven=10*sprotiven+cifra;
broj/=10;
}

while (sprotiven>0)
{
cifra=sprotiven%10;
switch (cifra)
{
case 1: cout<<" eden"; 
	break;
case 2: cout<<" dva";
	break;
case 3: cout<<" tri";
	break;
case 4: cout<<" chetiri";
	break;
case 5: cout<<" pet";
	break;
case 6: cout<<" shest";
	break;
case 7: cout<<" sedum";
	break;
case 9: cout<<" devet";
	break;


}
sprotiven/=10;
}

cin.get(); cin.get();
return 0;
}


I solved the problem on this way :)) tnx anyway ;)
closed account (z05DSL3A)
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
:)))) sorry... i case 0: cout<<"zero"; break; :)))
is it ok now? :))
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
Topic archived. No new replies allowed.