+ 3
I need your help
#include <iostream> #include <sstream> #include <vector> #include <string> using namespace std; int main(){ string changeable; int number; vector<int> countChars; cin >> changeable; cout << changeable.at(0); stringstream convert(changeable.at(0)); convert >> number; cout << number; return 0; }
2 Answers
+ 5
What about using a char instead, I mean, if you only want to convert a one digit number? that way you only need to manipulate the char to get its digit e.g.
char input = '9';
int value = (int) input - '0';
More curiosity, why you only want that very first character from the <changeable> string?
(Edit)
This works, though it is carried out in two steps, may not be what you want : }
stringstream convert;
convert << changeable.at(0);
convert >> number;
cout << number;
+ 1
the problem is that it can't convert one character from string to int at this line:
stringstream convert(changeable.at(0));