Lab8 – Bicycle4

You continue with project ‘Bicycle’, to test some more specific
aspects of project implementation:
- start implementing a Bicycle-menu-driven code;
- add an option to continue with another selection;
- create an ‘enum’/’switch’ implementation.
1. Restart solution ‘Bicycle’.
2. Build and run project ‘Bicycle’ of Lab7 to make sure the code is
still error-free.
3. To have only the new code active, comment out in ‘main()’ the code
already implemented and tested so far.

Copy the following statements from the existing commented out code:
cout << "Set the current-speed using user-input\n";
// function from Lab6
// Get the current-speed
cout << "Current speed in main() = " << GetSpeed()
<< "\n\n";
// GetSpeed() from Lab1

and add them to ‘main()’, as just the basic starting code for Lab8.
4. Build and run: current speed is set by user.
Implement a new function : ‘GetSpeedAccessCounter()’
5. Add a global variable
size_t speedAccessCounter;
and function ‘GetSpeed() to increment it at every access.

Add the prototype and implement function GetSpeedAccessCounter().
Call it at the end of ‘main()’ and display the counter value.
Add menu-application code
6. Ready now to add a new function to ‘Bicycle’ project, to handle
a menu selection.
Function ‘void MenuSelection()’, when called from ‘main()’, will
call itself three other functions, for:
- displaying the menu : void DisplayMenu()
- getting the selection : void GetSelection()
- implementing the selection : void ImplementSelection()
Implement first the function ‘void DisplayMenu()’
7. Update the list of function-prototypes with the new one, for
function ‘void DisplayMenu()’.
8. Implement function ‘DisplayMenu()’ to display the Bicycle menu on
the screen as:
Selection Menu
-------------1. Set Speed
2. Get Speed
3. Get MAXSpeed
4. Get MINSpeed
5. GetSpeedAccessCounter
6. Exit
Your selection :
Implement two versions: the classical ‘cascaded cout’ and also the
‘raw string’ style.

9. For testing, call function ‘DisplayMenu()’ from ‘main()’.
Build and Run (use one menu-code-version at a time).
Implement the top-level function ‘void MenuSelection()’
10. Update the function-prototypes with ‘void MenuSelection()’.
11. Implement ‘MenuSelection()’ to call first just ‘DisplayMenu()’.
Update code inside main()
12. Inside ‘main()’, comment out the call to ‘DisplayMenu()’ and
add instead a call to ‘MenuSelection()’.
13. Build and run. Menu will be displayed.
Add function ‘GetSelection()’
14. Add a new function prototype
void GetSelection()
15. Function ‘GetSelection()’ to get the user’s selection in a
‘size_t’ type local variable, and store it, if legal, in a
global variable ‘selection’.
Add to ‘Bicycle2Declarations.h’ the global variable
size_t selection{};
16. Implement ‘GetSelection()’ to handle potential illegal-int-input
(exceptions), by using two versions:
- good() only, and
- try/catch

This function simply sets the legal global variable ‘selection’.
In order to stop getting input from user after three illegalinputs:
- declare a ‘size_t’ counter above ‘while’;
- increment the counter before the ‘continue’ statements;
- and ckeck at the top of ‘while’:
if counter == 3
display : Selection set to Exit !\n";
selection = 6;
// exit
and break
17. For syntax check, implement one version at a time; and Build.
Function is not called yet from ‘main()’.
18. Add the call to ‘GetSelection()’ inside ‘MenuSelection()’, after
the call to ‘DisplayMenu()’.
19. Build. Error : ‘GetSelection()’ is undefined !
And this is because compiler needs the code of function
‘GetSelection()’ to be located above the code for
20. Rearrange the code : error is fixed.
21. Test the call with legal and illegal menu-selections.
Insert Header file "BicyclePrototypes.h"
22. To avoid having the above architectural requirement, a much
better approach is to insert the header file "BicyclePrototypes.h"
at the top of the file.
For testing, move back the code of ‘GetSelection()’.
No error !
23. You already have the same "#include BicyclePrototypes.h" above

