How to get the sum of Ascii values?

Hello Community,

Once again I kindly ask for help. As the topic suggests, this is what I try to achieve: Finding a way to sum character values in a particular way.

Here is my code so far:

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
33
34
35
#include <iostream>
using std::cin;
using std::cout;

template <class T>
T total(T numels)
{
	T number{};
	T sumTotal{};
	
	for (; numels > 0; numels--)
	{
                static int count = 1;

		cout << "Enter value #" << count++ << ": ";
		cin >> number;

		sumTotal += number;
	}
	
	return sumTotal;
}

int main()
{
	int numels = 0;

	cout << "Enter " << numels << " characters\n\n";
	cout << "The total value is "
             << total<unsigned char>(numels) << "\n";

	cin.ignore();
	cin.get();
   return 0;
}


By the nature of what is passed to the template function, what is returned is a character. If I change the function call the one below, the value of this character is output:

static_cast<unsigned int>(total<unsigned char>(numels) << "\n";

a+b+c = 38 = '&'

Some remarks
I know of course why, after summing a '97', b '98', c '99', the output is & '38' and not 294. It is the range of unsigned char 0 ... n[255]

The reason for passing an <unsigned char> is to have some meaningful values to watch while debugging. Otherwise, what I get is a '97' + b '98' = -61 'À' etc. The output will of course still be 38 = '&'

One solution I tried is:

1
2
3
4
5
6
7
8
9
10
11
12
13
   if (T number == unsigned char)
   {
      static_cast<unsigned int>(T number);
   }

// Error:
// error C2143: syntax error: missing ',' before '=='

// 1> (46): note: see reference to function template instantiation 'T total<unsigned char>(T)' being compiled
// 1>          with
// 1>          [
// 1>              T=unsigned char
// 1>          ] 


And this:

1
2
3
4
  if (sumTotal == unsigned char(sumTotal))
  {
     return static_cast<unsigned int>(sumTotal);
  }


And this:

 
   static_cast<unsigned int>(sumTotal += number);


The idea was trying to say: if the type is unsigned char, then make it an unsigned integer and return this and not a character. This did not work. Likely because the way I tried is patently wrong. The other less likely reason is, that no matter what is done inside, what is passed to the template is returned from the template.

Summary/Question
Is there a solution to this problem? Meaning to get across, that, if a + b + c is input, the result is 294, that this should be an integer value, and that this should be returned.

Code ..., I would not normally ask for any, but in this case it would be appreciated if it is inevitable. It must not be a/the solution. The general direction in which to look for, or an explanation and some general way how to go about doing it, would also do!
I am not completely sure what you are going for, but do you mean type casting?

1
2
3
4
5
6
7
8
9
10
11
#include<iostream>
using namespace std;

int main() {

	char a = 'a';
	cout << a << " as an int value is " << (int)a << endl;
        cout << 'a' + 'b' + 'c';
	cin.get();
	return 0;
}
Last edited on
Yes, but it does not work.

The point is, that, after all characters are input, the result, after summing the values, ends up being another (unsigned) char. It works up to the point until the last value is added to the sumTotal variable. Casting the result to integer only gives that character's value, that is the result of the calculation.

A simple example - sum of integer 1+2+3 = 6

This is what I wish to happen, only with char.
Last edited on
You cannot store 294 in an unsigned char (assuming 8 bits per char). It will wrap back around to a smaller number.

Instead of making sumTotal be of type T (which eventually is unsigned char), explicitly set it to be of type int. Make the return type be int instead of T, as well.
Last edited on
If you add three char's together you end up with a value that exceeds the char chart. If you want the output to be another char then you might consider looping back around to the start of the ASCII chart.

Can you map out what you wish the input to be and what you were hoping the out put would be?
Ganado, yes, that is the problem. And you point your finger at the other wound. The function should not only accept one type but several different. (Which ones is totally up to me, working with floating-point and int, double, etc works flawlessly).
By having only char or by explicitly setting the type to int would defeat the purpose of the template.

---

Manga, of course. The input should be individual characters, any number the user specifies in the main function.

Given that a user enters:
a
b
c

The result should be 294, the result of summing the values 97+98+99.

And the question is how to get the point across in code. IF POSSIBLE!

Edit: Describing the problem in other words, I do not wish to be the input treated as char but the ASCII values of a char being treated as integer values, that are then summed up. And the integer value should be returned. As per example 294.

This should only take place, if the input is characters.
Last edited on
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <type_traits>
#include <typeinfo>

using std::cin;
using std::cout;

template <typename T>
using MyType = typename std::conditional<std::is_same<T, unsigned char>::value, int, T>::type;

template <class T>
MyType<T> total(T numels)
{
	T number{};
	MyType<T> sumTotal{};
	
	for (; numels > 0; numels--)
	{
                static int count = 1;

		cout << "Enter value #" << count++ << ": ";
		cin >> number;

		sumTotal += number;
	}
	
	return sumTotal;
}

int main()
{
	int numels = 3;

	cout << "Enter " << numels << " characters\n\n";

	cout << "The total value is "
             << total<unsigned char>(numels) << "\n\n";
             
	cout << "Enter " << numels << " floating-point numbers\n\n";
	cout << "The total value is "
             << total<float>(numels) << "\n\n";
             
	cout << "Enter " << numels << " integral numbers\n\n";
	cout << "The total value is "
             << total<int>(numels) << "\n\n";

	cin.ignore();
	cin.get();
	return 0;
}


test to your liking with input:
1
2
3
a b c
0.5 0.8 0.2
100 200 350

The total value is 294
The total value is 1.5
The total value is 650


I think that's what you want, right?

Enter 3 characters

Enter value #1: a
Enter value #2: b
Enter value #3: d
The total value is 295

Enter 3 floating-point numbers

Enter value #1: 3.3
Enter value #2: 4.4
Enter value #3: 5.5
The total value is 13.2

Enter 3 integral numbers

Enter value #1: 5
Enter value #2: 6
Enter value #3: 7
The total value is 18
Last edited on
Ganado, yes, that is EXACTLY what I wanted! Thank you very much! :-))
Topic archived. No new replies allowed.