### Converting a string to an integer

Trying to convert a username into how many characters it is (ie the name Bob = 3) to do further calculation further down to multiply by age. I feel Im so close

 12345678910111213141516171819202122 #include #include using namespace std; int main(){ string fullname; int a = fullname.length(); int age; int enrol; int pin; cout <<"Welcome to the our App\n"; cout <<"Please enter your full name:\n"; cin >> fullname; cout <<"Please enter your age:\n"; cin >> age; cout <<"Please enter your enrolment number:\n"; cin >> enrol; cout <<"Generating pin...\n"; pin = (a*age)*enrol; cout <<"Hi " << fullname << ". Your account " << enrol << " has been created.\nYour unique pin number is " << pin; return 0;
 12 string fullname; int a = fullname.length();

I see that at this point in the code, the string is empty. Of size zero. So a will always be zero.

So you might as well just write
 int a = 0;

Did you mean for a to always be zero? If not, I suspect you need to think again about this part of the code.
I see.
No i didnt mean for it to be zero, it has to have full name put through

I have now moved the line to later on in the code.

Just trying to work out out to count 2 or 3 words if I have a fullname and ignore spaces

 123 cout <<"Please enter your first name:\n"; cin >> firstname; int fname = firstname.length();
Why not use getline(cin, fullname) instead of cin >> to allow the use to enter their full name including spaces?

So basically the question/ input is asking for the users full name and find the length of name so if name entered is John my code converts that to integer 4 to be multiplied by age input.

Im having trouble when users name is John Doe this should output 7 (ignoring the space) so while trying to get this to work I was cheating a little bit and asking for first name first, then asking for surname and adding together

But I also need it to work if someone had 3 names or a double barrelled name etc.

so your code helps but it includes the space which I dont want

Edit:
I have it working with this to subtract spaces but doesnt work with characters (John Bob-Doe returns 11, counting the - which im not to bothered with at the moment)

 12345 cout <<"Please enter your full name:\n"; getline(cin, fullname); //cin >> firstname; fullname.erase(remove(fullname.begin(), fullname.end(), ' '), fullname.end()); int fname = fullname.length();
Last edited on
The <cctype> library has isalpha(), useful on individual elements of a string.

https://en.cppreference.com/w/cpp/string/byte/isalpha

 123456789101112131415161718192021222324252627282930 #include #include #include int CountLetters(const std::string&); int main() { std::string str1 { "John Doe" }; std::string str2 { "John Bob-Doe" }; std::cout << "\'" << str1 << "\' has " << CountLetters(str1) << " letters.\n"; std::cout << "\'" << str2 << "\' has " << CountLetters(str2) << " letters.\n"; } int CountLetters(const std::string& str) { int num_letters { }; for (const auto& itr : str) { if (::isalpha(itr)) { num_letters++; } } return num_letters; }

 'John Doe' has 7 letters. 'John Bob-Doe' has 10 letters.
Do you mean something like this which only counts alpha chars:

 12345678910111213141516171819 #include #include #include #include using namespace std; int main() { std::string fullname; cout << "Please enter your full name: "; getline(cin, fullname); fullname.erase(remove_if(fullname.begin(), fullname.end(), [](unsigned char c) {return !std::isalpha(c); }), fullname.end()); const auto namsze{ fullname.length() }; cout << namsze << '\n'; }

Thanks guys, this helps alot.

i was looking more for Seeplus answer, but knowing it was alpha chars I needed was the key.
I dont think Ill forget that, Thanks
Last edited on
You don’t need to erase anything, just count the things that matter.

 123456789101112131415161718 #include #include #include #include int main() { std::string fullname; std::cout << "Please enter your full name: "; getline( std::cin, fullname ); auto name_length = std::count_if( fullname.begin(), fullname.end(), []( char c ) { return !(c & 0x80) and !std::isspace( c ); } ); std::cout << "You have " << name_length << " letters in your name.\n"; }

The weird []( char c ) { ... } thing is a lambda — I could have used a normal function instead but this just keeps all relevant code in one spot.

The !(c & 0x80) clause is for UTF-8 awareness — making your non-whitespace letter counting thing able to count a multibyte UTF-8 sequence as a single code-point value (which is how it is displayed, even though it is made up of multiple characters).

If that doesn’t matter to you, just leave it out:

 1234 []( char c ) { return !std::isspace( c ); }

Hope this helps.
Topic archived. No new replies allowed.