🔒 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.

About this Thread

  • 20
    Replies
  • 1K
    Views
  • 5
    Participants
Last reply from:
kramsuiluj

Online now

Members online
882
Guests online
2,558
Total visitors
3,440

Forum statistics

Threads
2,276,451
Posts
28,970,168
Members
1,231,287
Latest member
altershit
Back
Top