Int to String Function

Write your question here.

I'm trying to write a function to convert ints to strings. This is what I have. But when I try to compile I get an error. I can't figure out why. Any help would be appreciated.

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
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <iostream>
#include <sstream>
#include <vector>
#include <fstream>

using namespace std;

int main ()
{
	
	int n = 123;


	
string int_to_string(int n)
{


	ostringstream strm;
	strm << n;
	return strm.str();

}


int_to_string(n);



system("pause");
return 0;

}
Is this your actual program, or did you trim it out? You can't say "why won't my car start" and then give just the seats.

If this is your actual program, you should realize that you cannot define a function inside another function.

Also, you don't need lines 1, 2, 6, 7, 33, or 34. If you include the C headers, don't use the <blah.h> versions, use the <cblah> versions.
Last edited on
You can't nest your function inside main.
Move it outside main.

1
2
3
4
5
6
7
8
9
10
11
12
string int_to_string(int n)
{   ostringstream strm;
    strm << n;
    return strm.str();
}

int main ()
{   int n = 123;
    int_to_string(n);
    system("pause");
    return 0;
} 


You can't define a function inside a function.
Dang it! That's exactly the problem. The reason I have so many #includes up top is because this is my test program. I use it to test things until I get them right and then I move them onto whatever I am working on.

Thank you!
Topic archived. No new replies allowed.