converting uesr input int to string output

Hi,
I am writing a code in a class that will change the user input which is integer to a string. For example if the user enter 13347..the output should change to "one three three four seven" on the user screen.I'm not getting the right output.
please help with the code below

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <string>

using namespace std;
	string arr[]={"one","two","three","four","five","six","seven","eight","nine"};
class convertTo{
	public:
		int signed num;
		
	int convet(){
			
		cout<<"Enter a number to convert to string "; cin>>num;
	
		if(num==0){
		arr[1]=num;
		}
	
		if(num==1){
		arr[2]=num;
		}
		
		if(num==2){
			arr[3]=num;
		}
		
		if(num==3){
			arr[num]=num;
		}
		
		if(num==4){
			arr[num]=num;
		}
		
		if(num==5){
			arr[num]=num;
		}
		
		if(num==6){
			arr[num]=num;
		}
		if(num==7){
			arr[num]=num;
		}
		if(num==8){
			arr[num]=num;
		}
		if(num==9){
			arr[num]=num;
		}
	}
	
	void toString(){
	
		
			cout<<arr[num]<< " ";
	
	}	
};

int main(){
	convertTo strr;
	strr.convet();
	strr.toString();
	return 0;
}


Thanks
clement
Look at, for example, line 15. What is it that you think that line is doing?
You have multiple problems

1) You are assigning a int to a array of strings
2) your subscript usage is wrong. Array subscripts start at 0 and go up to arraySize-1. In your case subscript 9 will mean it is out of range.
3) You havent taken care of zero
4) Your program will work only for single digit numbers
5) You will need another array (or vector) of strings to store the result of conversion, in case you are going to modify your program to cater for numbers greater than 9.

If not then instead of assignment like that on line 15, 19 etc. cout the directly like cout << arr[num];. you dont even need those if statements, as long as you can ensure that user enters numbers from 0-9 only
Topic archived. No new replies allowed.