C++ Switch statement using strings - c++

I am fully aware that switch must be used with int but my assignment is requiring me to use switch in regards to user input which will be strings. I've looked and I've seen some mentioning stoi but I'm not sure if that is what my professor is expecting b/c we have not been introduced to it yet. I'm completely new to C++ so I'm still learning and this code is not completed yet but can anyone please help me with this?
int main()
{
// Declare Constant variables
const float DISC_GOLF_RETAIL = 14.96;
const float ULTIMATE_RETAIL = 20.96;
const double DISCOUNT1 = 8;
const float DISCOUNT2 = .16;
const float DISCOUNT3 = .24;
const float DISCOUNT4 = .32;
const double DEC = 100;
// Declare variables
double quantity;
double pricePerDisc;
double totalSavings;
double total;
char userInput;
float discount;
string discType;
string disc1 = "Ultimate Disc";
string disc2 = "Disc-Golf Disc";
// Display title
cout << "Welcome to the Flying-Disc Shop!" << "\n" << endl;
// Prompt the user for input
cout << "Enter 'u' for ultimate discs and 'g' for disc golf: ";
cin >> (userInput);
cout << endl;
switch (discType)
{
case 1: userInput=='u' || 'U';
discType = disc1;
pricePerDisc = ULTIMATE_RETAIL;
break;
case 2: userInput=='g' || 'G';
discType = disc2;
pricePerDisc = DISC_GOLF_RETAIL;
break;
default:
cout << "Invalid disc type." << endl;
return 0;
}
cout << "Enter the number of Ultimate Disc(s): ";
cin >> (quantity);
cout << endl;
cout << "------------Receipt------------" << endl;
if (quantity>5 && quantity<=9)
{
discount = DISCOUNT1 / DEC;
totalSavings = (pricePerDisc * quantity) * discount;
}
else if (quantity>10 && quantity<=19)
{
discount = DISCOUNT2;
totalSavings = (pricePerDisc * quantity) * discount;
}
else if (quantity>20 && quantity<=29)
{
discount = DISCOUNT3;
totalSavings = (pricePerDisc * quantity) * discount;
}
else if (quantity>= 30)
{
discount = DISCOUNT4;
}
totalSavings = (pricePerDisc * quantity) * discount;
total = quantity * pricePerDisc - totalSavings;
cout << "Disc Type: " << discType << endl;
cout << "Quantity: " << quantity << endl;
cout << "Pricer per Disc: " << "$ " << fixed << setprecision(2)
<< pricePerDisc << endl;
cout << "Total Savings: " << "$ " << "-" << fixed << setprecision(2)
<< totalSavings << endl;
cout << "Total: " << "$ " << total << fixed << setprecision(2) << endl;
}

For single char responses, you can use the switch/case statement:
switch (userInput)
{
case 'g':
case 'G':
/* ... */
break;
case 'u':
case 'U':
// ...
break;
default:
// ...
break;
}
A single character can be treated differently than a string. A string is usually consists of more than one character.

Related

Wrong result in c++

I want to code currency converter and I do it,
but, When I tried it, result was wrong than I expected,
Knowing that the code works, but the result is not correct and I tried all the methods I know
, I am a beginner, please a answer my question, and Thanks
#include <iostream>
using namespace std;
int main()
{
cout << "1- EGP TO Usd" << endl;
cout << "2- USD TO EGP" << endl;
cout << "3- EGP TO SAR" << endl;
cout << "4- USD TO SAR" << endl;
cout << "5- SAR TO EGP" << endl;
cout << "6- SAR TO USD" << endl;
int num;
double balance;
double egpToUsd = .064;
double usdToEgp = 15.70;
double egpToSar = .24;
double usdToSar = 3.75;
double sarToEgp = 4.19;
double sarToUsd = .27;
cout << "Enter a Num: ";
cin >> num;
cout << "Enter your balance: ";
cin >> balance;
if (num >= 1 && num <= 6)
{
switch (num)
{
case 1:
cout << (balance / egpToUsd);
break;
case 2:
cout << (balance * usdToEgp);
break;
case 3:
cout << (balance / egpToSar);
break;
case 4:
cout << (balance * usdToSar);
break;
case 5:
cout << (balance * sarToEgp);
break;
case 6:
cout << (balance / sarToUsd);
break;
}
}
else {
cout << "Error please select from list" << endl;
}
return 0;
}
#include <iostream>
int main()
{
std::cout << "1- EGP TO Usd\n";
std::cout << "2- USD TO EGP\n";
std::cout << "3- EGP TO SAR\n";
std::cout << "4- USD TO SAR\n";
std::cout << "5- SAR TO EGP\n";
std::cout << "6- SAR TO USD\n";
int num;
double balance;
double egpToUsd = .064;
double usdToEgp = 15.70;
double egpToSar = .24;
double usdToSar = 3.75;
double sarToEgp = 4.19;
double sarToUsd = .27;
std::cout << "Enter a Num: ";
std::cin >> num;
std::cout << "Enter your balance: ";
std::cin >> balance;
switch (num)
{
case 1:
std::cout << (balance * egpToUsd);
break;
case 2:
std::cout << (balance * usdToEgp);
break;
case 3:
std::cout << (balance * egpToSar);
break;
case 4:
std::cout << (balance * usdToSar);
break;
case 5:
std::cout << (balance * sarToEgp);
break;
case 6:
std::cout << (balance * sarToUsd);
break;
default:
std::cout << "Error please select from list\n";
break;
}
return 0;
}
Dont Use "using namespace std;" thats bad practice and why were you dividing in some cases, its a simple math. I hope you got your solution

