1

I'm currently working on a program and thinking if it is possible to implement another restrictions for the user input. The restrictions that I made as of now is that the user is only allow to input alpha and spaces, hitting enter without any input will not be also accepted.

cout<<"Input customer's lastname\t\t: ";
getline(cin,lname);
if(lname.find_first_not_of("abcdefghijklmnopqrstuvwxyz ")!=string::npos)
{
    cout<<"You can only input alpha here!\n";
    cin.clear();
    goto p1;
}
else if(lname.empty())
{
    cout<<"Please enter your lastname!\n";
    cin.clear();
    goto p1;
}

The another restrictions that I want is if the user input is all spaces, the program will also show a message. Second, I wonder if it's possible to detect the input if the user typed it properly like (de la Cruz) the words should be only separated by one space and if not another message will show. I cant think on how to do it, I already did some research but I couldn't found any similar to this with C++. I don't know if this is possible since I'm just starting to learn C++, or maybe I don't have enough logic at all. :(

vnylng
  • 65
  • 6

2 Answers2

0

Create a function to check whether the input is valid. Use the function in a while loop.

bools isInputValid(std::string const& input)
{
   // add all the checks
}

Use it as:

std::cout << "Enter input\n";
while ( getline(std::cout, line) )
{
   if ( isInputValid(line) )
   {
      break;
   }
   std::cout << "Input is not vaild. Try again\n";
}
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • Thanks for answering, but I'm asking on how to construct or make the last two restrictions that I wanted to add. I don't have any idea on how to make those things works with C++, that's why I'm asking if you have any logical idea for that. – vnylng Oct 19 '15 at 08:48
0

A little help from me on checking for spaces.

bool has_only_spaces(const std::string& str) 
{
   return str.find_first_not_of (' ') == str.npos;
}

bool has_two_consecutive_spaces(const std::string& str)
{
    for (unsigned int i = 1; i < str.length(); i++)
    {
        if ((str[i - 1] == str[i]) && (str[i] == ' '))
        {
            return true;
        }
    }
    return false;
}

int main()
{
  std::string lname;
  std::cout << "Input customer's last name: ";
  getline(std::cin, lname);
  if (has_only_spaces(lname) || has_two_consecutive_spaces(lname))
  {
      std::cout << "Not a valid input" << std::endl;
      std::cin.clear();
  }  
}
  • Thanks, I was able to do it by counting the length of the input and comparing it to the spaces detected by (isspace) using loop, if match then its not valid. I'm now working on the one space rule between two words. – vnylng Oct 19 '15 at 11:23
  • The function 'has_two_consecutive_spaces' checks if there are more than 1 spaces between words. If yes it returns true, else it returns false. –  Oct 19 '15 at 11:27
  • The idea of detecting two consecutive spaces and the logic of the if statement helped me. Thanks man! – vnylng Oct 19 '15 at 12:17
  • I'm glad that I helped. –  Oct 19 '15 at 12:24