Converting int to string C++

Hey,

I need to write a code in my program to convert int to string
like 1 will be USA
2 will be Australia
3 will be Canada
this is just an example actually what i need is so different...

Can anyone make a little example for me to understand?

Thanks in advance
Last edited on
Try to see if this example helps:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
string samplefunction(int i)
{
    switch ( i )
    {
        case 1:
            return "USA";
        case 2:
            return "Australia";
        case 3:
            return "Canada";
        default:
            return "None of the above";
    }
}
Bazzy thank you very much for reply...
Is there any way to do that with arrays?
for example:

countries[3]={"USA","Australia","Canada"};
numbers[3]={1,2,3};

for(int i=0;i<3;i++)
if (numbers[i]==i+1)
{numbers[i]=countries[i]};

I want to make it as small as possible because there could be 100's of counrties...

Thanks in advance!
Try with a map:
1
2
3
4
5
map<int,string> countries;
countries[1] = "USA";
//...

Country_2 = countries[2];

http://www.cplusplus.com/reference/stl/map/

Or simply with an array:
1
2
3
4
5
6
countries[3]={"USA","Australia","Canada"};

string GetCountry ( int n )
{
    return countries[n+1];
}


Or try some other containers:
http://www.cplusplus.com/reference/stl/
Hey bazzy, did you mean [n-1]? I thought maybe you meant to convert from a 1 based to 0 based index. I wasn't sure why you would want use n+1 as an offset into the array. I guess it depends on the functional interface. In the getCountry function interface, what does n represent?
Yes, I meant n-1 so that if n==1 is returned n[0] (the 1st element)
Topic archived. No new replies allowed.