ASCII to regular value

Hi

I'm here trying to convert the letters of a name to its ASCII value and add them, if the final value is not (0,1,2,3,4,5,6,7,8,9,11 or 12) then I wanna add them again but this time as an actual value.

For example if I'm using the name "Hussain" I get the ASCII value of 731 in total but it's not one of the numbers mention above so I wanna add 7+3+1 to get 11 and I can't figure out how to do it.

I appreciate any help.

Here is my code with the comments.

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <string> //The string class
#include <sstream>
using namespace std;

string Thename;// String not Char cause we wanna hold more than 1 character
int Thenumber1;
int Thenumber2;
string Thereducednumber1;
int Thereducednumber2;
int Thereducednumber3;
//The function that converts characters of the name to numbers (reduced numbers if needed, cause the numbers in the end should be one these 0,1,2,3,4,5,6,7,8,9,11,22)
void CharactersToNumbers(){
    
    int i = 0;
    
    for (Thename[i]; i <= 20; i++) {
        Thenumber1 = (int) Thename[i];
        Thenumber2 += Thenumber1;
        Thenumber1 = 0;
        
    }
    
    // Till here it's giving me the right value of ASCII which is 731 if I use the name "Hussain"
    
    //But I'm trying to add the 7, 3 and 1 to the total of 11 but I'm adding the ASCII value of 731 and the actual value of the numbers
    i = 0;
    
    if (Thenumber2 != 0 || 1 || 2 || 3 || 4 || 5 || 6 || 7 || 8 || 9 || 11 || 12 || 22) {
        ostringstream convert;
        convert << Thenumber2;
        Thereducednumber1 = convert.str();
        for (Thereducednumber1[i]; i <= 2; i++) {
            

            
            Thereducednumber2 = Thereducednumber1[i];
            Thereducednumber3 += Thereducednumber2;
            
            
        }
        cout << "The value is: " << Thereducednumber3 << endl ;
    }
    
    else if (Thenumber2 == 0 || 1 || 2 || 3 || 4 || 5 || 6 || 7 || 8 || 9 || 11 || 12 || 22) {
    cout << "The value is: " << Thenumber2 << endl ;
    }
  
}



//****************************** MAIN *********************************************

int main(){
    

    
    cout << "Enter your name: ";
    cin >> Thename;
    
    CharactersToNumbers();
    
    
    return 0;
}
Last edited on

You can do this you know:

 
int n = Thename[0];


So, this will glean the same result:

1
2
3
4
5
6
int Thenumber(0);

for (int n = 0; n < Thename.length(); ++n)
{
   Thenumber += Thename[n];
}
Use ajh32's code to get the number from the string.

You can't do the comparison the way you have the code at lines 29 and 45. Unfortunately, what you've written is valid C++ code, it just doesn't do what you think. Once you add the ASCII values, you can reduce the number like this:
1
2
3
4
5
6
7
8
    if (Thenumber > 9 && Thenumber != 11 && Thenumber != 12 && Thenumber != 22) {
	int newNum = 0;
	while (Thenumber) {
	    newNum += Thenumber %10;
	    Thenumber /= 10;
	}
	Thenumber = newNum;
    }
Topic archived. No new replies allowed.