简体   繁体   中英

C++ convert string array to integers, then sum int array

I need to pass an user input integer to a sumTotal(& userInt) function.
If the int is 2341 I need to sum 2+3+4+1 = 10 and return the value to main!

#include <iostream>                          
#include <string>                           
#include <vector>

using namespace std;                        

// The program needs to input an integer like 2341, then sum it as 2+3+4+1 + 10
// I am in putting an integer and will pass it to a funtion by &.
int main()
{
    string strNumber;
    int intNumber = 0;

    cout << "Enter your number: ";
    cin >> intNumber;

    // programming the logic for sumTotal(& intNumber) function before creating 

    strNumber = to_string(intNumber);
    cout << "Your number is: " << strNumber << endl;
    cout << "Your numbers length: " << strNumber.length() << " digits" << endl;

    // here I need to convert the string array to an integer array
    for (int i = 0; i < strNumber.length(); ++i){
        intNumber[&i] = strNumber[i] - '0';
        cout << "Element [" << i << "] contains: " << strNumber[i] << endl;
    }

    // next a recursive function must sum the integer array
    // I am taking an online course and cant afford a tutor please help!


    system("pause");                                            
    return 0;

}

A string is an array of char s. To convert a char to an int you have to do 'char' - '0' .

I wrote a couple of versions. Pick whichever one you like best.

#include <iostream>
#include <string>

int main()
{
    std::string str = "1234";
    int sum = 0;

    //pre C++11
    for (std::string::iterator i = str.begin(); i != str.end(); ++i)
        sum += (*i - '0');

    //C++11
    sum = 0;
    for (auto i = str.begin(); i != str.end(); ++i)
        sum += (*i - '0');

    //ranged-for
    sum = 0;
    for (const auto &i : str)
        sum += (i - '0');

    std::cout << "Sum: " << sum;

    std::cin.get();
}

A simple and efficient method is to keep the number as a string and access the digits one at a time.

Note the equation:

digit_number = digit_character - '0';

Also, knowing that when summing digits, the order is irrelevant. So, we have:

sum = 0;
for (i = 0; i < string.length(); ++i)
{
  sum += string[i] - '0';
}

if you want recursion ,you don't need any string work,try this :

#include <iostream>
using namespace std;

int recSum(int);
int main(){
    int i;
    cin>>i;
    cout<<recSum(i);
    return 0;
}

int recSum(int i){
    return i==0?0:i%10+recSum(i/10);

}

recursion on array version

#include <iostream>
using namespace std;

int recSum(int* ary,int len){
    return len<0?0:ary[len]+recSum(ary,len-1);
}
int main(){
    int j[]={1,2,3,4,5,6,7,8,9,10};
    cout<<recSum(j,9);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM