C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 10-26-2009, 02:33 AM   #1
epb
Registered User
 
Join Date: Oct 2009
Posts: 5
int to string - found method not working

Hi,

I tried the following code to convert an int to a string:

Code:
int i = 5678;
string s;
stringstream out;
out << i;
s = out.str();
But it returns an empty string! If I print the "out" variable using
Code:
cout << out << endl;
It prints "0". What could possibly be wrong? I'm using Mac OS X 10.6.1 and gcc version 4.2.1.
epb is offline   Reply With Quote
Old 10-26-2009, 05:06 AM   #2
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,368
Try printing s instead of out and you will probably find that out.str() did not return an empty string.
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is online now   Reply With Quote
Old 10-26-2009, 05:39 AM   #3
Registered User
 
Join Date: Oct 2009
Posts: 11
you can do lyke this

int val=2635;
char s[50];
cout<<itoa(val,s,10)<<endl;

is that you want?
joseCarlos is offline   Reply With Quote
Old 10-26-2009, 06:07 AM   #4
Registered User
 
Join Date: Nov 2007
Posts: 29
Quote:
Originally Posted by joseCarlos View Post
you can do lyke this

int val=2635;
char s[50];
cout<<itoa(val,s,10)<<endl;

is that you want?
Except that's C and not defined in ANSI-C nor a part of C++?
JacobN is offline   Reply With Quote
Old 10-26-2009, 06:55 AM   #5
epb
Registered User
 
Join Date: Oct 2009
Posts: 5
Quote:
Originally Posted by laserlight View Post
Try printing s instead of out and you will probably find that out.str() did not return an empty string.
Sorry if I wasn't clear about this.. I have already tried printing out 's', and that revealed an empty string (which is why I concluded that the code returns an empty string).

Quote:
Originally Posted by joseCarlos
you can do lyke this

Code:
int val=2635;
char s[50];
cout<<itoa(val,s,10)<<endl;
is that you want?
No that is not what I want.. I want to convert the int directly to a C++ string (of the string class)
epb is offline   Reply With Quote
Old 10-26-2009, 07:46 AM   #6
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,368
Quote:
Originally Posted by epb
Sorry if I wasn't clear about this.. I have already tried printing out 's', and that revealed an empty string (which is why I concluded that the code returns an empty string).
Okay, so given this program:
Code:
#include <string>
#include <sstream>
#include <iostream>

int main()
{
    using namespace std;
    int i = 5678;
    string s;
    stringstream out;
    out << i;
    s = out.str();
    cout << s << endl;
}
Compile and run it. What output do you get?
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is online now   Reply With Quote
Old 10-26-2009, 08:10 AM   #7
epb
Registered User
 
Join Date: Oct 2009
Posts: 5
Quote:
Originally Posted by laserlight View Post
Okay, so given this program:
Code:
#include <string>
#include <sstream>
#include <iostream>

int main()
{
    using namespace std;
    int i = 5678;
    string s;
    stringstream out;
    out << i;
    s = out.str();
    cout << s << endl;
}
Compile and run it. What output do you get?
I get no output. If I add
Code:
cout << out << endl;
after the assignment of 'i' to 'out' I get '0' as a single output.
epb is offline   Reply With Quote
Old 10-26-2009, 08:14 AM   #8
Registered User
 
Join Date: Oct 2009
Location: While(1)
Posts: 316
Quote:
Originally Posted by epb View Post
I get no output. If I add
Code:
cout << out << endl;
after the assignment of 'i' to 'out' I get '0' as a single output.

Check this out

Code:
#include <string>
#include <sstream>
#include <iostream>

int main()
{
  using namespace std;
  int i = 5678;
  string s;
  stringstream out;
  out << i;
  cout << out.str() << endl;
  s = out.str();
  cout << s << endl;
}
RockyMarrone is offline   Reply With Quote
Old 10-26-2009, 08:25 AM   #9
epb
Registered User
 
Join Date: Oct 2009
Posts: 5
Quote:
Originally Posted by RockyMarrone View Post
Check this out

Code:
#include <string>
#include <sstream>
#include <iostream>

int main()
{
  using namespace std;
  int i = 5678;
  string s;
  stringstream out;
  out << i;
  cout << out.str() << endl;
  s = out.str();
  cout << s << endl;
}
This is also gives me an empty output

EDIT: The output is two empty lines, so I guess you can say that the output is not completely empty

Last edited by epb; 10-26-2009 at 08:28 AM.
epb is offline   Reply With Quote
Old 10-26-2009, 08:30 AM   #10
Registered User
 
Join Date: Oct 2009
Location: While(1)
Posts: 316
Quote:
Originally Posted by epb View Post
This is also gives me an empty output

EDIT: This gives me two empty lines, so I guess you can say that the output is not entirely empty
I don't know why you are not getting the output but see my shell here

and for your information i m on linux 2.6.29.4 and gcc 4.3.2

Quote:
[rocky@localhost test]$ ls
a.out test.cc
[rocky@localhost test]$ cat test.cc
Code:
#include <string>
#include <sstream>
#include <iostream>

int main()
{
  using namespace std;
  int i = 5678;
  string s;
  stringstream out;
  out << i;
  cout << out.str() << endl;
  s = out.str();
  cout << s << endl;
}
[rocky@localhost test]$ g++ test.cc
.[rocky@localhost test]$ ./a.out
5678
5678
RockyMarrone is offline   Reply With Quote
Old 10-26-2009, 08:32 AM   #11
Registered User
 
Join Date: Nov 2007
Posts: 29
According to this thread:

Cocoabuilder - (Dmitry Markman) ostringstream problem with Xcode 3.2

Assuming you're using Xcode, try setting it to build as Release rather than Debug?
JacobN is offline   Reply With Quote
Old 10-26-2009, 08:32 AM   #12
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,368
Quote:
Originally Posted by epb
after the assignment of 'i' to 'out' I get '0' as a single output.
hmm... that should imply that the stringstream is in a failed state, which is strange.
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is online now   Reply With Quote
Old 10-26-2009, 08:34 AM   #13
epb
Registered User
 
Join Date: Oct 2009
Posts: 5
Quote:
Originally Posted by JacobN View Post
According to this thread:

Cocoabuilder - (Dmitry Markman) ostringstream problem with Xcode 3.2

Assuming you're using Xcode, try setting it to build as Release rather than Debug?
Yes I am using Xcode. And that worked! Thanks
epb is offline   Reply With Quote
Reply

Tags
int, string, stringstream

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading characters from a string and assigning them to a single char??? DonGenaro C++ Programming 26 11-30-2007 03:51 AM
Line Counting 00Sven C Programming 26 04-02-2006 08:59 PM
getting a headache sreetvert83 C++ Programming 41 09-30-2005 05:20 AM
Quack! It doesn't work! >.< *Michelle* C++ Programming 8 03-02-2003 12:26 AM


All times are GMT -6. The time now is 10:30 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22