Using cctype?

I've read through the reference pages here many times now, but I'm fairly new to programming and do not quite understand how to work it. I am interested in using tolower, and isalpha. I wanted to create a function to check if user input was alphabetic to prevent possible errors due to incorrect user input (such as inputting numbers to a string), and convert to lower for easier program navigation (so I don't have to use multiple cases of each word to get the same result). Could anyone here please give me some more detailed, or easier to understand, information on using these? Such as what I should put in my code to use them?
Actually, inputting numbers as a string is usually fine (unless it's a string that has to follow specific semantics). Inputtings NaN characters for number input is worse.
tolower() returns a lower-case version of a char you put into it, or the same character if that doesn't apply.

tolower('A') == 'a'

isalpha() returns true if the character passed is a alphabetical, and false otherwise.

1
2
isalpha('d') == true
isalpha('2') == false
tolower() returns a lower-case version of a char you put into it, or the same character if that doesn't apply.

tolower('A') == 'a'

isalpha() returns true if the character passed is a alphabetical, and false otherwise.

1
2


isalpha('d') == true
isalpha('2') == false

I wanted to know if there is any way I can pass a whole string through tolower, using the string name, I tried various forms of tolower(StringTemp) & isalpha(StringTemp) but they came up with a build error every time.


Actually, inputting numbers as a string is usually fine (unless it's a string that has to follow specific semantics). Inputtings NaN characters for number input is worse.

I didn't entirely mean errors due to coding issues, this particular program uses user input to navigate to different parts of the program, and passing numbers through where they should pass letters has very odd effects, in respect to where they end up.

I wanted to know if there is any way I can pass a whole string through tolower, using the string name, I tried various forms of tolower(StringTemp) & isalpha(StringTemp) but they came up with a build error every time.

Iterate over your string and set each character to it's lowercase variant if it's a letter, and if you don't want numbers, throw an error if that character is a digit.


I didn't entirely mean errors due to coding issues, this particular program uses user input to navigate to different parts of the program, and passing numbers through where they should pass letters has very odd effects, in respect to where they end up.


Uh... how do you implement that? Whether there are numbers or not really shouldn't matter, a string is a string after all...
[quote]I didn't entirely mean errors due to coding issues, this particular program uses user input to navigate to different parts of the program, and passing numbers through where they should pass letters has very odd effects, in respect to where they end up.


Uh... how do you implement that? Whether there are numbers or not really shouldn't matter, a string is a string after all...[/quote]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// Example on how to do that (roughly)

void Navigation ()
{
   if (StringTemp=="home")
   {
      main();
      return;
   }
   else if (StringTemp=="somekeyword")
   {
      somefunction();
      return;
   }
// Add some more options here
   else
   {
      cout << "\n\nError: Please Enter A Valid Option";
      system("cls");
      return;
   }
}   
/* Whenever you ask for user input during program run(like having main display a main menu with different options, or areas of the program), you then call the navigation function, then based on the contents of string StringTemp, you call a different function to navigate through the program. Also, I added a return statement after each function call, because if not it usually goes to main() when you try to close it by passing return from main(). I think it is due to some kind of weird backtracing of all of the different function calls, but with the returns put in it works fine. */


When the program goes to the navigation function, while StringTemp has numerical values, it can end up going off to parts of the program it shouldn't. One of the earlier versions ended up in my built-in debugging section, when trying to go back to the main menu (or something like that, I don't entirely remember), so I wanted to know how to stop numbers from being allowed through.

Iterate over your string and set each character to it's lowercase variant if it's a letter, and if you don't want numbers, throw an error if that character is a digit.


Exactly how would I go about doing that?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <algorithm>
#include <cctype>
#include <functional>
#include <string>

std::string tolower( const std::string& s )
  {
  std::string result( s );
  std::transform( result.begin(), result.end(), result.begin(), std::ptr_fun <int, int> ( std::tolower ) );
  return result;
  }

bool isalpha( const std::string& s )
  {
  return std::count_if( s.begin(), s.end(), std::not1( std::ptr_fun <int, int> ( std::isalpha ) ) );
  }

Code not tested... (I may have made a mistake.)
Hope this helps.
Topic archived. No new replies allowed.