Bajade Labtask5
Bajade Labtask5
1.)How many sets of switch structures were used in the program? What was the data type
used in the selection structure that allows input letters?
The program uses one switch structure. The data type used in the selection structure to
allow input letters is `char`.
2.)In the main menu selection, if the expression to be inputted is a number instead of
characters, which portion of the program needs to change
If the input should be a number instead of characters, you would need to change how the
`option` variable is handled. Specifically, you need to change the `option` variable from
`char` to `int`, and update the `switch` cases to handle integers. Here’s how the
modified portion of the code would look:
```cpp
Int main() {
Int option;
While (true) {
If (option == 4) {
Break;
Switch (option) {
Case 1: {
Double side;
Cout << “The perimeter of a square with a side measurement of “ << side << “
inches is “ << perimeter << “ inches” << endl;
Break;
Case 2: {
Cout << “The area of the trapezoid with base measurements “ << b1 << “ and “ <<
b2 << “ and height “ << height << “ is “ << area << “ square inches” << endl;
Break;
Case 3: {
Double side;
Cout << “The surface area of the cube with a side measurement of “ << side << “
inches is “ << surfaceArea << “ square inches” << endl;
Break;
Default:
}
Char back;
Cout << “Press <K/k> to go back to the main menu or any other key to exit: “;
Break;
Return 0;
```
If the `default` statement is omitted in a `switch` structure, nothing will happen if none of
the cases match the Input. The program will simply exit the switch block without executing
any statements. This can lead to situations where invalid input is not properly handled or
no feedback is given to the user.
4.)How do you enable the user to input either lowercase `a` or uppercase `A`? Write the
complete C++ code.
My current program already allows for both lowercase `a` and uppercase `A` because it
handles both cases in the `switch` statement.
```cpp
#include <iostream>
#include <iomanip>
Int main() {
Char option;
While (true) {
Break;
Switch (option) {
Case ‘A’:
Case ‘a’: {
Double side;
Cout << “The perimeter of a square with a side measurement of “ << side << “
inches is “ << perimeter << “ inches” << endl;
Break;
Case ‘B’:
Case ‘b’: {
Cout << “The area of the trapezoid with base measurements “ << b1 << “ and “ <<
b2 << “ and height “ << height << “ is “ << area << “ square inches” << endl;
Break;
}
Case ‘C’:
Case ‘c’: {
Double side;
Cout << “The surface area of the cube with a side measurement of “ << side << “
inches is “ << surfaceArea << “ square inches” << endl;
Break;
Default:
Char back;
Cout << “Press <K/k> to go back to the main menu or any other key to exit: “;
Break;
}
Return 0;
```
In this code, the `switch` statement allows for both lowercase and uppercase input for
each case option (`A/a`, `B/b`, `C/c`). This ensures that the user can enter either
lowercase or uppercase letters to select an option.
OBSERVATION
I observed that the program efficiently handles various geometric calculations such as
perimeter, area, and surface area based on user input. It uses a single `switch` structure
to manage user menu selections, allowing for both uppercase and lowercase letter inputs
(`char` type), enhancing user-friendliness. The program correctly handles input validation
by checking if the input scores are within the valid range of 0 to 100, ensuring robustness.
The `while` loop In the main function is well-implemented, allowing the menu to be
displayed repeatedly until the user decides to exit. Each geometric calculation is
encapsulated within a case block, and the use of `fixed` and `setprecision(2)` ensures
that the output is formatted to two decimal places, providing clear and precise results.
Additionally, the program prompts the user to return to the main menu or exit after each
operation, adding to the overall user experience. However, the repetition of input validation
code could be refactored into a separate function to improve readability and
maintainability. Overall, the program is functional and meets its objectives, with clear
output and user guidance.