Is this a good way to use a string for int operations?
Nov 9, 2012 at 10:03pm UTC
I'm trying to use a string like an int as well as a string. For example, I have to pass my string through a for loop. My code works, but I get the nagging feeling that there's a better way out there.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
string x = "0" ;
for (int i = atoi(x.c_str()); i<10; i++)
{
stringstream ss;
ss<<i;
x = ss.str();
cout<<x<<endl;
}
cin.get();
return 0;
}
This is just a proof of concept I wiped up. Is this the 'right' way to go about it?
Nov 9, 2012 at 10:34pm UTC
I'm trying to use a string like an int as well as a string.
Why?
Nov 10, 2012 at 12:33am UTC
I can understand wanting to do that with a char, but not a string.
A char is just a small int, so you can do calculations with it.
HTH
Topic archived. No new replies allowed.