I have been trying to find a solution for my school project. The program should display an invalid error if the user enters "Y Y". Currently the program still accepts it and moves on to the next question it considers the 2nd Y as an answer for the next question.
#include <iostream>
#include <string>
using namespace std;
bool response = true;
char repeatQuestion = true;
void patientRecordCreationDisplay (){
cout << "\t====================================================================" << endl;
cout << "\t| Patient Record Creation |" << endl;
cout << "\t====================================================================" << endl;
cout << "\n\n";
}
void patientWeight(){
string patWeight;
cout << "Enter Patient's Weight in kilograms: ";
getline (cin, patWeight);
}
void patientHeight(){
string patHeight;
cout << "Enter Patient's Height in centimeters: ";
getline (cin, patHeight);
cout << "\n\n";
}
void patientRecordCreationCurrent(string patientCurrentResponse[], string patientCurrentConditions[]){
cout << "Current Medical Status: \n " << endl;
for (int i = 0; i < 10; ++i) {
do{
cout << "Does the patient have " << patientCurrentConditions[i] << "? (Y/N) = ";
cin >> patientCurrentResponse[i];
if (patientCurrentResponse[i] == "Y" || patientCurrentResponse[i] == "y" || patientCurrentResponse[i] == "N" || patientCurrentResponse[i] == "n"){
break;
}
else if (patientCurrentResponse[i] != "Y" || patientCurrentResponse[i] != "y" || patientCurrentResponse[i] != "N" || patientCurrentResponse[i] != "n"){
cout << "Invalid Input. Y/N only" << endl;
response = false;
cout << "Do you want the program to repeat the question?\n[Y] will repeat the question.\t[N] will exit the program.\n[Y/N]: ";
cin >> repeatQuestion;
if (repeatQuestion == 'Y' || repeatQuestion == 'y'){
repeatQuestion = true;
response = true;
continue;
} else {
cout << "Goodbye" << endl;
repeatQuestion = false;
}
return;
}
} while (true);
}
}
void patientRecordCreationPast(string patientMedicalHistoryResponse[], string patientMedicalHistory[]){
if (response == true){
cout << "\n\nMedical History: \n " << endl;
for (int i = 0; i < 10; ++i) {
do {
cout << "Did the patient have " << patientMedicalHistory[i] << "? (Y/N) = ";
cin >> patientMedicalHistoryResponse[i];
if (patientMedicalHistoryResponse[i] == "Y" || patientMedicalHistoryResponse[i] == "y" || patientMedicalHistoryResponse[i] == "N" || patientMedicalHistoryResponse[i] == "n"){
break;
}
else if (patientMedicalHistoryResponse[i] != "Y" || patientMedicalHistoryResponse[i] != "y" || patientMedicalHistoryResponse[i] != "N" || patientMedicalHistoryResponse[i] != "n"){
cout << "Invalid Input. Y/N only" << endl;
response = false;
cout << "Do you want the program to repeat the question?\n[Y] will repeat the question.\t[N] will exit the program.\n[Y/N]: ";
cin >> repeatQuestion;
if (repeatQuestion == 'Y' || repeatQuestion == 'y'){
repeatQuestion = true;
response = true;
continue;
} else {
cout << "Goodbye" << endl;
repeatQuestion = false;
}
return;
}
} while (true);
}
}
}
int main (){
string patientCurrentConditions[10] = {
"Asthma", "Anemia", "High Blood Pressure", "Stroke","Seizures",
"Heart Attack", "Diabetes", "Cancer", "Fever", "Chicken Pox"};
string patientMedicalHistory[10] = {
"Asthma", "Anemia", "High Blood Pressure", "Stroke","Seizures",
"Heart Attack", "Diabetes", "Cancer", "Fever", "Chicken Pox"};
string patientCurrentResponse[10];
string patientMedicalHistoryResponse[10];
while (repeatQuestion == true){
patientRecordCreationDisplay ();
patientWeight();
patientHeight();
patientRecordCreationCurrent(patientCurrentResponse, patientCurrentConditions);
patientRecordCreationPast(patientMedicalHistoryResponse, patientMedicalHistory);
break;
}
return 0;
}
I have already tried using .length, .size and strlen and nothing works as of now. i'm already out of option. answers are appreciated!
cin stops at whitespace, so if the input is two or more words it will only put the first word in the string.
so instead use getline (cin, patientMedicalHistory[i]);
as that will account for whitespace and show the "Invalid Input. Y/N only" prompt.
although this solution may cause some conflict with cin >> repeatQuestion;
and repeat the invalid input prompt.