C++ Clearing streams, and testing input - multiple issues

I'm currently learning C++ in class, and I have 3 problems with validation. My professor asked me to not user stringstream for validating input. I'm running into a problem where if the user enters two characters 'rr' for example, the error message displays twice (Which is why I just used stringstream originally to test for the first incorrect character). The second problem is if they enter two values, such as -45 -56. Somehow, the second value of 56 is coming to be positive after the loop and the program is running with it, even after I cleared the stream.
Lastly, if a user enters a float into an int, it truncates it - but how do I prevent that from happening? E.g. I only want an int entered. I tried less than and greater then with no avail. Notice in the below posted code, I do not have the truncating problem because the user input is a double - but I think someone should understand the theory of what I'm asking.
Please let me know if the formatting on here is not correct, I was having some issues with pasting it in the block, but it looks like it may be alright now.
Thank you!
Here is my code:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
const double EARTH_G = 1, SPACE_G = 0, EARTH_MOON_G = .17, VENUS_G = .90, MARS_G = .38,
MERCURY_G = .38, JUPITER_G = 2.36, SATURN_G = .92, URANUS_G = .89,
NEPTUNE_G = 1.14, PLUTO_G = .07;
double userWeight;
enum planets {Earth = 1, Space, Moon, Venus, Mars, Mercury, Jupiter, Saturn, Uranus,
Neptune, Pluto};
/*Validates input by making sure checking for only numbers greater than 0*/
{
bool flag = 0;
while (flag == 0)
{
cout << "What is your earth weight in lbs?" << endl;
cin >> userWeight;
if (!static_cast<double>(userWeight) || userWeight < 0)
{
cout << "Invalid Input.\nPlease enter only positive numbers." << endl;
cin.clear();
cin.ignore();
}
else
{
flag = 1;
}
}
}
{
//Displays menu with choices, and allows produces output based on choice.
//Loops while input is invalid.
bool flag = 0;
int choice;
double newWeight;
while (flag == 0)
{
cout << "Select a place in space with a number to see your weight there!\n\n";
cout << "1 - Earth\n\n2 - Space\n\n3 - Earth's Moon\n\n4 - Venus\n\n5 - Mars\n\n"
<< "6 - Mercury\n\n7 - Jupiter\n\n8 - Saturn\n\n9 - Uranus\n\n10 - Neptune\n\n11 - Pluto\n\n";
cout << "Select 1 - 11:\n";
cin >> choice;
switch (choice)
{
case(Earth):
{
newWeight = (userWeight * EARTH_G);
cout << "Your weight on earth is " << fixed << setprecision(1)
<< newWeight << endl;
flag = 1;
break;
}
case(Space):
{
newWeight = (userWeight * SPACE_G);
cout << "Your weight in space is " << fixed << setprecision(1)
<< newWeight << endl;
flag = 1;
break;
}
case(Moon):
{
newWeight = (userWeight * EARTH_MOON_G);
cout << "Your weight on the Earth's moon is " << fixed << setprecision(1)
<< newWeight << endl;
flag = 1;
break;
}
case(Venus):
{
newWeight = (userWeight * VENUS_G);
cout << "Your weight on Venus is " << fixed << setprecision(1) << newWeight << endl;
flag = 1;
break;
}
case(Mars):
{
newWeight = (userWeight * MARS_G);
cout << "Your weight on Mars is " << fixed << setprecision(1) << newWeight << endl;
flag = 1;
break;
}
case(Mercury):
{
newWeight = (userWeight * MERCURY_G);
cout << "Your weight on Mercury is " << fixed << setprecision(1) << newWeight << endl;
flag = 1;
break;
}
case(Jupiter):
{
newWeight = (userWeight * JUPITER_G);
cout << "Your weight on Jupiter is " << fixed << setprecision(1) << newWeight << endl;
flag = 1;
break;
}
case(Saturn):
{
newWeight = (userWeight * SATURN_G);
cout << "Your weight on Saturn is " << fixed << setprecision(1) << newWeight << endl;
flag = 1;
break;
}
case(Uranus):
{
newWeight = (userWeight * URANUS_G);
cout << "Your weight on Uranus is " << fixed << setprecision(1) << newWeight << endl;
flag = 1;
break;
}
case(Neptune):
{
newWeight = (userWeight * NEPTUNE_G);
cout << "Your weight on Neptune is " << fixed << setprecision(1) << newWeight << endl;
flag = 1;
break;
}
case(Pluto):
{
newWeight = (userWeight * PLUTO_G);
cout << "Your weight on Pluto is " << fixed << setprecision(1) << newWeight << endl;
flag = 1;
break;
}
default:
{
cout << "Invalid Input." << endl;
cin.clear();
cin.ignore();
}
}
}
}
}
According to http://en.cppreference.com/w/cpp/io/basic_istream/ignore , the basic, no parameters ignore only removes one character from the input stream. According to the example provided in the above documentation link, the recommended usage is
input.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
To consume the entire line. Note the additional include statement required to get numeric_limits::max.

