C++ Online Compiler
Example: Car Recommendation using Switch and If-Else in C++
C
C++
C#
Java
Python
PHP
main.cpp
STDIN
Run
// Car Recommendation using Switch and If-Else #include <iostream> #include <string> #include <limits> // Required for numeric_limits #include <algorithm> // Required for std::transform int main() { // Step 1: Declare variables int budget; std::string fuelType; int seatingCapacity; std::cout << "Welcome to the Advanced Car Recommendation System!\n"; // Step 2: Get user inputs with validation (similar to Approach 1) std::cout << "Please enter your maximum budget (in USD, e.g., 25000): "; while (!(std::cin >> budget) || budget <= 0) { std::cout << "Invalid budget. Please enter a positive number: "; std::cin.clear(); std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); } std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); std::cout << "Enter desired fuel type (petrol, diesel, electric): "; std::getline(std::cin, fuelType); // Convert fuelType to lowercase for consistent comparison std::transform(fuelType.begin(), fuelType.end(), fuelType.begin(), ::tolower); std::cout << "Enter minimum seating capacity (e.g., 4, 5, 7): "; while (!(std::cin >> seatingCapacity) || seatingCapacity <= 0) { std::cout << "Invalid seating capacity. Please enter a positive number: "; std::cin.clear(); std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); } std::cout << "\n"; // Step 3: Display user preferences std::cout << "Based on your preferences:\n"; std::cout << "Budget: $" << budget << "\n"; std::cout << "Fuel Type: " << fuelType << "\n"; std::cout << "Seating Capacity: " << seatingCapacity << "\n\n"; // Step 4: Apply conditional logic using switch for fuel type and if-else for other conditions std::string recommendedCar = "No specific recommendation found for your criteria."; if (budget <= 0 || seatingCapacity <= 0) { recommendedCar = "Please provide valid positive numbers for budget and seating capacity."; } else { switch (fuelType[0]) { // Use the first character for switch for simplicity case 'p': // petrol if (budget < 20000 && seatingCapacity >= 4) { recommendedCar = "A compact petrol hatchback (e.g., 'City Sprint')."; } else if (budget >= 20000 && budget <= 40000 && seatingCapacity >= 5) { recommendedCar = "A versatile petrol family sedan or SUV (e.g., 'Family Explorer')."; } else if (budget > 40000 && seatingCapacity >= 5) { recommendedCar = "A powerful petrol luxury car or performance SUV (e.g., 'Veloce Grand')."; } break; case 'd': // diesel if (budget >= 20000 && budget <= 50000 && seatingCapacity >= 5) { recommendedCar = "A fuel-efficient diesel SUV or sedan for long drives (e.g., 'EcoRoute')."; } else if (budget > 50000 && seatingCapacity >= 6) { recommendedCar = "A high-torque diesel luxury SUV or minivan (e.g., 'MegaTourer')."; } break; case 'e': // electric if (budget < 30000 && seatingCapacity >= 2) { recommendedCar = "A compact electric city car with good range (e.g., 'Spark EV')."; } else if (budget >= 30000 && budget <= 60000 && seatingCapacity >= 4) { recommendedCar = "A mid-range electric sedan or crossover (e.g., 'PowerGlide 400')."; } else if (budget > 60000 && seatingCapacity >= 5) { recommendedCar = "A premium long-range electric SUV or sedan (e.g., 'Electra Grandeur')."; } break; default: recommendedCar = "Unsupported fuel type. Please choose petrol, diesel, or electric."; break; } } std::cout << "Recommended Car: " << recommendedCar << "\n"; return 0; }
Output
Clear
ADVERTISEMENTS