I have tried switching the value of the variable within the switch but the minute I step outside the switch I have just the integer value not the float value. If I try and call the switch with anything but an integer the compiler pukes. I just finished trying to write separate functions to handle the variable value switch but I cannot seem to get them to hold the value I want outside of the function, I think my functions are correct but I am not sure. The teacher is not the best and we are generally left to our own devices to figure out how to solve the problem. I am sure there is a less complicated way to do this but I can't seem to find it. The only requirement is that is must have switches and a menu listing the cookie sizes and prices, and no if/else statements. Here is what I have so far, I have tried to comment as best I can, thanks for any help. I am just looking for pointers on where to look or advice not for someone to do the work for me.
The next thoughts on my mind is that somehow I have declared the variables wrong of I have written the functions wrong for what I am trying to do. Thanks for any help anyone can give. BTW I am using Visual Studio 2005 as my IDE and compiler.
Thanks,
Matty
CODE
#include <iostream>
using namespace std;
void showmenu();
void small(); //function to swap the value of dPrice with dPriceSmall
void medium(); //function to swap the value of dPrice with dPriceMed
void large(); //function to swap the value of dPrice with dPricelarge
int main()
{
int nChoice; //this will be the cone size the person want
int nNumCones = 0; //this is the number of cones the person wants
double dPrice = 0; //this is what I want to use within the functions
double dPriceSmall = 1.29; //the price for a small cone
double dPriceMed = 1.49; //the price for a medium cone
double dPriceLarge = 1.79; //the price for a large cone
double dTotal = 0; //this will be the amount of the final bill
showmenu();
cin >> nChoice;
switch(nChoice)
{
case 1 : small();
// <old code> cout << "You have chosen a small cone" << endl;
// <old code> dTotal = dPriceSmall * nChoice;
break;
case 2 : medium();
// <old code> cout << "You have chosen a medium cone" << endl;
// <old code> nChoice = dPriceMed;
break;
case 3 : large();
// <old code> cout << "You have chosen a large cone" << endl;
// <old code> nChoice = dPriceLarge;
break;
default : cout << "That is not a choice please chose again" << endl;
// <old code> showmenu();
// <old code> cin >> nChoice;
exit(0);
}
cout << "How many cones wuld you like to buy?\n";
cin >> nNumCones; // get the number of cones from the customer
dTotal = nNumCones * dPrice; //calculate the price of the bill
cout << "Your total bill is $" << dTotal << ".\n" //print the price of the bill
"Thank You and come back soon.";
return 0;
}
void showmenu() // this is the menu used to show the sizes and prices
{
cout << "Please enter 1, 2, 3, or 0:\n"
"1> Small Cone $1.29\n"
"2> Medium Cone $1.49\n"
"3> large Cone $1.79\n";
}
void small() //I am trying to set a variable that I can use for calculations
{
double dPrice = 0; // setting the value to zero to start with
double dPriceSmall = 1.29; //the price of a small cone
cout << "You have picked a small cone.\n";
dPrice = dPriceSmall; // trying to make the price of the small cone available for calcultions outside the switch
}
void medium() //I am trying to set a variable that I can use for calculations
{
double dPrice = 0; // setting the value to zero to start with
double dPriceMed = 1.49; //the price of a medium cone
cout << "You have picked a medium cone.\n";
dPrice = dPriceMed; // trying to make the price of the medium cone available for calcultions outside the switch
}
void large() //I am trying to set a variable that I can use for calculations
{
double dPrice = 0; // setting the value to zero to start with
double dPriceLarge = 1.59; //the price of a large cone
cout << "You have picked a large cone.\n";
dPrice = dPriceLarge; // trying to make the price of the large cone available for calcultions outside the switch
}
using namespace std;
void showmenu();
void small(); //function to swap the value of dPrice with dPriceSmall
void medium(); //function to swap the value of dPrice with dPriceMed
void large(); //function to swap the value of dPrice with dPricelarge
int main()
{
int nChoice; //this will be the cone size the person want
int nNumCones = 0; //this is the number of cones the person wants
double dPrice = 0; //this is what I want to use within the functions
double dPriceSmall = 1.29; //the price for a small cone
double dPriceMed = 1.49; //the price for a medium cone
double dPriceLarge = 1.79; //the price for a large cone
double dTotal = 0; //this will be the amount of the final bill
showmenu();
cin >> nChoice;
switch(nChoice)
{
case 1 : small();
// <old code> cout << "You have chosen a small cone" << endl;
// <old code> dTotal = dPriceSmall * nChoice;
break;
case 2 : medium();
// <old code> cout << "You have chosen a medium cone" << endl;
// <old code> nChoice = dPriceMed;
break;
case 3 : large();
// <old code> cout << "You have chosen a large cone" << endl;
// <old code> nChoice = dPriceLarge;
break;
default : cout << "That is not a choice please chose again" << endl;
// <old code> showmenu();
// <old code> cin >> nChoice;
exit(0);
}
cout << "How many cones wuld you like to buy?\n";
cin >> nNumCones; // get the number of cones from the customer
dTotal = nNumCones * dPrice; //calculate the price of the bill
cout << "Your total bill is $" << dTotal << ".\n" //print the price of the bill
"Thank You and come back soon.";
return 0;
}
void showmenu() // this is the menu used to show the sizes and prices
{
cout << "Please enter 1, 2, 3, or 0:\n"
"1> Small Cone $1.29\n"
"2> Medium Cone $1.49\n"
"3> large Cone $1.79\n";
}
void small() //I am trying to set a variable that I can use for calculations
{
double dPrice = 0; // setting the value to zero to start with
double dPriceSmall = 1.29; //the price of a small cone
cout << "You have picked a small cone.\n";
dPrice = dPriceSmall; // trying to make the price of the small cone available for calcultions outside the switch
}
void medium() //I am trying to set a variable that I can use for calculations
{
double dPrice = 0; // setting the value to zero to start with
double dPriceMed = 1.49; //the price of a medium cone
cout << "You have picked a medium cone.\n";
dPrice = dPriceMed; // trying to make the price of the medium cone available for calcultions outside the switch
}
void large() //I am trying to set a variable that I can use for calculations
{
double dPrice = 0; // setting the value to zero to start with
double dPriceLarge = 1.59; //the price of a large cone
cout << "You have picked a large cone.\n";
dPrice = dPriceLarge; // trying to make the price of the large cone available for calcultions outside the switch
}
