Re: Need help with my C++ code [message #445207 is a reply to message #445206] |
Wed, 23 March 2011 13:16 |
_SSnipe_
Messages: 4121 Registered: May 2007 Location: Riverside Southern Califo...
Karma:
|
General (4 Stars) |
|
|
Nevermind guys, a professor came into the lab and sat down with me and helped me fix it as you can see here
#include <cstdlib>
#include <iostream>
#include <string.h>
using namespace std;
void money_message(double&);
double insert_coin(double coin, double&);
void intro_message();
void end_message(char&);
void selections(string string_pool, double&);
void wrong_input(double&);
void welcome_banner();
void while_loop();
int zer0 = 0;
const double three_fifty = 3.50;
int main()
{
char response; // to restart program
double money_pool;
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
do {
money_pool = 0;
intro_message();
while (money_pool < three_fifty)
{
money_message(money_pool);
}
end_message(response);
} while ((response == 'y') || (response== 'Y'));
system("PAUSE");
return 0;
}
/*
void while_loop()
{
while (money_pool < three_fifty)
{
money_message();
}
}
*/
//displays what user to input then puts it into a string that calls for selections function
void money_message(double& money_pool)
{
using namespace std;
string string_pool;
cout << "Type quarter for .25" << endl;
cout << "Type nickle for .05" << endl;
cout << "Type dime for .10" << endl;
cout << "Type dollar for 1.00" << endl;
cout << endl;
cin >> string_pool;
selections(string_pool, money_pool);
}
//function gets called and has different value for coin depending on users entry, if current value is 3.50 or above, gives back change if not display current money and remaining and continues to loop
double insert_coin(double coin, double& money_pool)
{
double change;
money_pool = money_pool + coin;
system("CLS");
if (money_pool > three_fifty)
{
welcome_banner();
change = money_pool - three_fifty;
cout << "Your change is: " << change << endl;
cout << endl;
}
else
{
welcome_banner();
cout << "You have inserted: " << money_pool << " So far" << endl;
cout << "Remaining Balance: " << three_fifty - money_pool << endl;
cout << endl;
}
return money_pool;
}
//if user at anytime types in wrong char or string
void wrong_input(double& money_pool)
{
using namespace std;
system("CLS");
welcome_banner();
cout << "***********************************" << endl;
cout << "Not a valid entry, Please try again" << endl;
cout << "***********************************" << endl;
cout << endl;
cout << "Remaining Balance for a Deep Fried Twinkie: " << three_fifty - money_pool << endl;
cout << endl;
}
// display intro message
void intro_message()
{
welcome_banner();
cout << "Want a deep fried twinkie?" << endl;
cout << "Only $3.50" << endl;
cout << endl;
}
// display after program ends
void end_message(char& response)
{
cout << "Enjoy your deep fried twinkie" << endl;
cout << endl;
cout << "Want another? (type y for yes, anything else quits)" << endl;
cin >> response;
system("CLS");
}
// my banner that should always be on top of screen to look pretty =]
void welcome_banner()
{
cout << "****************************" << endl;
cout << "* Welcome to Tanner's *" << endl;
cout << "* Deep Fried Twinkie *" << endl;
cout << "* Dispenser *" << endl;
cout << "****************************" << endl;
cout << endl;
}
// selection function takes result from money_message function and then runs coin function depending on which string, if none runs error function
void selections(string string_pool, double& money_pool)
{
using namespace std;
double coin = zer0;
const double dollar = 1.00;
const double nickle = .05;
const double dime = .10;
const double quarter = .25;
if ((string_pool == "quarter") || (string_pool == "nickle") || (string_pool == "dime") || (string_pool == "dollar"))
{
if (string_pool == "quarter")
{
coin = quarter;
insert_coin(coin, money_pool);
}
if (string_pool == "nickle")
{
coin = nickle;
insert_coin(coin, money_pool);
}
if (string_pool == "dime")
{
coin = dime;
insert_coin(coin, money_pool);
}
if (string_pool == "dollar")
{
coin = dollar;
insert_coin(coin, money_pool);
}
}
else
{
wrong_input(money_pool);
}
}
anyways im happy
|
|
|