🔒 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

Trending Topics

Online now

Members online
962
Guests online
6,791
Total visitors
7,753

Forum statistics

Threads
2,354,384
Posts
29,383,453
Members
1,079,436
Latest member
Yes it is me
Back
Top