0% found this document useful (0 votes)
11 views4 pages

Untitled Document

Uploaded by

farooqurwa33
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views4 pages

Untitled Document

Uploaded by

farooqurwa33
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

SWITCH STATEMENTS

NESTED SWITCH

#include <iostream>
using namespace std;
int main(){
int mainChoice, subChoice;
cout << "Main Menu:\n1. Food\n2. Drinks\n";
cin >> mainChoice;
switch(mainChoice) {
case 1: // Food Menu
cout << "1. Pizza\n2. Burger\n";
cin >> subChoice;
switch(subChoice) {
case 1: cout << "You chose Pizza"; break;
case 2: cout << "You chose Burger"; break;
}
break;
case 2: // Drinks Menu
cout << "1. Coffee\n2. Tea\n";
cin >> subChoice;
switch(subChoice) {
case 1: cout << "You chose Coffee"; break;
case 2: cout << "You chose Tea"; break;
}
break;
}
}

=======================================================
#include <iostream>
using namespace std;

int main() {
char operation;
float number1, number2;

// Prompt the user to enter an operator


cout << "Enter an operator (+, -, *, /): ";
cin >> operation;

// Prompt the user to enter two numbers


cout << "Enter two numbers: ";
cin >> number1 >> number2;

// Perform the selected operation


switch(operation) {
case '+':
cout << "Result: " << number1 + number2 << endl;
break;
case '-':
cout << "Result: " << number1 - number2 << endl;
break;
case '*':
cout << "Result: " << number1 * number2 << endl;
break;
case '/':
if (number2 != 0)
cout << "Result: " << number1 / number2 << endl;
else
cout << "Error: Division by zero is not allowed." << endl;
break;
default:
cout << "Invalid operator!" << endl;
break;
}

return 0;
}

=============================================
#include <iostream>
#include <string>

using namespace std;

int main() {
int choice;

cout << "Welcome to the Adventure Game!" << endl;


cout << "You are in a dark forest. You can see paths to the left and right." <<
endl;
cout << "What do you want to do?" << endl;
cout << "1. Go left" << endl;
cout << "2. Go right" << endl;
cout << "Enter your choice (1 or 2): ";
cin >> choice;

switch (choice) {
case 1: {
cout << "You decided to go left. You encounter a wild beast!" << endl;
cout << "What will you do?" << endl;
cout << "1. Fight the beast" << endl;
cout << "2. Run away" << endl;
cout << "Enter your choice (1 or 2): ";
cin >> choice;

switch (choice) {
case 1:
cout << "You bravely fought the beast and won! You found a treasure
chest!" << endl;
break;
case 2:
cout << "You ran away safely but missed a chance to find treasure."
<< endl;
break;
default:
cout << "Invalid choice! The beast caught you!" << endl;
}
break;
}
case 2: {
cout << "You decided to go right. You find a mysterious cave." << endl;
cout << "What will you do?" << endl;
cout << "1. Enter the cave" << endl;
cout << "2. Stay outside" << endl;
cout << "Enter your choice (1 or 2): ";
cin >> choice;

switch (choice) {
case 1:
cout << "Inside the cave, you find glowing crystals! You are now rich!"
<< endl;
break;
case 2:
cout << "You chose to stay outside, but a dragon appeared! You ran
away!" << endl;
break;
default:
cout << "Invalid choice! A dragon caught you!" << endl;
}
break;
}
default:
cout << "Invalid choice! You are lost in the forest!" << endl;
}

cout << "Thank you for playing the Adventure Game!" << endl;
return 0;
}

You might also like