What's new

Closed C++ User input

Status
Not open for further replies.
Ah ok sorry, i thought you mean pano kung yung name na ieenter may integer, yes validation is extremely important and i am well aware of that, and that is what im trying to say, so were on the same side. Well its programming so there's a lot of ways to do that validation like this:
C++:
#include<iostream>
#include<string>
using namespace std;

string getName()
{
   const string notAllowed = "0123456789!?~#@{}|£$%^&*+";
   const string notOnly = " ";
  
   string name;
   cout << "Enter employee name: ";
   getline( cin, name );
  
   bool wrong = name.length() <= 1
             || name.find_first_of( notAllowed ) != string::npos
             || name.find_first_not_of( notOnly ) == string::npos
            
             ;
  
   return wrong ? getName() : name;
}


int main()
{
   string name = getName();
   cout << name << '\n';
}
Im sure there are easier ways to do this, but im sure in his current level he wouldn't be able to absorb all of this.
 
Status
Not open for further replies.

Similar threads

Back
Top