validation of the data. (You could have set up the condition differently, for example
(!( (code == "ABC111") || (code == "DEF112") || (code == "XYZ113")))
The two value parameters in the function header of updateCorrectTotal contain the values of the specific module code and the number of hours that should be added to one of the totals. These values will not be changed inside the function. However, the three totals are all reference parameters (note the &) since their values may be changed in the function and then should be available in the main function. The body of the function involves the checking of the module code to determine which total should be updated and the addition of the hours to the correct total.
The main function contains a for loop where the two functions written above are called. When the loop has been executed NUMBER times, the totals are displayed.
It does compile but my do loop is not working properly and it does not ask me about the time – WHY?????
#include <iostream>
#include <string>
using namespace std;
const int NUMBER = 2;
//Function inputAndValidate
void inputAndValidate(string & code, int & hours)
{
do
{ //Start of do loop
cout << "Enter a module code: ";
cin >> code;
} while ((code != "ABC111" ) || (code != "DEF112" )|| (code != "XYZ113" ) || (code != "abc111" ) || (code != "def112" )|| (code != "xyz113" )); //Condition of loop
cout << "Enter number of hours: ";
cin >> hours;
} //End of Void function
//Function inputAndValidate
void updateCorrectTotal(string code, int hours,int & totalABC, int & totalDEF, int & totalXYZ)
{ //Beginning of void function
if (code == "ABC111")
totalABC += hours;
else if (code == "DEF112")
totalDEF += hours;
else
totalXYZ += hours;
} //End of void function
int main( )
{
string moduleCode;
int numberHours, totABC, totDEF, totXYZ;
// Counters
totABC = 0;
totDEF = 0;
totXYZ = 0;
// loop
for (int i = 1; i <= NUMBER; i++)
{
//Void inputAndValidate
inputAndValidate(moduleCode, numberHours);
//Void updateCorrectTotal
updateCorrectTotal(moduleCode, numberHours, totABC, totDEF, totXYZ);
} //End of loop
// Display
cout <<"ABC111" << endl;
cout << "DEF112" << endl;
cout << "XYZ113" << endl;
cout << totABC << endl;
cout << totDEF << endl;
cout << totXYZ << endl;
return 0;
}

New Topic/Question
Reply



MultiQuote




|