Skip to content

Instantly share code, notes, and snippets.

@RezaBidar
Last active November 17, 2015 16:05
Show Gist options
  • Save RezaBidar/afacc88907b361843c93 to your computer and use it in GitHub Desktop.
Save RezaBidar/afacc88907b361843c93 to your computer and use it in GitHub Desktop.
write a program that get student information and validate inputs . if inputs was valid show success message otherwise show correct error message .
#include<iostream>
#include<string>
using namespace std;
bool isNameValid(string name){
int length = name.length(); // name.size() ; strlen(name.c_str);
if (length > 40 || length < 5)
return false;
else
return true;
}
bool isStudentIdValid(string studentId){
int length = studentId.size();
if (length == 9)
return true;
else
return false;
}
bool isAgeValid(int age){
if (age > 120 || age < 17)
return false;
else
return true;
}
int main(){
string studentId, name;
int age;
bool wrongInputFlag = false;
// get inputs
cin >> name >> studentId >> age;
// validation inputs
// name's length should be between 5 and 40
if (!isNameValid(name)){
cout << "Your name is incorrect" << endl;
wrongInputFlag = true;
}
// studentId's length shoudl be exactly 9 character
if (!isStudentIdValid(studentId)){
cout << "Your student id is incorrect" << endl;
wrongInputFlag = true;
}
// age should be between 17 and 120
if (!isAgeValid(age)){
cout << "Your age is incorrect" << endl;
wrongInputFlag = true;
}
// show errors if inputs was incorrect
// if all inputs was correct show success message
if (wrongInputFlag == false)
cout << "Your information saved successfully" << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment