I have written this block of code and I don't seem to understand why it is not working. I have tried using a while statement and if statement but that has not worked out so far.
When I input 100 for closing time or 100 for opening time it simply accepts them. Can anyone point out what I am doing wrong in the code and maybe a better method to write it since I will be using the same code for 3 different stations.
ofstream outputFile;
outputFile.open("SelangorOperationHours.txt");
int SelangorOpening; // changed to int instead of string
int SelangorClosing; // changed to int instead of string
do
{
cout << "Opening hour has to be an valide number which is greater then 0 and less then 24.\n\n";
cout << "Enter Opening Hours for Selangor Railway: ";
cin >> SelangorOpening;
cout << "Closing hour has to be an valide number which is greater then 12 and less then 24.\n\n";
cout << "Enter Closing Hours for Selangor Railway: ";
cin >> SelangorClosing;
} while (!(SelangorOpening > 0 && SelangorOpening < 24) && !(SelangorClosing > 12 && SelangorClosing < 24));
outputFile << "Opening hours for Selangor Railway: " << SelangorOpening << endl;
outputFile << "Closing hours for Selangor Railway: " << SelangorClosing << endl;
outputFile.close();
cout << "Successfully Registered!\n\n";
I've changed the && to || and in all three yet the issue still remains, I also mixed and matched. Which && are you referring to? The logic seems to make sense.
I looked over what you are trying to do and offer this suggestion:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
do
{
std::cout << "\nOpening hour has to be an valide number which is greater then 0 and less then 13.\n\n";
std::cout << "Enter Opening Hours for Selangor Railway: ";
std::cin >> SelangorOpening;
} while (SelangorOpening < 1 || SelangorOpening > 12);
do
{
std::cout << "\nClosing hour has to be an valide number which is greater then 12 and less then 24.\n\n";
std::cout << "Enter Closing Hours for Selangor Railway: ";
std::cin >> SelangorClosing;
} while (SelangorClosing < 13 || SelangorClosing > 23);
Trying to do everything at one time is a nice thought, but how do you know which one is wrong. Splitting it into two you are assured to get the right value for each variable.