i need some help here....i am taking input as name and i want to count the alphabets in the string as integers....e.g. if the in out is cab i have to output 3+1+2 = 5 i have been able to ad d up the strings..but if my input starts with a " " (space) it does not wrk...please help

n = toLowerCase(n);

for(int i = 0 ; i<n.length() ; i++)
{
lower = lower + n.at(i);
} 
for(int k=0;k<n.length();k++)
{

index=index+int(n.at(k)-96);
cout<<index<<endl;
//just to see adding
}


while(index>=10)
{
temp=temp+index%10;
index=index/10;

s2=sum=temp+index;
} 
sum=sum%10;
s2=s2/10;
a=sum+s2;
cout<<a<<endl;
if(a==1)
cout<<"1you are quirky and eccentric"<<endl;
else if(a==2)
cout<<"2you are introvert and shy"<<endl;
else if(a==3)
cout<<"3you are party animal"<<endl;
else if(a==4)
cout<<"4you are trustworthy and reliable"<<endl;
else if(a==5)
cout<<"5you are patient and forgiving"<<endl;
else if(a==6)
cout<<"6you are friendly and jovial"<<endl;
else if(a==7)
cout<<"7you are lucky and talented"<<endl;
else if(a==8)
cout<<"8you are genius"<<endl;
else if(a==9)
cout<<"9you are street-smart and cunning"<<endl;

isalpha()

Loop throught the input string, copy any alpha characters across to a new string, just ignore others

Chris

i din get it...can u show an example...

#include <iostream>
#include <string>
#include <cctype>
#include <vector>

using namespace std;

int main(void){
    string input;
    vector<char> v;
    while(1){
        cout << "Enter a string:\n>>";
        getline(cin, input);
        for(int i = 0; i < input.length(); i++){
            if(isalpha(input[i]) != 0){
                cout << input[i];
                v.push_back(input[i]);
            }
        }
        cout << "\nNumerical Value: ";
        for(int i = 0; i < v.size(); i++){
            cout << toupper(v.at(i))-64;
        }
        v.clear();
        cout << endl;
    }
    return 0;
}

Hope that makes sense

Chris

I think the following snippet is more efficient (using the ideas of the above code by Chris):

#include<iostream>
#include<cctype>
#include<string>

using namespace std;

int main(void)
{
int InputValue=0,x;
string Input;
cout<<"Input a string to check its value:\n";
getline(cin,Input);
    for(x=0;x<Input.length();x++)
    {
     if(isalpha(Input[x]))
        {
        InputValue+=toupper(Input[x])-'A'+1;
        cout<<toupper(Input[x])-'A'+1<<"+";
        }
    }
cout<<"\b="<<InputValue<<"\n";
system("pause");
return 0;
}

Just to bring it to your attention: 3+1+2 = 6, not 5 :P

Just to note, it was not all about efficiency more demonstrating something, it also left him values to work with and didn't provide the actually solution to his problem meerly demonstrated a method in which he could adapt to get the solution he was after.

[edit] if you want a more hacky solution

#include <iostream>
#include <string>
#include <cctype>

int main(void){
    std::string input;
    int total = 0;
    getline(std::cin, input);
    for(int i = 0; i < input.length(); i++) (isalpha(input[i]) ? total+= (toupper(input[i])-64) : total+=0);
    std::cout << total;
    return 0;
}

Chris

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.