Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

COP3014 C++....need the correct way to program this code using switch statements

ID: 669441 • Letter: C

Question

COP3014

C++....need the correct way to program this code using switch statements.

int main()

{

int quoteNumber = 1;

bool customerHappy = false; // are they happy with car?

int carSelection = 0; // used to capture selected car

double carCost = 0; // car cost alone

double upgradeCost = 0; // upgrade cost alone

double totalCost = 0; // car plus upgrades cost

// You are going to DOOO something until the customer has

// selected a car and is happy

do

{

// if(true) will ALWAYS be activated

// TODO: Make this loop run if we haven't selected a car

// yet - get the customer to select one

// if carSelection == 0, then they haven't picked a car yet (hint hint)

if (true)

{

std::cout << "Welcome to Bob's used car lot and chop shop!" << " ";

std::cout << "As you can see we have quite a few to choose from!" << " ";

std::cout << "Which one would you like?:" << " ";

std::cout << "[1] 2005 Vokswagen Beetle ($8,000)" << " ";

std::cout << "[2] 2008 Pontiac G6 ($8,581)" << " ";

std::cout << "[3] 2004 Chevy S-10 ($10,500)" << " ";

std::cout << "[4] 2016 Jeep Patriot ($15,209)" << " ";

std::cout << "[5] 2012 Jeep Wrangler Sport ($24,390)" << " ";

std::cout << "Which car would you like?: ";

std::cin >> carSelection;

// TODO: If the user makes a bad selection, tell them

// TODO: Else, set the carCost and move on to upgrades

// ----------------------------------------------

// Put your code in here! --> hint: use a switch

// ----------------------------------------------

// switch(carSelection)...

// ----------------------------------------------

// This newline will create a space between menus

std::cout << " ";

}

else // Otherwise try to sell them some upgrades!

{

int upgradeSelection = 0; // The users menu choice

int engineLevel = 0; // the upgrade level of engine (0-5 only)

int tireLevel = 0; // the upgrade level of tire (0-5 only)

int rimLevel = 0; // the upgrade level of rims (0-5 only)

int paintLevel = 0; // the upgrade level of paint (0-5 only)

int mufflerLevel = 0; // the upgrade level of muffler (0-5 only)

// while(true) will NEVER stop running

// TODO: Make this while loop exit based on user input

while (true)

{

/////////////////////////////////////////////////////

// TODO: Calculate all of the upgrade costs here

/////////////////////////////////////////////////////

// Upgrades are $100 per level plus the cost of lower upgrades

// Example: Level 1 upgrade is $100

// Example: Level 2 upgrade is $100 + $200 = $300

// Example: Level 3 upgrade is $100 + $200 + $300 = $600

// etc etc etc etc etc etc etc etc etc etc etc etc etc...

// Hint: You might find a for-loop helpful for accumulating an amount

upgradeCost = 0;

//////////////////////////////////////////////////////

// Put your code below each of the comments marked

// for each upgrade type

//////////////////////////////////////////////////////

// Calculate upgrade cost for engine

// i.e. Put code here for engine...

// Calculate upgrade cost for tires

// i.e. Put code here for tires...

// Calculate upgrade cost for rims

// etc

// Calculate upgrade cost for paint

// Calculate upgrade cost for muffler

//////////////////////////////////////////////////////

//////////////////////////////////////////////////////

// TODO: Make the quote lines below print to look like money

// Example 3.45234 should be --> 3.45

// Think about iomanip

// ---------------------------------------

// Put some code in here

// ---------------------------------------

// ---------------------------------------

// ============================================================

// Don't mess with these lines ================================

// ============================================================

std::cout << "[" << quoteNumber++ << "] "; // Don't touch me

std::cout << " Car($" << carCost << ")"; // Don't touch me

std::cout << " E(" << engineLevel << ")"; // Don't touch me

std::cout << " T(" << tireLevel << ")"; // Don't touch me

std::cout << " R(" << rimLevel << ")"; // Don't touch me

std::cout << " P(" << paintLevel << ")"; // Don't touch me

std::cout << " M(" << mufflerLevel << ")"; // Don't touch me

// Don't touch the following line -- HOWEVER, you should put

// something in the space ABOVE to make this line print like money

std::cout << " Upgrades($" << upgradeCost << ")" << std::endl; // Don't touch me

// ============================================================

// Leave this menu the way it is

std::cout << "Do you want to upgrade your car? ";

std::cout << "[-/+1] Downgrade / Upgrade Engine ";

std::cout << "[-/+2] Downgrade / Upgrade Tires ";

std::cout << "[-/+3] Downgrade / Upgrade Rims ";

std::cout << "[-/+4] Downgrade / Upgrade Paint ";

std::cout << "[-/+5] Downgrade / Upgrade Muffler ";

std::cout << "[ 6] Clear all upgrades ";

std::cout << "[ 7] Reset car ";

std::cout << "[ 8] Buy Car!!! ";

std::cout << "What would you like to do?: ";

std::cin >> upgradeSelection; // can be positive or negative

// TODO: Now operate on the user's selection

// TODO: If the user makes a bad selection, tell them

// -----------------------------------------------------------

// Put your code in here! --> hint: what did you do last time?

// -----------------------------------------------------------

// -----------------------------------------------------------

// This newline will create a space between menus

std::cout << " ";

}

}

// Once again, a while(true) loop will never exit

// TODO: Make this loop exit when a car is selected and the customer is happy

} while (true);

/////////////////////////////////////////////////

// Do not modify these lines

/////////////////////////////////////////////////

totalCost = carCost + upgradeCost; // Don't touch me

std::cout << "Bill: Car($" << carCost << ")"; // Don't touch me

std::cout << " Upgrades($" << upgradeCost << ")"; // Don't touch me

std::cout << " Total ($" << totalCost << ") "; // Don't touch me

/////////////////////////////////////////////////

// these will stop the window from closing

std::cout << "Press ENTER to continue";

std::cin.ignore();

std::cin.get();

}

