Programs using Conditional and Loop statements and loops.
Aim
The aim is to demonstrate the practical application of the Loop structure (specifically the exit-controlled loop do...while) and the Selection structure (switch statement) to provide a repeated menu interface and execute different actions based on user input,.
Algorithm
Define a class (
ITEMS) that holds the data for items (code, price) and member functions (likegetitem(),remove(),displaySum()) to manipulate the data,.In the
main()function, create an object (order) of the class and initialize its item counter.Execute a
do...whileloop to repeatedly display the menu options (Add item, Display total, Delete item, Display all, Quit).Read the user's option into variable
x.Use a
switchstatement based on the value ofxto call the appropriate member function or handle invalid input.The loop terminates when the user selects option 5 (
Quit).
Program (Processing Shopping List)
#include <iostream>
using namespace std;
const int m = 50;
class ITEMS {
int itemCode[m];
float itemPrice[m];
int count;
public:
void CNT(void) { count = 0; }
void getitem(void);
void displaySum(void);
void remove(void);
void displayItems(void);
};
// --- Function Definitions ---
void ITEMS::getitem(void) {
if (count < m) {
cout << "Enter item code: ";
cin >> itemCode[count];
cout << "Enter item price: ";
cin >> itemPrice[count];
count++;
} else {
cout << "Inventory full!\n";
}
}
void ITEMS::displaySum(void) {
float sum = 0;
for (int i = 0; i < count; i++) {
sum += itemPrice[i];
}
cout << "\nTotal Value: " << sum << "\n";
}
void ITEMS::remove(void) {
int a;
cout << "Enter item code to delete: ";
cin >> a;
for (int i = 0; i < count; i++) {
if (itemCode[i] == a) {
itemPrice[i] = 0; // Simple logic: set price to 0 so it doesn't add to sum
cout << "Item removed (price set to 0).\n";
return;
}
}
cout << "Item code not found.\n";
}
void ITEMS::displayItems(void) {
cout << "\n--- Current Inventory ---\n";
cout << "Code\tPrice\n";
for (int i = 0; i < count; i++) {
cout << itemCode[i] << "\t" << itemPrice[i] << "\n";
}
}
// --- Main remains the same as your provided structure ---
int main() {
ITEMS order;
order.CNT();
int x;
do {
cout << "\n1 : Add an item";
cout << "\n2 : Display total value";
cout << "\n3 : Delete an item";
cout << "\n4 : Display all items";
cout << "\n5 : Quit";
cout << "\n\nWhat is your option? ";
cin >> x;
switch(x) {
case 1 : order.getitem(); break;
case 2 : order.displaySum(); break;
case 3 : order.remove(); break;
case 4 : order.displayItems(); break;
case 5 : break;
default : cout << " Error in input; try again\n";
}
} while(x != 5);
return 0;
}
Output
(Demonstrating the iterative menu flow and selection)
You can do the following; Enter appropriate number
1 : Add an item
2 : Display total value
3 : Delete an item
4 : Display all items
5 : Quit
What is your option?1
Enter item code :111
Enter item cost :100
You can do the following; Enter appropriate number
...
What is your option?2
Total value :100
You can do the following; Enter appropriate number
...
What is your option?5
Result and Discussion
This program successfully utilized the core C++ control structures to manage the flow of the application.
Looping (Iteration): The entire user experience is controlled by the
do...whileloop. This structure is classified as an exit-controlled loop, meaning the body of the loop (displaying the menu and processing input) is executed at least once before the termination condition (x != 5) is checked,.Selection (Branching): The
switchstatement handles the program's necessary two-way or multiple branching (multi-way branching) to jump to the appropriate block of code based on the user's choice (stored inx),,. This ensures that only the required task function is executed per iteration.
The combination of do...while and switch statements creates a robust and reusable system interface, illustrating the foundational elements of structured programming maintained within C++.