Left of '.getDrinkPrice' must have class/struct/union

This is a project from a c++ book I'm learning from.
I keep getting the error
"left of '.getDrinkPrice' must have class/struct/union"
I have tried fixing it but I just keep messing it up further. The visual studio error lists aren't too friendly for new users and this is really bothering me. I obviously don't want the code to be reposted fixed but I do want to be pointed in the right direction.
#include <iostream>
#include <string>
#include <Windows.h>
using namespace std;
double wallet = 10.00;
int change;
bool isRunning;
bool isChoosing;
int userChoice;
double pricePaid;
class Soda {
private:
string drinkName;
double drinkPrice;
int drinkAmt;
public:
Soda() {
drinkName;
drinkPrice = .75;
drinkAmt = 20;
}
void setDrinkName(string name) {
drinkName = name;
}
string getDrinkName() {
return drinkName;
}
void setDrinkPrice(double price) {
drinkPrice = price;
}
double getDrinkPrice() {
return drinkPrice;
}
void setDrinkAmt(int amt) {
drinkAmt = amt;
}
void setNewDrinkAmount(int amt, int i) {
drinkAmt = amt - i;
}
int getDrinkAmt() {
return drinkAmt;
}
};
// Declares classes
Soda * Cola = new Soda;
Soda * RootBeer = new Soda;
Soda * LemonLime = new Soda;
Soda * Grape = new Soda;
Soda * Cream = new Soda;
Soda *soda = new Soda;
static void init() {
// Pushes the Vending Machine data to the screen
cout << "Drink Name: " << " " << "Drink Cost: " << " " << "Number in machine: \n";
cout << *Cola->getDrinkName << " " << *Cola->getDrinkPrice << " " << *Cola->getDrinkAmt <<endl;
cout << *RootBeer->getDrinkName << " " << *RootBeer->getDrinkPrice << " " << *RootBeer->getDrinkAmt << endl;
cout << *LemonLime->getDrinkName << " " << *LemonLime->getDrinkPrice << " " << *LemonLime->getDrinkAmt << endl;
cout << *Grape->getDrinkName << " " << *Grape->getDrinkPrice << " " << *Grape->getDrinkAmt << endl;
cout << *Cream->getDrinkName << " " << *Cream->getDrinkPrice << " " << *Cream->getDrinkAmt << endl;
cout << endl;
cout << "You have $" << wallet << endl;
cout << endl;
}
void checkValidPurchase(Soda soda, double w, double p) {
double priceOfDrink = soda.getDrinkPrice();
int amountOfDrink = soda.getDrinkAmt;
// If there are enough drinks are in the machine
if (amountOfDrink > 0) {
// If user has enough money
if (w >= priceOfDrink) {
// If amount paid is greater than drink price
if (p > priceOfDrink) {
// Calculate price and change
w = w - p;
double getChange = p - priceOfDrink;
// Return Change
cout << "Paid " << p << " returning " << getChange << endl;
// Update amounts
int j = soda.getDrinkAmt;
soda.setNewDrinkAmount(j, 1);
w = w + getChange;
}
// If amount paid is equal to drink price
else if (p == priceOfDrink) {
// Calculate price
w = w - p;
cout << "Paid " << p << endl;
// Update amounts
int j = soda.getDrinkAmt;
soda.setNewDrinkAmount(j, 1);
}
// If amount paid is less than drink price
else if (p < priceOfDrink) {
// Prompt error and return to drink select
cout << "You did not enter enough money, returning to drink select. \n";
Sleep(3000);
}
}
// If user does not have enough money
else {
cout << "Not enough money in wallet. \n";
double amtNeeded = w + (w - priceOfDrink);
cout << "You have: " << w << " Needed for purchase: " << amtNeeded << endl;
Sleep(3000);
}
}
// If there are not enough drinks in machine
else {
cout << "Not enough of " << soda.getDrinkName << " in machine. \n";
Sleep(3000);
}
}
void sodaMachine() {
// Starts the drink select loop
isChoosing = true;
while (isChoosing) {
// gets user input
cout << "Enter the number of the soda you would like: \n";
cout << "Or to quit, press escape \n";
switch (userChoice) {
case 1:
cout << "Dispensing Cola \n";
cout << "Cost is .75 \n";
cin >> pricePaid;
checkValidPurchase(*Cola, wallet, pricePaid);
isChoosing = false;
break;
case 2:
cout << "Dispensing Root Beer \n";
cout << "Cost is .75 \n";
cin >> pricePaid;
checkValidPurchase(*RootBeer, wallet, pricePaid);
isChoosing = false;
break;
case 3:
cout << "Dispensing Lemon Lime \n";
cout << "Cost is .75 \n";
cin >> pricePaid;
checkValidPurchase(*LemonLime, wallet, pricePaid);
isChoosing = false;
break;
case 4:
cout << "Dispensing Grape \n";
cout << "Cost is .80 \n";
cin >> pricePaid;
checkValidPurchase(*Grape, wallet, pricePaid);
isChoosing = false;
break;
case 5:
cout << "Dispensing Cream \n";
cout << "Cost is .80 \n";
cin >> pricePaid;
checkValidPurchase(*Cream, wallet, pricePaid);
isChoosing = false;
break;
case WM_CHAR:
if (GetAsyncKeyState(VK_ESCAPE)) {
cout << "Exiting program \n";
isRunning = false;
break;
}
default:
isChoosing = false;
break;
}
}
}
int main() {
// Sets class values
Cola->setDrinkName("1. Cola");
RootBeer->setDrinkName("2. RootBeer");
LemonLime->setDrinkName("3. LemonLime");
Grape->setDrinkName("4. Grape");
Grape->setDrinkPrice(.80);
Cream->setDrinkName("5. Cream");
Cream->setDrinkPrice(.80);
isRunning = true;
while (isRunning) {
// Run Program
init();
sodaMachine();
system("cls");
}
return 0;
}
*Cola->getDrinkName is not correct.
Use either Cola->getDrinkName() or (*Cola).getDrinkName() instead; same for all other calls of the same kind in your code.

