User Input Validation
Nov 2, 2019 at 11:59pm Nov 2, 2019 at 11:59pm UTC
User Input Validation is frustrating. Is there a way I can make sure that spaces do not mess up my cin >> (a string here) from user and char input does not mess up cin >> (int here please) input? I have a few c++ books I can read. If I can just get advice on what to search or read about when it comes to being able to cleanly re-prompt a user to cout << "Hey thats invalid input, Try again PLEASE" << endl;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
#include <iostream>
#include <string>
#include "PatientRecord.h"
#include "Queue.h"
using namespace std;
int PatientRecord::patientRecordCount = 0;
int main()
{
int a;// An integer to control the Do-While Loop
// string and int variables for patient registration
string last;
string first;
string bDay;
string stuff;
int iUrgency;
PQueue <PatientRecord> hospital;
PatientRecord patientInfo;
do {
cout << "Please choose one of the following:" << endl;
cout << "+------------------------------+\n" ;
cout << "| 1. Register a Patient |\n" ;
cout << "| 2. Display Patient Queue |\n" ;
cout << "| 3. Remove a Patient |\n" ;
cout << "| 4. Exit Registration Program |\n" ;
cout << "+------------------------------+\n" ;
cin >> a;
getchar();
switch (a)
{
case 1:
cout << "Enter patients last " << endl;
cin >> last;
getchar();
cout << "Enter patients first " << endl;
cin >> first;
getchar();
cout << "Enter patients birth day" << endl;
cin >> bDay;
getchar();
cout << "Describe symptoms" << endl;
getline(cin, stuff); cout << endl;
cout << "Enter priority" << endl;
cin >> iUrgency;
// Initialized the Patients Information using vRegisterPatient()
patientInfo.vRegisterPatient(last, first, bDay, stuff, iUrgency);
// Added the Patient to the PriorityQueue
hospital.enqueue(patientInfo);
break ;
case 2:
hospital.displayQueue();
cout << "====There are currently " << PatientRecord::patientRecordCount << " Patients in the Queue====" << endl;
break ;
case 3:
hospital.dequeue();
PatientRecord::patientRecordCount--;
break ;
case 4:
cout << "You choose to exit the program." << endl;
break ;
default :
cout << "Invalid input, please enter 1, 2, 3., or 4" << endl;
};
} while (a != 4);
//system("pause");
return 0;
}
Nov 3, 2019 at 1:30am Nov 3, 2019 at 1:30am UTC
Get all input as a string, then parse the string.
Use a trim function to remove leading and trailing spaces. Boost String Algorithms has one. And you can find one on the FAQ here:
http://www.cplusplus.com/faq/sequences/strings/trim/
Small functions help a lot.
Hope this helps.
Nov 3, 2019 at 6:22pm Nov 3, 2019 at 6:22pm UTC
I will take a look at that in a few mins. Thank you very much. I was thinking to
1. Save user input into a string
2. Take each char out and place it in a char array
3. add what slashes or dashes I need following what ever char
4. Then display
Topic archived. No new replies allowed.