🔒 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
1,096
Guests online
2,628
Total visitors
3,724

Forum statistics

Threads
2,294,126
Posts
29,088,847
Members
1,208,088
Latest member
Wiizzeee
Back
Top