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

please help me
i realy want help >>>it's impostant

7 Years
Discussion Span
Last Post by #www#
#include <sstream>

stringstream toString;

int num = 55; //Your integer
string stringNum; //Your output

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:


Try this:

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

Edited by Clinton Portis: your mom.


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');
    cout << a << '\n';
    return 0;

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

Edited by Ancient Dragon: n/a


necrolin , clinton portis , ancient gragon

for your help
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 : ";

Edited by Dani: Formatting fixed

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.