i want a code to store a decimal number as a string
i'm working on a progeam to convert a binarry number which entered as a string from user to decimal number stored as string.

i have done the converting from binary number to decimal integer
but i couldn't store a dicimal integer as a string ...so that when i print the string the output is the decimal number

i realy want help >>>it's impostant

``````#include <sstream>

stringstream toString;

int num = 55; //Your integer

toString << num;
stringNum = toString->str();
toString->str(""); //Clear toString so you can use it again``````

This code converts an integer into a string. Hope it helps.

Here's some more reading on stringstream …

## All 4 Replies

``````#include <sstream>

stringstream toString;

int num = 55; //Your integer

toString << num;
stringNum = toString->str();
toString->str(""); //Clear toString so you can use it again``````

This code converts an integer into a string. Hope it helps.

Here's some more reading on stringstream if you're interested:
http://www.cplusplus.com/reference/iostream/stringstream/

Try this:

``````#include<iostream>
#include<sstream>
#include<string>
using namespace std;

int main()
{
ostringstream oss;
double number = 0.0;
string string_number;

cout << "Enter number:  ";
cin >> number;

oss << number;
string_number = oss.str();

cout << "\n\nThe number you entered is:  " << string_number;

return 0;
}``````

The above example is based on this tutorial: http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1045689663&id=1043284385

in a loop, use % operator to get the last digit, then /= operator to shift digits right and drop the last digit.

``````#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

int main()
{
int x = 123;
string a;
while(x > 0)
{
int n = x%10;
x /= 10;
a += (char)(n + '0');
}
reverse(a.begin(),a.end());
cout << a << '\n';
return 0;
}``````

Or better still, use previously posted examples which use stringstream class. They are a lot simpler than what I posted here.

thanx
necrolin , clinton portis , ancient gragon

i face some problems to understand these codes
becouse i don't know the work of some of the librraries a bove.
i'm new in c++

but i have think in other way to do what i want in my program
& i didn't need to store the decimal in a string any more

so i just want to convert a binary number to decimal
could any one help me?? plz

i write this code but it print the number in reverse
so what is the right code ??

my code:-

``````int input = 0;

cout<<"Digit : ";
cin>>input;
while(input!=0)
{
cout<<input%2;
input=input/2;
}
cout<<endl;
``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.