Getting wrong result while adding the sum of the ascii values of a string

//I have written a code to print the sum of the ascii values in a string but I keep getting a wrong result. I couldn't figure out where I was wrong.

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 <algorithm>
#include <vector>
#include <string>

using namespace std;

//Hashing
int main()
{
	vector<string>v = { "leo"};
	vector<int>total;

	for (int i = 0; i < v.size(); i++)
	{
		string s = v[i];
		int sum = 0;
                //This loop calculates the sum of the ascii values of the given string
		for (int j = 0; j < s.size(); j++)
		{
			char c = s[j];
			sum = sum + (int)c;
		}
		total.push_back(sum);
	}
	for (int i = 0; i < total.size(); i++)
	{
		cout << "The sum is " << total[i] << endl;
	}

	return 0;
}
Last edited on
What 'wrong' result are you getting?
What is the expected (correct) result that you aren't getting?

http://www.asciitable.com/
Last edited on
@Ganado, I found my mistake.. Thanks..
Topic archived. No new replies allowed.