Hankyaku, I think you wanted IntToStr and to use length rather then temp in your loop. In any event, none of these are standard. The cool standard way of doing this would be
Code:
#include<iostream>
#include<string>
#include<algorithm>
using std::cout;
using std::endl;
struct adder {
unsigned sum;
adder() : sum(0) {}
void operator()(char ch) {sum += ch-'0';}
operator unsigned() {return sum;}
};
int main() {
std::string s;
if(std::cin >> s) cout << std::for_each(s.begin(),s.end(),adder()) << endl;
else std::cerr << "Bad Input" << endl;
return 0;
}
Here's a slightly more readable version that also inlcudes a fun premature optomization. Trading n subtractions for a single additonal multiplication and subtraction.
Code:
using std::cout;
using std::endl;
int main() {
std::string str;
std::cin >> str;
const unsigned size = str.size();
int sum = 0;
for(unsigned i=0; i != size; ++i) sum += str[i];
sum -= size * '0';
cout << sum << endl;
return 0;
}