There is no conflict by inserting it in this file also, because
it describes the function-prototypes.
But, you can’t insert "BicycleDeclarations.h" twice, because
you can’t declare the data-variables twice.
Add function ‘ImplementSelection()’
24. Add the prototype
void ImplementSelection()
This function to call the selected functions using a ‘switch’
construct on variable ‘selection’, for all ‘cases’ 1-5 (selection
6, ‘Exit’, to be processed by ‘main()’).
25. ‘Build’ and check the syntax.
‘ImplementSelection()’ is not called yet from ‘MenuSelection()’.
Update ‘MenuSelection()’
26. Add the call to ‘ImplementSelection()’ inside ‘MenuSelection()’,
after the call to ‘GetSelection()’, but only when ‘selection’ is
not 6.
27. Build and run.
Try all menu selections, one at a time.
When you select ‘GetSpeed()’, you get the last value set by the
code in ‘main()’.
Update again function ‘MenuSelection()’
28. In order the ‘selection’ to be known in ‘main()’, the current
implementation of function ‘void MenuSelection()’ must be
changed, to return the ‘selection’:

- add the prototype ‘size_t MenuSelection()’ and comment
out the first one ‘void MenuSelection()’;
- copy the existing code of ‘MenuSelection()’ and comment out
the one already used;
- change the header to ‘size_t’ and add the return statement
return selection;
Update ‘main()’
29. Comment out the existing call to ‘MenuSelection()’.
The new call to ‘MenuSelection()’, in ‘main()’, must now store
the returned selection, in a local variable’, like
size_t menuSelection = MenuSelection();
For debugging, display the ‘menuSelection’ in ‘main()’.
30. Build and run.
Check if user wants to continue with another selection
31. Add a prototype for a new function
bool CheckContinue()

function code will:
use a while(true) loop;
ask the user to continue Y, or no N;
use get(), toupper(), and handle illegal input;
return status true/false.
Insert a counter and increment it inside the illegal
for counter=3, set selection N and break.

32. Build’, to check the syntax.
‘CheckContinue()’ is not called yet from ‘main()’.
Update ‘main()’
33. Inside ‘main()’, add the call:
bool status = CheckContinue();
but only when the ‘menuSelection’ is not 6 (Exit).
And display the status using manipulator ‘boolalpha’ (for
debugging for now).
Otherwise, for menu-selection 6, display “Goodbye”.
34. ‘Build’ and run, to check the syntax and the logic for code
implemented so far.
The existing code doesn’t continue yet with another selection.
Redisplay the Menu when the user wants to continue
35. Back to ‘main()’ to rearrange the code to permit the user to
continue with another selection from the ‘Menu’.
Comment out the last two sections of code:
- the call to MenuSelection(), and
- the following if/else
and insert both in a ‘while(true)’ loop.
After the call to MenuSelection():

if(menuSelection != 6)
// selection was not Exit
if returned ‘status’ of CheckContinue() is ‘true’
clear the screen
‘continue’; // calls MenuSelection(), displays Menu
else // Exit




And after the loop, keep the “Goodbye”.
36. ‘Build’ and run.
Add function ‘void SelectModel()‘
37. Declare the prototype and implement a new function
void SelectModel()
to select the bike-model.
You may implement it as calls to three different functions,
but you may also implement it as one long function (not a
normal style !), with the following sections:
- first section, to display a menu for selection of a bike-model,
with names based on the enumerators of the enumeration:
enum BicycleModels
{ Road=1, Mountain, Racing, Folding, Electric };
Note: insert the above ‘enum’ in file ‘declarations’;
- second part, as a while(true) loop, to get from user the
bike-model selection (1-5);
for selection, use, for example, a variable like ‘size_t type’.
- check the input selection without using try-catch (just good());
store it if legal;

- last part, to declare a variable of ‘enum BicycleModels’:
BicycleModels bm = BicycleModels(type);
and implement a ‘switch(bm)’ to display the string-name of
all enumerators, like
case Road:
cout << "\nYou selected type " << “Road” << endl;
38. Build, to check the syntax.
Update ‘main()’
--------------39. Inside ‘main()’, call ‘SelectModel()’ before the exit ‘’Goodbye’.
Build and run.
40. Update the ‘Project-Header’ at the top of the source-file.
41. Save All and close solution.