현재 사용자 입력 문자열을 가져 와서 개별적으로 int로 변환하고 합계를 계산하려고합니다.문자열에서 숫자의 합계 (문자열 개체 사용) C++
EX : 사용자가 "1234"를 입력하면 프로그램은 1 + 2 + 3 + 4를 수행하고 "10"을 표시해야합니다. 나는 많은 것을 시도했지만 여전히 서있는 것처럼 보인다.
C 문자열/문자열 개체와 가능한 경우 "istringstream()"을 사용하여이 코드를 만들려고합니다. (더 쉬운 방법이있는 경우 ...]
내가 잘못 뭐하는 거지#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
string digits;
int total = 0, num;
cout << "Enter a string of digits, and something magical will happen to them..." << endl;
cin >> digits;
for (int i = 0; i < digits.length(); i++)
{
cout << digits.at(i) << endl;
istringstream(digits.at(i)) >> num;
cout << num << endl; // Displays what came out of function
total += num;
}
cout << "Total: " << total << endl;
// Digitize me cap'm
// Add string of digits together
// Display Sum
// Display high/low int in string
}
: 여기
는 지금까지이 무엇인가? for 루프에서 수퍼 하이 숫자를 계속 가져 오는 이유는 무엇입니까? 더 효율적인 기능이 있어야합니까? 어떤 도움 주셔서 감사합니다 :)
'istringstream (digits.at (i)를)'안도 컴파일, 어떤 컴파일러/표준 라이브러리를 사용하고 있습니까? – user657267