Trouble getting percentages in C++

C++ beginner here. I'm trying to replicate this output and I'm almost there but I'm having trouble with the last part (the quiz percentage).
Example output:
Welcome to our math quiz program! Please enter the answers to the
following questions:
5! = 5
(2^(2^(2^2))) = 65536
3 * (4 + 8) / ((4 * 2) / (5 – 1)) = 18
3 + 2 + 1 + 2 + 3 = 11
Number of correct answers: 3
Number of incorrect answers: 1
Quiz percentage: 75%
Here is my code so far:
int problem4() {
int numCorrect = 0;
int numIncorrect = 0;
int possibleCorrect = 4;
int correctAnswerOne = 120;
int correctAnswerTwo = 65536;
int correctAnswerThree = 18;
int correctAnswerFour = 11;
int guessOne;
int guessTwo;
int guessThree;
int guessFour;
cout << "*** Start of problem 4 ***" << endl;
cout << "Welcome to our math quiz program!" << endl;
cout << "Please enter the answers to the following questions: " << endl;
cout << "5! = ";
cin >> guessOne;
cout << "(2^(2^(2^2))) = ";
cin >> guessTwo;
cout << "3 * (4 + 8) / ((4 * 2) / (5 - 1)) = ";
cin >> guessThree;
cout << "3 + 2 + 1 + 2 + 3 = ";
cin >> guessFour;
switch (guessOne) {
case 120:
numCorrect++;
break;
default:
numIncorrect++;
}
switch (guessTwo) {
case 65536:
numCorrect++;
break;
default:
numIncorrect++;
}
switch (guessThree) {
case 18:
numCorrect++;
break;
default:
numIncorrect++;
}
switch (guessFour) {
case 11:
numCorrect++;
break;
default:
numIncorrect++;
}
cout << "Number of correct answers: " << numCorrect << endl;
cout << "Number of incorrect answers: " << numIncorrect << endl;
cout << "Quiz percentage: " << (numCorrect / possibleCorrect) * 100 << "%" << endl;
cout << endl;
return 0;
}
It seems like when I answer all the questions correctly I get 100% as I should, however, when I score anything but that (only 1, 2, or 3 correct), I end up getting a 0% as a result. I thought I was doing the percentage calculation correctly but the output is suggesting otherwise. Can anybody point me in the right direction? Thanks.
EDIT: Added breaks to switches (however, not root of problem).
Break statements are missing from your switch(s).
Plus you need to type cast the result to float as all the variables are integers. i suggest you use:
float perc;
perc = (numCorrect/possibleCorrect)*100;
You need a break if you switch, and multiply by 100.0 first to implicitly promote the first expression to a double and then divide:
int problem4() {
int numCorrect = 0;
int numIncorrect = 0;
int possibleCorrect = 4;
int correctAnswerOne = 120;
int correctAnswerTwo = 65536;
int correctAnswerThree = 18;
int correctAnswerFour = 11;
int guessOne;
int guessTwo;
int guessThree;
int guessFour;
cout << "*** Start of problem 4 ***" << endl;
cout << "Welcome to our math quiz program!" << endl;
cout << "Please enter the answers to the following questions: " << endl;
cout << "5! = ";
cin >> guessOne;
cout << "(2^(2^(2^2))) = ";
cin >> guessTwo;
cout << "3 * (4 + 8) / ((4 * 2) / (5 - 1)) = ";
cin >> guessThree;
cout << "3 + 2 + 1 + 2 + 3 = ";
cin >> guessFour;
switch (guessOne) {
case 120:
numCorrect++;
break;
default:
numIncorrect++;
}
switch (guessTwo) {
case 65536:
numCorrect++;
break;
default:
numIncorrect++;
}
switch (guessThree) {
case 18:
numCorrect++;
break;
default:
numIncorrect++;
}
switch (guessFour) {
case 11:
numCorrect++;
break;
default:
numIncorrect++;
}
cout << "Number of correct answers: " << numCorrect << endl;
cout << "Number of incorrect answers: " << numIncorrect << endl;
cout << "Quiz percentage: " << (numCorrect * 100.0 / possibleCorrect) << "%" << endl;
cout << endl;
return 0;
}
For this purpose though, a simple if/else would be easier to read:
int problem4() {
int numCorrect = 0;
int numIncorrect = 0;
int possibleCorrect = 4;
int correctAnswerOne = 120;
int correctAnswerTwo = 65536;
int correctAnswerThree = 18;
int correctAnswerFour = 11;
int guessOne;
int guessTwo;
int guessThree;
int guessFour;
cout << "*** Start of problem 4 ***" << endl;
cout << "Welcome to our math quiz program!" << endl;
cout << "Please enter the answers to the following questions: " << endl;
cout << "5! = ";
cin >> guessOne;
cout << "(2^(2^(2^2))) = ";
cin >> guessTwo;
cout << "3 * (4 + 8) / ((4 * 2) / (5 - 1)) = ";
cin >> guessThree;
cout << "3 + 2 + 1 + 2 + 3 = ";
cin >> guessFour;
if( guessOne == 120 ) ++numCorrect; else ++numIncorrect;
if( guessTwo == 65536 ) ++numCorrect; else ++numIncorrect;
if( guessThree == 18 ) ++numCorrect; else ++numIncorrect;
if( guessFour == 11 ) ++numCorrect; else ++numIncorrect;
cout << "Number of correct answers: " << numCorrect << endl;
cout << "Number of incorrect answers: " << numIncorrect << endl;
cout << "Quiz percentage: " << (numCorrect * 100.0 / possibleCorrect) << "%" << endl;
cout << endl;
return 0;
}

