C++ New Car example:

By: Nicholas Duchon

Links:

Following Option 2:

// File: newcar.cpp
// Date: Aug 4, 2014
// Author: Nicholas Duchon
// Purpose: select options for a new car

#import <iostream>
using namespace std;

// C++ Global variable
double basePrice, engineCost, trimCost, radioCost, shippingCharge, dealerCharge;

// C++: functions or modules BEFORE main!
void welcomeMessage () {
      cout << "Welcome to the New Car Price Calculator" << endl;
      cout << "Enter the base price, and codes for engine, trim and radio options" << endl;
      cout << "This program will determine the final cost of the car" << endl;
      cout << "including the shipping and dealer charges." << endl;
} // end welcomeMessage

void getBasePrice () {
   cout << "What is the base price for this model? ";
   cin >> basePrice;
} // end getBasePrice

void getEngineCost () {
     char code;
      cout <<  "Engine codes:" << endl;
      cout <<  "  S - 6 cylinder, $150" << endl;
      cout <<  "  E - 8 cylinder, $475" << endl;
      cout <<  "  D - Diesel, $750" << endl;
      cout <<  "Select your engine code (S, E, D): ";
      cin >> code;
      switch (code) {
      case 'S': engineCost = 150; break;
      case 'E': engineCost = 475; break;
      case 'D': engineCost = 750; break;
      default:
        cout <<  "Invalid selection, defaulting to 6 cylinder" << endl;
        engineCost = 150;
      } // End Case
} // end getEngineCost

void getTrimCost () {
     char code;
      cout << "Trim codes:" << endl;
      cout << "  V - vinyl, $50" << endl;
      cout << "  C - cloth, $225" << endl;
      cout << "  L - leather, $800" << endl;
      cout << "Select your trim code (V, C, L): ";
      cin >> code;
      switch (code) {
      case 'V': trimCost = 50; break;
      case 'C': trimCost = 225; break;
      case 'L': trimCost = 800; break;
      default:
        cout << "Invalid selection, defaulting to cloth";
        trimCost = 50;
      } // End Case
} // end getTrimCost

void getRadioCost () {
     char code;
      cout << "Radio codes:" << endl;
      cout << "  C - AM/FM/CD/DVD, $100" << endl;
      cout << "  P - with GPS, $400" << endl;
      cout << "Select your radio code (C, P): ";
      cin >> code;
      switch (code) {
      case 'C': radioCost = 100; break;
      case 'P': radioCost = 400; break;
      default:
        cout << "Invalid selection, defaulting to AM/FM/CD/DVD" << endl;
        radioCost = 100;
      } // End Case
} // end getRadioCost

void displaySellingPrice () {
   double sellingPrice = basePrice + engineCost + trimCost + radioCost + shippingCharge + dealerCharge;
   printf ("The total cost of this case is: $%.2f\n", sellingPrice);
} // end displaySellingPrice

int main () {
   shippingCharge = 500;
   dealerCharge   = 175;
  
   welcomeMessage ();
   getBasePrice ();
   getEngineCost ();
   getTrimCost ();
   getRadioCost ();
   displaySellingPrice ();
   cout << "Bye" << endl;
} // end main