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;
}

Recommended Answers

All 3 Replies

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

string ABC111="ABC111";

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.