Write your question here.
I want for any user to put in only 9 digits and if its less than nine digits or more than nine digits, i want for my program to redirect them to input only nine digits before going through the rest of the program. I know it requires a loop but in my class we haven't covered more advanced issues
Put the code you need help with here.
cout << "PLEASE ENTER THE PATIENT 9 DIGIT MEDICAL RECORD NUMBER:" << endl;
cin >> mrn;
if (mrn <= 111111111)
{
cout << "Please enter a nine digit medical record number." << endl;
}
if (mrn >= 999999999)
{
cout << "Please enter a nine digital medical record number." << endl;
}
#include <iostream>
usingnamespace std;
void medical(int mrn) {
if (mrn >= 100000000 && mrn <= 999999999) {
cout << "entering 9-digit medical number was successful!\n";
}
else {
cout << "PLEASE ENTER THE PATIENT 9 DIGIT MEDICAL RECORD NUMBER, YOUR ENTERED NUMBER ISN'T 9-DIGIT ONE:\n";
cin >> mrn;
medical(mrn);
}
}
int main() {
int mrn;
cout << "PLEASE ENTER THE PATIENT 9 DIGIT MEDICAL RECORD NUMBER:\n";
cin >> mrn;
medical(mrn);
system("pause");
return 0;
}
the code above takes a number from user and using function medical, determines whether it's 9-digit one or not. if it is, prints success, if not, prompts the user to input another number and the function calls itself for the new number
#include <iostream>
usingnamespace std;
int main()
{
unsignedint mrn;
cout << "PLEASE ENTER THE PATIENT 9 DIGIT MEDICAL RECORD NUMBER:" << endl;
while (!(cin >> mrn) || (mrn < 100000000) || (mrn > 999999999))
{
cout << "Invalid input\nPlease enter a nine digit medical record number.\n";
cin.clear(); //. reset status flags
cin.ignore(1000, '\n'); // discard input until end of line
}
cout << "Thank you\nMedical Record Number: " << mrn << '\n';
}
Another possibility is to enter the user input as a string, check that the length is 9 characters, and that all of them are numeric. That would allow numbers such as "000000000". You might further add a check that the very first character of the string was not '0'.