Need to write c++ code that calls a function and validate the moduleCode with a do...while loop.

I ca'nt get the function to go out the loop
Pleez help

void inputAndValidate(string &moduleCode,int &numberHours)
{
string ABC111,DEF112,XYZ113;
do
{
cout << " Enter module code: ";
cin >> moduleCode;
}

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 4 Replies

your ABC111, DEF112, XYZ113 has no value...it will just continue to execute the do{} since the the while is giving off false boolean value...try giving it a value...
try this 1 and study the result...

void inputAndValidate(string &moduleCode,int &numberHours)
{
string ABC111 = "x";
do
{
cout << " Enter module code: ";
cin >> moduleCode;
}

while (moduleCode!=ABC111);
cout << " Enter number of hours: ";
cin >> numberHours;

}

enter "x" or any other value then study the result..

your ABC111, DEF112, XYZ113 has no value...it will just continue to execute the do{} since the the while is giving off false boolean value...try giving it a value...
try this 1 and study the result...

void inputAndValidate(string &moduleCode,int &numberHours)
{
string ABC111 = "x";
do
{
cout << " Enter module code: ";
cin >> moduleCode;
}
 
while (moduleCode!=ABC111);
cout << " Enter number of hours: ";
cin >> numberHours;
 
}

enter "x" or any other value then study the result..

Have assigned a value to the string but when I assign values to the whole lo (DEF112 and XYZ113) it does'nt go out of the loop.
With the above mentioned method where ABC111 is given a value x,
then my output display x instead of ABC111 - on the cout statement

void inputAndValidate(string &moduleCode,int &numberHours){
  do{
    cout << " Enter module code: ";
    cin >> moduleCode; 
  }while ((moduleCode!="ABC111") || (moduleCode!="DEF112") || (moduleCode!="XYZ113"));
  cout << " Enter number of hours: ";
  cin >> numberHours;
}

Have you tried something like this? this way, it will loop until the user enters ABC111, DEF112 or XYZ113...
Notice, I removed your declaration for the 3 local variables and the function is comparing the user-entered value in moduleCode against literal strings...
(BTW, I haven't compiled this yet, but I think this is what you were looking for)
Hope that helps...

Have assigned a value to the string but when I assign values to the whole lo (DEF112 and XYZ113) it does'nt go out of the loop.
With the above mentioned method where ABC111 is given a value x,
then my output display x instead of ABC111 - on the cout statement

oh, you mean the ABC111 blah blah are the module thingies, i thought they were variables...

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.