I need to validate the moduleCode by a do...while loop.
But my code does'nt exit the loop - Not sure if I did right thing by declaring the string ABC111 before the loop - please help urgently

using namespace std;
// function inputAndValidate should be inserted here
void inputAndValidate(string &moduleCode,int &numberHours)
{
string ABC111,DEF112,XYZ113;
do
{
cout << " Enter module code: ";
cin >> moduleCode;
}

while ((moduleCode!=ABC111) || (moduleCode!=DEF112) || (moduleCode!=XYZ113));
// while (moduleCode != ABC111);
// ||(moduleCode != DEF112) || (moduleCode != XYZ113));

cout << " Enter number of hours: ";
cin >> numberHours;

}
int main ()
{
string moduleCode;
int numberHours;

inputAndValidate(moduleCode, numberHours);

cout << " The module code is " << moduleCode;
cout << " and the number of hours is " << numberHours << endl;

return 0;
}

Your ABsomething has no value. so when compared to your moduleCode it doesnt match your condition so it loops forever

I need to validate the moduleCode by a do...while loop.
But my code does'nt exit the loop - Not sure if I did right thing by declaring the string ABC111 before the loop - please help urgently

using namespace std;
// function inputAndValidate should be inserted here
void inputAndValidate(string &moduleCode,int &numberHours)
{
string ABC111,DEF112,XYZ113;
do
{
cout << " Enter module code: ";
cin >> moduleCode;
}

while ((moduleCode!=ABC111) || (moduleCode!=DEF112) || (moduleCode!=XYZ113));
// while (moduleCode != ABC111);
// ||(moduleCode != DEF112) || (moduleCode != XYZ113));

cout << " Enter number of hours: ";
cin >> numberHours;

}
int main ()
{
string moduleCode;
int numberHours;

inputAndValidate(moduleCode, numberHours);

cout << " The module code is " << moduleCode;
cout << " and the number of hours is " << numberHours << endl;

return 0;
}

The while condition was messing me around - got it right finally -thanks to y'all

This article has been dead for over six months. Start a new discussion instead.