Issues with linker error in code blocks using C++

this is my first time posting here, so be a little gentle ok? Also, I am taking my first programming class, and though I understand everything we are doing in class, a lot of the stuff I read on here is over my head. If you could please try to keep your answers as basic as possible I would appreciate it.
With that said, I am doing C++ programming, using code blocks on a mac.
This is the error I am getting from code blocks
Undefined symbols for architecture x86_64:
"totalBill(double, char, double&, double&, double&, double&)", referenced from:
_main in Watkins-wk6-prog1.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Process terminated with status 1 (0 minute(s), 0 second(s))
0 error(s), 0 warning(s) (0 minute(s), 0 second(s))
I will also post the code so you can take a look and see if that sheds any light.
#include <iostream>
#include <iomanip>
#include <cctype>
using namespace std;
//Global Constants
double const COM_PRICE = 22.91;
double const MID_PRICE = 25.76;
double const FULL_PRICE = 28.87;
double const SUV_PRICE = 98.88;
int const COM_MILES = 20;
int const MID_MILES = 25;
int const FULL_MILES = 30;
string const SUV_MILES = "Unlimited";
double const COM_OVER_MILES = 0.05;
double const MID_OVER_MILES = 0.07;
double const FULL_OVER_MILES = 0.09;
string const SUV_OVER_MILES = "N/A";
int const WEEK = 7;
double const WEEK_RATE = 6.5;
double const TAX = .115;
string const PROMPT1 = "Enter the number of days for the rental";
string const PROMPT2 = "Enter the number of miles driven";
string const PROMPT3 = "Enter the type of car to be rented (C,M,F,S)";
// function prototypes
char carSize(string prompt);
double inputs (string prompt);
double ChargeForCar(char carType, int numDays);
double carPrice (char carType);
double ChargeforMiles(double miles, char carType);
void totalBill(double miles, char carType, double& taxTotal, double& mileCharge, double&
grandTotal, double& carCharge);
string carSize (char carType);
void billOutput(string carTypeName, int numDays, double miles, double carCharge, double
milageCharge, double taxTotal, double grandTotal);
int main()
{
int numDays; // number of days of rental
int weeks ; // number of weeks in rental period
int extraDays; // number of extra days if car rented for more than a week
double mileCharge = 0;
double carCharge = 0;
double taxTotal = 0;
double grandTotal = 0;
char carType;
double miles;
string carTypeName;
cout << "Program to calculate the charge for a rental car." << endl << endl;
carType = carSize(PROMPT3); // calls function to get the type of car from
// the user
numDays = static_cast<int> (inputs(PROMPT1)); // calls function to get number of days in
//the rental period
miles = inputs(PROMPT2); // calls function to get number of mile driven
// during rental period
// calls function to calculate the total rental car bill
totalBill(miles, carType, taxTotal, grandTotal, mileCharge, carCharge);
carTypeName = carSize(carType); // calls function to save car size as string
//calls function to output the bill componenets
billOutput(carTypeName, numDays, miles, carCharge, mileCharge, taxTotal, grandTotal);
return 0;
}
char carSize(string prompt) // function to present car choices and read in user input
{
char carType;
bool valid = false;
cout << "Type of Car" << setw(16) << "Price per day" << setw(15);
cout << "Included Milage" << setw(24) << "Extra charge per mile" << endl;
cout << setw(66) << setfill ('-') << "-" << endl;
cout << setfill(' ') << "C - Compact size" << setw(8) << COM_PRICE;
cout << setw(5) << COM_MILES << setw(5) << COM_OVER_MILES << endl;
cout << "M - Mid-Size" << setw(8) << MID_PRICE;
cout << setw(5) << MID_MILES << setw(5) << MID_OVER_MILES << endl;
cout << "F - Full size" << setw(8) << FULL_PRICE << setw(5) ;
cout << FULL_MILES << setw(5) << FULL_OVER_MILES << endl;
cout << " S - SUV" << setw(8) << SUV_PRICE << setw(5) ;
cout << SUV_MILES << setw(5) << SUV_OVER_MILES << endl;
do
{
cout << prompt ;
cin >> carType;
carType = toupper(carType);
switch(carType)
{
case 'C':
case 'F':
case 'M':
case 'S':
valid = true;
break;
default:
cout << "Invalid entry. Please enter a letter from the list";
break;
}
}
while (valid == false);
return carType;
}
double inputs(string prompt) // general function to read a prompt and output user input
{
double input;
do
{
cout << prompt;
cin >> input;
if (input <= 0)
{
cout << "Invalid input." << endl;
cout << "Please enter a positive, nonzero interger." << endl;
}
}
while (input <= 0);
return input;
}
double ChargeForCar(char carType, int numDays) // function to calculate the charge for the
//rental period
{
double carCharge;
int weeks;
int extraDays;
double carRate;
carRate = carPrice(carType);
if (numDays >= WEEK)
{
weeks = numDays / WEEK;
extraDays = numDays % WEEK;
carCharge = (weeks * WEEK_RATE * carRate) + (extraDays * carRate);
}
else
{
carCharge = carRate * numDays;
}
return carCharge;
}
double carPrice (char carType) // function to determine which price to use for rental car
{
double carRate;
switch (carType)
{
case 'C':
carRate = COM_PRICE;
break;
case 'F':
carRate = FULL_PRICE;
break;
case 'M':
carRate = MID_PRICE;
break;
case 'S':
carRate = SUV_PRICE;
break;
default:
cout << "Unknown car type" << endl;
break;
}
return carRate;
}
double ChargeForMiles(double miles, char carType) // function to calculate the extra milage
//cost
{
double milesCost;
double extraMiles;
switch(carType)
{
case 'C':
if (miles > COM_MILES)
{
extraMiles = miles - COM_MILES;
milesCost = extraMiles * COM_OVER_MILES;
}
else
{
milesCost = 0;
}
break;
case 'F':
if (miles > FULL_MILES)
{
extraMiles = miles - FULL_MILES;
milesCost = extraMiles * FULL_OVER_MILES;
}
else
{
milesCost = 0;
}
break;
case 'M':
if (miles > MID_MILES)
{
extraMiles = miles - MID_MILES;
milesCost = extraMiles * MID_OVER_MILES;
}
else
{
milesCost = 0;
}
break;
}
return milesCost;
}
void totalBill(double miles, double numDays, char carType, double& taxTotal, double& mileCharge,
double& grandTotal, double& carCharge)
// function to calculate the program totals
{
switch(carType)
{
case 'C':
case 'M':
case 'F':
mileCharge = ChargeForMiles(miles, carType);
break;
default:
mileCharge = 0;
break;
}
carCharge = ChargeForCar(carType, numDays); // call to the function to calculate the charge
//for the rental car
taxTotal = (mileCharge + carCharge) * TAX;
grandTotal = mileCharge + carCharge + taxTotal;
}
string carSize(char carType) //function for setting car type name as a string type
{
string typeName;
if (carType == 'C')
{
typeName = "Compact size";
}
else if (carType == 'M')
{
typeName = "Mid size";
}
else if (carType == 'F')
{
typeName = "Full size";
}
else if (carType == 'S')
{
typeName = "SUV size";
}
return typeName;
}
void billOutput(string carTypeName, int numDays, double miles, double carCharge, double
milageCharge, double taxTotal, double grandTotal)
{
cout << "Rent2U Rental Details" << endl << endl;
cout << "Car Size: " << carTypeName << endl;
cout << "Days Rented" << numDays << "days" << endl;
cout << "Miles Driven" << miles << "miles" << endl << endl;
cout << "BILL" << endl;
cout << "Car Charge" << "$" << carCharge << endl;
cout << "Mialge Charge" << "$" << milageCharge << endl;
cout << "Tax" << "$" << taxTotal << endl;
cout << setw(21) << " " << setw(11) << setfill('-') << "-" << endl;
cout << setfill(' ') << "Total Bill" << "$" << grandTotal << endl << endl;
}
It's because your implementation of totalBill has a different signature (i.e. different parameters) from the declaration. One takes two doubles then a char and the other, one double then a char. They need to match.