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.
#include <iostream>
#include <string> //The string class
#include <sstream>
usingnamespace 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 ;
}
elseif (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;
}
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: