我正在开始自己学习C++,而且我对一项我正在努力完成的任务感到困惑.用户shoud键入自然数字,只要他想要,直到他输入0.之后,我的程序应找到最大的数字总和并打印出来.它还打印出一个数字,从中得出总和.这是我试图做的:
#include <iostream>
#include <string>
using namespace std;
int main()
{
int input = 0;
int digit;
int sum = 0;
int largest = 0;
do
{
cout << "enter a natural number (0 if done): " << flush;
cin >> input;
while (input > 0)
{
digit = input % 10;
sum = sum + digit;
input = input / 10;
}
if (sum > largest)
largest = sum;
} while (input);
cout << "Max sum of digits was …Run Code Online (Sandbox Code Playgroud) c++ ×1