converting to string problem

Hey Guys!
I'am just trying to write my function to converting from int value to string .
it seems it'll work , but there is some problem , can any body help !
here's my code :
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>
using namespace std;
int main()
{
	string str(30, '\0');
	int counter=0,x,c;
	float fl,fll;
	cin>>x;
	fl=x;
	c=x;
	while(c>=1)
	{
		c=c/10;
		counter++;
	}
	for(int i=counter-1;i>=0;i--)
	{
		str[i]=(int)((fl/10-x/10)*10)+48;
		x=x/10;
		fl=x;
	}
	cout<<str<<endl;
}

it does't return the correct value ,
like :
input = 123456789
output = 12345577:
or
input = 2587
output = 2586
just like that ..
Last edited on
i would recommend using stringstreams

http://cpp.sh/4ciu
Last edited on
I always found counting backwards to be easier.
1
2
3
4
5
6
7
8
9
std::string str;
int c = 1234567;
while (c>0)
{
   int m = c % 10;
  str.append('0'+m);
  c /= 10;
}
std::reverse(str.begin(),str.end());
@Jaybob66
it's no working !
what's the libraries should I include ?

@rafae11
Thank you !
you code is well done !
but in the 2008 of MSVC it's no working .
it should work i use visual studio also. maybe you did not include "stdafx.h"
Topic archived. No new replies allowed.