Need help with my C++ code [message #445206] |
Wed, 23 March 2011 12:15 |
_SSnipe_
Messages: 4121 Registered: May 2007 Location: Riverside Southern Califo...
Karma: 0
|
General (4 Stars) |
|
|
ok here is the story; I am still new at c++. I have only been taking the class for about a month. one assignment is make a program so its kinda like a vending machine for a deep fried twinkie (lol?), anyways if you see the code, i have a variable called money_pool which is used in a while loop, and different functions throughout the code. however, my professor said we can;t do global variables, so he recommended using call-by-reference (pointers) which he has barley started teaching us for about 5 minutes. So in other words, I don;t know about them yet and i'm trying to get my program to work with them. So far I messed up my code, like I said before it worked as global but since we can;t do that Iv been trying to figure out pointers in order to get it working, so I ask if someone can help me get this working. The problem is the variable money_pool....I hope....
I only ask if people dont suggest other ways to re arrange my functions of programs since they are ok, I only need to figure out how to get this to work like it used to
so please help me,
#include <cstdlib>
#include <iostream>
#include <string.h>
using namespace std;
void money_message();
double insert_coin(double coin, double&);
void intro_message();
void end_message();
string selections(string string_pool);
void wrong_input(double&);
void welcome_banner();
void while_loop(double&);
char response; // to restart program
const int zer0 = 0;
const double three_fifty = 3.50;
int main()
{
double money_pool = 0;
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
do {
intro_message();
while_loop(money_pool);
end_message();
} while ((response == 'y') || (response== 'Y'));
system("PAUSE");
return 0;
}
void while_loop(double& money_pool)
{
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()
{
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);
}
//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()
{
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
string selections(string string_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);
}
if (string_pool == "nickle")
{
coin = nickle;
insert_coin(coin);
}
if (string_pool == "dime")
{
coin = dime;
insert_coin(coin);
}
if (string_pool == "dollar")
{
coin = dollar;
insert_coin(coin);
}
}
else
{
// wrong_input(double&);
}
}
Quote: |
C:\Users\rcc\Documents\Untitled1.cpp In function `std::string selections(std::string)':
69 C:\Users\rcc\Documents\Untitled1.cpp too few arguments to function `double insert_coin(double, double&)'
154 C:\Users\rcc\Documents\Untitled1.cpp at this point in file
69 C:\Users\rcc\Documents\Untitled1.cpp too few arguments to function `double insert_coin(double, double&)'
159 C:\Users\rcc\Documents\Untitled1.cpp at this point in file
69 C:\Users\rcc\Documents\Untitled1.cpp too few arguments to function `double insert_coin(double, double&)'
164 C:\Users\rcc\Documents\Untitled1.cpp at this point in file
69 C:\Users\rcc\Documents\Untitled1.cpp too few arguments to function `double insert_coin(double, double&)'
169 C:\Users\rcc\Documents\Untitled1.cpp at this point in file
|
|
|
|
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: 0
|
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
|
|
|
|
Re: Need help with my C++ code [message #445226 is a reply to message #445210] |
Wed, 23 March 2011 21:03 |
_SSnipe_
Messages: 4121 Registered: May 2007 Location: Riverside Southern Califo...
Karma: 0
|
General (4 Stars) |
|
|
Hypnos wrote on Wed, 23 March 2011 15:16 | There's an "Other" section at the bottom where this should of been posted.
Glad you got it fixed though.
|
Ya but honestly hardly anyone checks that section
|
|
|
Re: Need help with my C++ code [message #445227 is a reply to message #445206] |
Wed, 23 March 2011 21:25 |
|
Dover
Messages: 2547 Registered: March 2006 Location: Monterey, California
Karma: 0
|
General (2 Stars) |
|
|
With good reason, because nobody wants to do your homework for you.
DarkDemin wrote on Thu, 03 August 2006 19:19 | Remember kids the internet is serious business.
|
|
|
|
Re: Need help with my C++ code [message #445231 is a reply to message #445206] |
Thu, 24 March 2011 01:46 |
|
reborn
Messages: 3231 Registered: September 2004 Location: uk - london
Karma: 0
|
General (3 Stars) |
|
|
You're still using global variables (although there seems little need, I believe you really want to use DEFINE instead), and there are functions which declare that you're using the std namespace, which is weird, because you've aleady added that at the top just under your includes.
|
|
|
|
Re: Need help with my C++ code [message #445235 is a reply to message #445206] |
Thu, 24 March 2011 02:12 |
|
reborn
Messages: 3231 Registered: September 2004 Location: uk - london
Karma: 0
|
General (3 Stars) |
|
|
Yeah, DP's right.
When you call the function you're not even asking for a return value:
if (string_pool == "quarter")
{
coin = quarter;
insert_coin(coin, money_pool);
}
If you was doing this:
if (string_pool == "quarter")
{
coin = quarter;
double return = insert_coin(coin, money_pool);
printf("%d\n", return); //or cout, whatever
}
Then I might understand, but even then, you would still be able to do:
if (string_pool == "quarter")
{
coin = quarter;
insert_coin(coin, money_pool);
printf("%d\n", money_pool); //or cout, whatever
}
I believe that is the point DP was trying to make to you.
|
|
|
|
Re: Need help with my C++ code [message #445264 is a reply to message #445259] |
Thu, 24 March 2011 20:50 |
_SSnipe_
Messages: 4121 Registered: May 2007 Location: Riverside Southern Califo...
Karma: 0
|
General (4 Stars) |
|
|
Sir Kane wrote on Thu, 24 March 2011 20:31 |
That's your problem right there!
|
You know I hear good things about this, but bad yet people like you say comments like that, whats wrong with it?
|
|
|
Re: Need help with my C++ code [message #445269 is a reply to message #445206] |
Fri, 25 March 2011 01:54 |
|
reborn
Messages: 3231 Registered: September 2004 Location: uk - london
Karma: 0
|
General (3 Stars) |
|
|
The std namepsace has a lot of identifiers, and they are called generic things like string or iterator. If you use another library then there could be a chance that it has the same identifier. Even if the other library doesn't use the same identifier at the moment, it could do in the future, so there's a conflict in your global namespace.
Really you should just do std::cin instead of using the global namespace, it also adds clarity to code and makes it easier to read.
However, I get the impression from Silent Kane that he doesn't like the std namespace itself, rather than what I just posted.
|
|
|
|
Re: Need help with my C++ code [message #445275 is a reply to message #445206] |
Fri, 25 March 2011 09:42 |
_SSnipe_
Messages: 4121 Registered: May 2007 Location: Riverside Southern Califo...
Karma: 0
|
General (4 Stars) |
|
|
Thanks guys, but maybe you guys can clarify something for me.
I am still confused between the difference of a call-by-value
example, void something(int a, int b)
compared to a call-by-reference (&)
example, void something(int& a, int& b)
the book sucks at explaining it.
[Updated on: Fri, 25 March 2011 09:42] Report message to a moderator
|
|
|
|
Re: Need help with my C++ code [message #445308 is a reply to message #445291] |
Sat, 26 March 2011 11:32 |
_SSnipe_
Messages: 4121 Registered: May 2007 Location: Riverside Southern Califo...
Karma: 0
|
General (4 Stars) |
|
|
Omar007 wrote on Fri, 25 March 2011 14:30 | I'm not a C++ master nor am I writing this down with any reference. This is from the top of my head so it may contain faulty information.
---
A call by value makes a duplicate and does not affect the original.
Call by reference modifies the original value you pass.
Call by value
void main()
{
int a = 10;
somefunction(a);
cout << a; //10
}
void somefunction(int a)
{
a = 20;
}
Call by reference
void main()
{
int a = 10;
somefunction(a);
cout << a; //20
}
void somefunction(int& a)
{
a = 20;
}
|
See, that makes much more scene, thanks
Another problem I am having is I am lost on the difference between a functions formal parameters and arguments
|
|
|
Re: Need help with my C++ code [message #445313 is a reply to message #445206] |
Sat, 26 March 2011 17:31 |
|
Omar007
Messages: 1711 Registered: December 2007 Location: Amsterdam
Karma: 0
|
General (1 Star) |
|
|
AFAIK 'arguments' is the same as 'actual parameters' so I''l be using that in my story
Formal parameters are the parameters you define in your function prototype or definition.
Actual parameters are the parameters you supply to the function.
For instance:
void someFunction(int a); //'a' is a formal parameter
void main()
{
int a = 1;
someFunction(a); //'a' is a actual parameter
}
When an actual parameter is of a lower type (eg the formal parameter is a long and the actual parameter an int) it will be automatically converted.
The other way around requires you to typecast
[Updated on: Sat, 26 March 2011 17:34] Report message to a moderator
|
|
|