Overloaded Functions

Hi, I am to create two function that will sum the ASCII values in a string. The first ASCII function solves the first string and the second ASCII function solves two strings. I just need to add the first and second string with the second function without duplicating the code. Any suggestions or hints will be appreciated.

Here's my code:

#include <iostream>
#include <string>

using namespace std;

int sumAscii(string &seqStr);
int sumAscii(string &seqStr, string &seqString);

int main()
{
string getString = "";
string getString2 = "";
cout << "Enter two strings " << endl;
cin >> getString >> getString2;

int theSum = sumAscii(getString, getString2);

cout << "The sum of the ASCII characters in both strings is " << theSum << endl;



return 0;
}
int sumAscii(string &seqStr)
{
int sum = 0;
for (size_t characters = 0; characters < seqStr.length(); ++characters)
sum += seqStr[characters];
return sum;
}
int sumAscii (string &seqStr, string &seqString)
{
return sumAscii((seqStr), seqString);
}

using variadic templates:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <string>
#include <algorithm>

template <typename T>
int sumAscii(const T &seqStr)
{
    return std::accumulate(seqStr.begin(), seqStr.end(), 0,
            [](const int& a, const char& b){ return a + static_cast<int>(b);});
    //http://en.cppreference.com/w/cpp/algorithm/accumulate
}

template <typename T, typename ... Args>
//http://en.cppreference.com/w/cpp/language/parameter_pack
auto sumAscii (const T& seqStr, Args... args) {
  return sumAscii(seqStr) + sumAscii (args...);
}

int main()
{
        std::string getStringOne {}, getStringTwo {};

        std::cout << "Enter first string \n";
        getline(std::cin, getStringOne);

        std::cout << "Enter second string \n";
        getline(std::cin, getStringTwo);

        std::cout << "Ascii sum StringOne " << sumAscii(getStringOne) << "\n";
        std::cout << "Ascii sum StringTwo " << sumAscii(getStringTwo) << "\n";
        std::cout << "Ascii sum of two strings " << sumAscii (getStringOne, getStringTwo) << "\n";
}


This causes an endless recursion
1
2
3
4
int sumAscii (string &seqStr, string &seqString)
{
   return sumAscii((seqStr), seqString);
}

You probably want to do
1
2
3
4
int sumAscii(string &seqStr, string &seqString)
{
  return sumAscii(seqStr + seqString);
}
Thanks gunner for the input but that's way too advance for me.
Thanks thomas! That's what I'm looking for. I don't know why I didn't think of that. It's as simple as returning a + b.
Note that this:
1
2
3
4
int sumAscii(string &seqStr, string &seqString)
{
  return sumAscii(seqStr + seqString);
}

should not compile since the one-argument version of sumAscii takes a non-const reference and the temporary produced by the expression seqStr+seqString should not bind to a non-const reference.

What would compile (and avoid unnecessary copies) is:
1
2
3
4
int sumAscii(string &seqStr, string &seqString)
{
  return sumAscii(seqStr) + sumAscii(seqString);
}


Making the functions const correct would allow the first to compile (although I would still recommend the second.)
Topic archived. No new replies allowed.