Explanation / Answer

int main()
{
int quoteNumber = 1;
bool customerHappy = false; // are they happy with car?
int carSelection = 0; // used to capture selected car
double carCost = 0; // car cost alone
double upgradeCost = 0; // upgrade cost alone
double totalCost = 0; // car plus upgrades cost
// You are going to DOOO something until the customer has
// selected a car and is happy
do
{
// if(true) will ALWAYS be activated
// TODO: Make this loop run if we haven't selected a car
// yet - get the customer to select one
// if carSelection == 0, then they haven't picked a car yet (hint hint)
std::cout << "Welcome to Bob's used car lot and chop shop!" << " ";
std::cout << "As you can see we have quite a few to choose from!" << " ";
std::cout << "Which one would you like?:" << " ";
std::cout << "[1] 2005 Vokswagen Beetle ($8,000)" << " ";
std::cout << "[2] 2008 Pontiac G6 ($8,581)" << " ";
std::cout << "[3] 2004 Chevy S-10 ($10,500)" << " ";
std::cout << "[4] 2016 Jeep Patriot ($15,209)" << " ";
std::cout << "[5] 2012 Jeep Wrangler Sport ($24,390)" << " ";
std::cout << "Which car would you like?: ";
std::cin >> carSelection;
// TODO: If the user makes a bad selection, tell them
// TODO: Else, set the carCost and move on to upgrades
// ----------------------------------------------
// Put your code in here! --> hint: use a switch
// ----------------------------------------------
// switch(carSelection)...
switch(carSelection)
{
   case 1 :
       carCost = 8000;
       break;
   case 2 :
       carCost = 8581;
       break;
   case 3 :
       carCost = 10500;
       break;
   case 4 :
       carCost = 15209;
       break;
   case 5 :
       carCost = 24390;
       break;
}
}
while(carSelection < 1 && carSelection > 5);

// ----------------------------------------------
// This newline will create a space between menus
std::cout << " ";
int upgradeSelection = 0; // The users menu choice
int engineLevel = 0; // the upgrade level of engine (0-5 only)
int tireLevel = 0; // the upgrade level of tire (0-5 only)
int rimLevel = 0; // the upgrade level of rims (0-5 only)
int paintLevel = 0; // the upgrade level of paint (0-5 only)
int mufflerLevel = 0; // the upgrade level of muffler (0-5 only)

// while(true) will NEVER stop running
// TODO: Make this while loop exit based on user input
while (true)
{
/////////////////////////////////////////////////////
// TODO: Calculate all of the upgrade costs here
/////////////////////////////////////////////////////
// Upgrades are $100 per level plus the cost of lower upgrades
// Example: Level 1 upgrade is $100
// Example: Level 2 upgrade is $100 + $200 = $300
// Example: Level 3 upgrade is $100 + $200 + $300 = $600
// etc etc etc etc etc etc etc etc etc etc etc etc etc...
// Hint: You might find a for-loop helpful for accumulating an amount
upgradeCost = 0;
//////////////////////////////////////////////////////
// Put your code below each of the comments marked
// for each upgrade type
//////////////////////////////////////////////////////
// Calculate upgrade cost for engine
for(int i = 0 ; i < engineLevel ; i++){
   upgradeCost += i*100;
   }
// i.e. Put code here for engine...
// Calculate upgrade cost for tires
   for(int i = 0 ; i < tireLevel ; i++){
   upgradeCost += i*100;
   }
for(int i = 0 ; i < paintLevel ; i++){
   upgradeCost += i*100;
   }
for(int i = 0 ; i < rimLevel ; i++){
   upgradeCost += i*100;
   }
   for(int i = 0 ; i < mufflerLevel ; i++){
   upgradeCost += i*100;
   }
// Calculate upgrade cost for muffler
//////////////////////////////////////////////////////

//////////////////////////////////////////////////////
// TODO: Make the quote lines below print to look like money
// Example 3.45234 should be --> 3.45
// Think about iomanip
// ---------------------------------------
// Put some code in here
// ---------------------------------------



// ---------------------------------------
// ============================================================
// Don't mess with these lines ================================
// ============================================================
std::cout << "[" << quoteNumber++ << "] "; // Don't touch me
std::cout << " Car($" << carCost << ")"; // Don't touch me
std::cout << " E(" << engineLevel << ")"; // Don't touch me
std::cout << " T(" << tireLevel << ")"; // Don't touch me
std::cout << " R(" << rimLevel << ")"; // Don't touch me
std::cout << " P(" << paintLevel << ")"; // Don't touch me
std::cout << " M(" << mufflerLevel << ")"; // Don't touch me
// Don't touch the following line -- HOWEVER, you should put
// something in the space ABOVE to make this line print like money
std::cout << " Upgrades($" << upgradeCost << ")" << std::endl; // Don't touch me
// ============================================================
// Leave this menu the way it is
std::cout << "Do you want to upgrade your car? ";
std::cout << "[-/+1] Downgrade / Upgrade Engine ";
std::cout << "[-/+2] Downgrade / Upgrade Tires ";
std::cout << "[-/+3] Downgrade / Upgrade Rims ";
std::cout << "[-/+4] Downgrade / Upgrade Paint ";
std::cout << "[-/+5] Downgrade / Upgrade Muffler ";
std::cout << "[ 6] Clear all upgrades ";
std::cout << "[ 7] Reset car ";
std::cout << "[ 8] Buy Car!!! ";
std::cout << "What would you like to do?: ";
std::cin >> upgradeSelection; // can be positive or negative
// TODO: Now operate on the user's selection
// TODO: If the user makes a bad selection, tell them
// -----------------------------------------------------------
// Put your code in here! --> hint: what did you do last time?
// -----------------------------------------------------------


// -----------------------------------------------------------

// This newline will create a space between menus
std::cout << " ";
}
}
// Once again, a while(true) loop will never exit
// TODO: Make this loop exit when a car is selected and the customer is happy
} while (true);
/////////////////////////////////////////////////
// Do not modify these lines
/////////////////////////////////////////////////
totalCost = carCost + upgradeCost; // Don't touch me
std::cout << "Bill: Car($" << carCost << ")"; // Don't touch me
std::cout << " Upgrades($" << upgradeCost << ")"; // Don't touch me
std::cout << " Total ($" << totalCost << ") "; // Don't touch me
/////////////////////////////////////////////////
// these will stop the window from closing
std::cout << "Press ENTER to continue";
std::cin.ignore();
std::cin.get();
}