我正在尝试自己学习c ++,而且我遇到了一些障碍.问题是我需要取一个整数,将其拆分为数字并获得数字的总和并显示它们.
例:
输入数字:123456
整数中的数字:1 2 3 4 5 6
sum:21
我完成了所有这些,但是当我将整数转换为数字时,我无法正确显示它.它以相反的顺序显示.
所以在下面的程序中我输入1234并吐出来4 3 2 1.我知道为什么,我只是不知道如何解决它.
到目前为止,这是我的代码:
#include "stdafx.h"
#include <cstdlib>
#include <iostream>
#include <math.h>
int countDigitsInInteger(int n)
{
int count =0;
while(n>0)
{
count++;
n=n/10;
}
return count;
}
using namespace std;
int main(int argc, char *argv[])
{
int intLength =0;
int number;
int digit;
int sum = 0;
string s;
cout << "Please enter an integer ";
cin >>number;
cout << …Run Code Online (Sandbox Code Playgroud)