C++ Online Compiler
Example: Car Recommendation using Nested If-Else in C++
C
C++
C#
Java
Python
PHP
main.cpp
STDIN
Run
// Car Recommendation using Nested If-Else #include <iostream> #include <string> #include <limits> // Required for numeric_limits int main() { // Step 1: Declare variables to store user preferences int budget; std::string fuelType; int seatingCapacity; std::cout << "Welcome to the Car Recommendation System!\n"; // Step 2: Get budget input from the user 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(); // Clear error flag std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // Discard invalid input } std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // Consume the rest of the line // Step 3: Get fuel type input from the user std::cout << "Enter desired fuel type (petrol, diesel, electric): "; std::getline(std::cin, fuelType); // Step 4: Get seating capacity input from the user 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(); // Clear error flag std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // Discard invalid input } std::cout << "\n"; // Add a newline for better readability // Step 5: 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 6: Apply conditional logic to recommend a car std::string recommendedCar = "No specific recommendation found for your criteria."; if (budget < 20000) { if (fuelType == "petrol" && seatingCapacity >= 4) { recommendedCar = "A compact hatchback or entry-level sedan (e.g., 'City Commuter')."; } else if (fuelType == "electric" && seatingCapacity >= 2) { recommendedCar = "A small electric city car (e.g., 'Eco Pod')."; } } else if (budget >= 20000 && budget <= 40000) { if (fuelType == "petrol" && seatingCapacity >= 5) { recommendedCar = "A mid-range family sedan or compact SUV (e.g., 'Comfort Cruiser')."; } else if (fuelType == "diesel" && seatingCapacity >= 5) { recommendedCar = "A fuel-efficient diesel SUV for longer trips (e.g., 'Mileage Master')."; } else if (fuelType == "electric" && seatingCapacity >= 4) { recommendedCar = "A modern electric sedan or crossover (e.g., 'VoltStream 300')."; } } else { // budget > 40000 if (fuelType == "petrol" && seatingCapacity >= 5) { recommendedCar = "A premium sedan or a large SUV (e.g., 'Luxury Drive XL')."; } else if (fuelType == "electric" && seatingCapacity >= 5) { recommendedCar = "A high-performance luxury electric vehicle (e.g., 'ElectroStar Grand')."; } } std::cout << "Recommended Car: " << recommendedCar << "\n"; return 0; }
Output
Clear
ADVERTISEMENTS