Comparing char for validation in C++
Posted
by
Corey Starbird
on Stack Overflow
See other posts from Stack Overflow
or by Corey Starbird
Published on 2013-11-01T03:50:38Z
Indexed on
2013/11/01
3:53 UTC
Read the original article
Hit count: 133
c++
|data-validation
/*
PROGRAM: Ch6_14.cpp
Written by Corey Starbird
This program calculates the balance
owed to a hospital for a patient.
Last modified: 10/28/13
*/
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
// Prototypes for In-patient and Out-patient functions.
double stayTotal (int, double, double, double); // For In-patients
double stayTotal (double, double); // For Out-patients
int main()
{
char patientType; // In-patient (I or i) or Out-patient (O or o)
double rate, // Daily rate for the In-patient stay
servCharge, // Service charge for the stay
medCharge, // Medication charge for the stay
inTotal, // Total for the In-patient stay
outTotal; // Total for the Out-patient stay
int days; // Number of days for the In-patient stay
// Find out if they were an In-patient or an Out-patient
cout << "Welcome, please enter (I) for an In-patient or (O) for an Out-patient:" << endl;
cin >> patientType;
while (patientType != 'I' || 'i' || 'O' || 'o')
{
cout << "Invalid entry. Please enter either (I) for an In-patient or (O) for an Out-patient:" << endl;
cin >> patientType;
}
cout << "FIN";
return 0;
}
Hey, brand new to C++ here. I am working on a project and I'm having trouble figuring out why my validation for patientType
isn't working properly. I first had double quotes, but realized that would denote strings. I changed them to single quotes, my program will compile and run now, but the while loop runs no matter what I enter, I, i, O, o, or anything else.
I don't know why the while loop isn't checking the condition, seeing that I did enter one of the characters in the condition, and move on to cout. Probably a simple mistake, but I thank you in advance.
© Stack Overflow or respective owner