Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

YoNeX

macrumors regular
Apr 29, 2005
141
0
Remember you will spend around 20% coding, and 80% debugging. So stare at your code for a little bit, and scream out some obscentity, and you will eventually get it. Worked for me. :D

By the way, your function call, its suppose to be (int, double, double, double) or make quanBreak an int instead of a double.
 

Heath

macrumors regular
Aug 19, 2005
133
0
Canada
I'll assume you mean this line:

breakfastTotal = calcDiscount (breakfastMeals, breakfast, bDiscount, quanBreak);

I ran it through a compiler and found a few things.
If you notice, what you're getting is actually a warning, not an error (regarding this line anyway). You should correct this, however it will not stop your compile. However the error that IS stopping it is the case of pesky typos and a scope issue.

In your function, you are using a variable called costEach, but in the header where you define the function you have called it eachCost, the compiler hates that!
Also you are typing to use the variable bDiscount in your function; the way that you currently have it set up you function cannot see this variable to use it since it is not declared anywhere inside the function.

There are a few ways to solve these issues (the warning, naming, and scope issue) I'll let you puzzle them out first. Come back if you get stuck :)
 

sixstorm

macrumors regular
Original poster
Jan 16, 2006
212
0
Nashville, TN
YoNeX said:
Remember you will spend around 20% coding, and 80% debugging. So stare at your code for a little bit, and scream out some obscentity, and you will eventually get it. Worked for me. :D

By the way, your function call, its suppose to be (int, double, double, double) or make quanBreak an int instead of a double.

Haha. Let me drop this here:

double calcDiscount (int quantity, double costEach, double discountPerCent, int orderMoreThan);

int main()

{

double breakfast = 5.50;
double bDiscount = .10;
double quanBreak = 10;
int breakfastMeals;

cout << "Enter the number of meals: " << endl;
cin >> breakfastMeals;

breakfastTotal = calcDiscount (breakfastMeals, breakfast, bDiscount, quanBreak);
cout << "Your total is: " << breakfastTotal;
return 0;
}

double calcDiscount (int quantity, double costEach, double discountPerCent, double orderMoreThan)

FUNCTION

It's having a problem in the function call, saying that it has a warning assignment to 'int' from 'double'. All of my parameters are in order . . .
 

sixstorm

macrumors regular
Original poster
Jan 16, 2006
212
0
Nashville, TN
Heath said:
Also you are typing to use the variable bDiscount in your function; the way that you currently have it set up you function cannot see this variable to use it since it is not declared anywhere inside the function.

So bDiscount, an identifier, HAS to be within the function? That kinda doesn't make sense because you would want the function to use whatever is in the Main section right?
 

Heath

macrumors regular
Aug 19, 2005
133
0
Canada
sixstorm said:
So bDiscount, an identifier, HAS to be within the function? That kinda doesn't make sense because you would want the function to use whatever is in the Main section right?

If you want to access it then it either has to be inside the function, or globally accessable.
I'll attempt to steer you away from global variables now however. :)
One thing you might notice if you stare at the code, is that you are not using bDiscount anywhere in main, other than to pass it to your function. You could consider moving it inside the function altogether.
Also you are passing in the value of bDiscount using discountPerCent, but trying to use bDiscount instead, I'm not sure why, but that is another thing you might want to look at.
 

sixstorm

macrumors regular
Original poster
Jan 16, 2006
212
0
Nashville, TN
Heath said:
If you want to access it then it either has to be inside the function, or globally accessable.
I'll attempt to steer you away from global variables now however. :)
One thing you might notice if you stare at the code, is that you are not using bDiscount anywhere in main, other than to pass it to your function. You could consider moving it inside the function altogether.
Also you are passing in the value of bDiscount using discountPerCent, but trying to use bDiscount instead, I'm not sure why, but that is another thing you might want to look at.

In this project, it is instructed to have the declaration as it is already, so I gotta work around that. I've made breakfast=5.50, bDiscount and breakQuan constants . . . but that doesnt help. Hope I'm making sense. How much harder would it be to make it globally accessible? Identify it before int main()?
 

Heath

macrumors regular
Aug 19, 2005
133
0
Canada
sixstorm said:
In this project, it is instructed to have the declaration as it is already, so I gotta work around that. I've made breakfast=5.50, bDiscount and breakQuan constants . . . but that doesnt help. Hope I'm making sense. How much harder would it be to make it globally accessible? Identify it before int main()?

I would suggest you simply change the function line that says:

discountAmt = totalOne * bDiscount;

to use the value you have passed in instead of trying to use the bDiscount variable. You have already passed in the value, you just need to use it.
 

bousozoku

Moderator emeritus
Jun 25, 2002
16,120
2,397
Lard
Did you clean up the costEach/eachCost misspelling?

After that and making sure the semicolons were only where they should be, it's able to be compiled and runs.
 

sixstorm

macrumors regular
Original poster
Jan 16, 2006
212
0
Nashville, TN
bousozoku said:
Did you clean up the costEach/eachCost misspelling?

After that and making sure the semicolons were only where they should be, it's able to be compiled and runs.

Yeah, I cleaned that up. I also changed one thing in the function, yet I'm still getting warnings/problems in the function call. If it's just one or two stupid semicolons, I'm going to freak out.
 

YoNeX

macrumors regular
Apr 29, 2005
141
0
Code (Slightly Optimized) said:
#include <iostream>
using namespace std;

//Constants
const double BREAKFAST = 5.5;
const double BDISCOUNT = 0.1;
const double QUANBREAK = 10;

double calcDiscount(int quantity, double costEach, double discountPerCent,
double orderMoreThan);

int main()
{
double breakfastTotal;
int breakfastMeals;

cout << "Enter the number of Breakfast Meals ordered: ";
cin >> breakfastMeals;

breakfastTotal = calcDiscount(breakfastMeals, BREAKFAST, BDISCOUNT,
QUANBREAK);

cout << "Your total is: " << breakfastTotal << endl;

return 0;
}

double calcDiscount(int quantity, double costEach, double discountPerCent,
double orderMoreThan)
{
double discountAmt, totalOne, subTotal;

totalOne = quantity * costEach;

if(quantity >= orderMoreThan)
{
return (totalOne - (totalOne * BDISCOUNT));
}
return (totalOne * BDISCOUNT);
}

Slightly optimized. Should work, but haven't ran it through a compiler.:eek:
 

uaaerospace

macrumors 6502
Feb 15, 2005
396
0
Alabama
sixstorm said:
It's having a problem in the function call, saying that it has a warning assignment to 'int' from 'double'. All of my parameters are in order . . .
The problem is that in the function declaration you have the last variable set as one type and in the function definition you have the last variable set as another type. Those types must agree!

Think about the warning being given by the compiler. That warning actually tells you EXACTLY what is wrong with your code.
 

uaaerospace

macrumors 6502
Feb 15, 2005
396
0
Alabama
Heath said:
I'll attempt to steer you away from global variables now however. :)

I second that attempt. :cool:

"With great power comes great responsibility"

Don't open yourself for additional careless mistakes. Know what you are doing and why you are doing it.
 

sixstorm

macrumors regular
Original poster
Jan 16, 2006
212
0
Nashville, TN
YoNeX said:
Slightly optimized. Should work, but haven't ran it through a compiler.:eek:

Works on XCode with 1 warning but the Linux server that I remotely access is still having the same problems as before with the 'int' to 'double' crap. :mad:

EDIT: I angrily retyped the entire thing and viola, I got it to compile. I think one thing is where I put my constants, making orderMoreThan a double instead of a constant (weird) and prolly a stroke of luck. Now onto the other parts of the program! Thanks a lot guys, this means a lot.
 

uaaerospace

macrumors 6502
Feb 15, 2005
396
0
Alabama
sixstorm said:
Works on XCode with 1 warning but the Linux server that I remotely access is still having the same problems as before with the 'int' to 'double' crap. :mad:

There are 2 warnings in Xcode....both involve unused variables.

Can you post the code from the Linux server so that we can see what you have fixed and what remains? It would also be helpful to know the exact message from the Linux compiler and the referenced line number.
 

Heath

macrumors regular
Aug 19, 2005
133
0
Canada
sixstorm said:
Works on XCode with 1 warning but the Linux server that I remotely access is still having the same problems as before with the 'int' to 'double' crap. :mad:

And it will until it's fixed. :)
Double (ha ha) check your argument types against what you pass in, try to rely on the compiler to do as little converting as you can. Even better would be to make the types match, and where you cannot, explicitly state your intentions with a cast to the appropriate type. That way later on when you revisit the code you'll know that a conversion is being done on purpose and which types are involved.

Once you get it compiling, there are a couple other things you might want to look into. One is that your breakast price total is not being totalled correctly.
When you run it then number you get back on the screen is the discount, not the total cost. Also, when you type in the number of meals, there is no checking done on the input to make sure it is in fact a number. I'm not sure how far along you are in learning so that might be a bit beyond where you sit now.
However, the one check you should always do when writing functions is to make sure that what you get passed in is what you expect. Always try to check your passed in function arguments to make sure they are valid. In your example I could type in -5 for the number of meals and the function would give a nonsensical (in this context) answer. In your case one check could be that all the arguments are greater than 0.
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,566
sixstorm said:
If I'm not being a PITA for ya, could you explain why I wouldn't put it there? Just trying to learn . . .

What kind of statements are there?

There are expression statements.
There are compound statements.
There are if, do/while, while, for, switch, goto and return statements.
And there are empty statements.

Please describe exactly what an empty statement looks like, and where you used one in your code, and why you most definitely didn't want to use it where you used it.
 

sixstorm

macrumors regular
Original poster
Jan 16, 2006
212
0
Nashville, TN
gnasher729 said:
What kind of statements are there?

There are expression statements.
There are compound statements.
There are if, do/while, while, for, switch, goto and return statements.
And there are empty statements.

Please describe exactly what an empty statement looks like, and where you used one in your code, and why you most definitely didn't want to use it where you used it.

While I'm not that advanced yet, I still have a little trouble with my syntaxes. I'm going to start back on this project again this afternoon, so keep an eye out on this thread. I'm sure I'll have another couple of questions. Thanks guys!
 

sixstorm

macrumors regular
Original poster
Jan 16, 2006
212
0
Nashville, TN
Hello again. Got one quick Q, here is the code:

#include <iostream>
using namespace std;

//Constants
const double BREAKFAST = 5.5;
const double BDISCOUNT = 0.1;
const double QUANBREAK = 10;
const double LUNCH = 9.5;
const double LDISCOUNT = .15;
const double QUANLUN = 15;
const double DINNER = 16.5;
const double DINDISCOUNT = .12;
const double QUANDIN = 8;
double basicTotal, basicDiscount, costAfterDiscount;

double calcDiscount(int quantity, double costEach, double discountPerCent,
double orderMoreThan);

int main()
{
double breakfastTotal, lunchTotal, dinnerTotal;
int breakfastMeals, lunchMeals, dinnerMeals;

cout << "Enter the number of Breakfast Meals ordered: ";
cin >> breakfastMeals;
breakfastTotal = calcDiscount(breakfastMeals, BREAKFAST, BDISCOUNT,
QUANBREAK);

cout << "Enter the number of Lunch Meals ordered: ";
cin >> lunchMeals;
lunchTotal = calcDiscount(lunchMeals, LUNCH, LDISCOUNT, QUANLUN);

cout << "Enter the number of Dinner Meals ordered: ";
cin >> dinnerMeals;
dinnerTotal = calcDiscount(dinnerMeals, DINNER, DINDISCOUNT, QUANDIN);

cout << "Your breakfast total is: " << breakfastTotal << endl;
cout << "Your lunch total is: " << lunchTotal << endl;
cout << "Your dinner total is: " << dinnerTotal << endl;

return 0;
}

double calcDiscount(int quantity, double costEach, double discountPerCent,
double orderMoreThan)
{
basicTotal = quantity * costEach;

if(quantity >= orderMoreThan)
{
basicDiscount = basicTotal * discountPerCent;
costAfterDiscount = basicTotal - basicDiscount;
return costAfterDiscount;
}
return basicTotal;
}

The outcome of this program needs to display the total before discount, how much is saved (total - discount) and the total after the discount. Since the user is going to input the quantity and run it through a function, can there be more than one thing returned? Also, in this project, I can only have that calcDiscount function, no other functions can be used. How can I get it to where I can display these numbers within my main()? For example:

Total Before Discount = $xx.xx
Discount = $xx.xx
Total After Discount = $xx.xx
 

Heath

macrumors regular
Aug 19, 2005
133
0
Canada
sixstorm said:
Hello again. Got one quick Q, here is the code:



The outcome of this program needs to display the total before discount, how much is saved (total - discount) and the total after the discount. Since the user is going to input the quantity and run it through a function, can there be more than one thing returned? Also, in this project, I can only have that calcDiscount function, no other functions can be used. How can I get it to where I can display these numbers within my main()? For example:

Total Before Discount = $xx.xx
Discount = $xx.xx
Total After Discount = $xx.xx

Ok, maybe I'm missing something, but couldn't you just go:
cout << "Total Before Discount = $" << // Total before discount calculation here << endl;
cout << "Discount = $" << // discount amount here
cout << "Total After Discount = $" << // Other total here

For each meal type?
This would be better siuted to go into some function, but if you are not allowed to have other functions this seems like the simplest approach.
 

sixstorm

macrumors regular
Original poster
Jan 16, 2006
212
0
Nashville, TN
Heath said:
Ok, maybe I'm missing something, but couldn't you just go:
cout << "Total Before Discount = $" << // Total before discount calculation here << endl;
cout << "Discount = $" << // discount amount here
cout << "Total After Discount = $" << // Other total here

For each meal type?
This would be better siuted to go into some function, but if you are not allowed to have other functions this seems like the simplest approach.

EDIT: NM, I figured it out. Just after doing the function, I put breakfastDiscount = basicDiscount. This takes that temporary memory cell (right after running breakfastMeals through the function) and displays it!!! At least I figured something of this out myself lol.
 

Heath

macrumors regular
Aug 19, 2005
133
0
Canada
sixstorm said:
There has got to be an easier/better way to do that. If you are getting a number through your function, you should be able to display or call back that number right? If you do as you suggests, you are basically doing the function all over again in your main().

Oh I agree that it's most definitely not the way you should approach the problem. However if you are only allowed to have one function then your options become limited. A function however should ideally be short, easy to read and only do one thing. The function you have has a name of calcDiscount, to me that implies that is exactly what the function should do. No more and no less. If you start trying to cram other logic in there you head on down the trail of 'spaghetti code' and you don't want to go there. As for simpilcity.. well it can't get much simpler than that you already have the numbers, you just need to print them out basically.
 

YoNeX

macrumors regular
Apr 29, 2005
141
0
Yes, when starting off, I highly suggest making more functions that do simple task, rather than a function that does a lot of stuff. Sure, it may be more inefficent and not optimized, but you are trying to learn. But once you get better at it, then you can start optimizing your code, and using little tricks. Until then, try using more functions to do your task, because debugging optimized code is a pain at times.
 

sixstorm

macrumors regular
Original poster
Jan 16, 2006
212
0
Nashville, TN
Well, I'm finally done with the project, thanks to your help (all of you). Keep this thread open . . . the semester isn't over yet!!! :D :D :D
 

sixstorm

macrumors regular
Original poster
Jan 16, 2006
212
0
Nashville, TN
Here's another problem from yours truely. It keeps telling me that calcComm can't be used as a function . . . but somehow I've got the right syntax for it. Well heck, I prolly don't since it's not working huh? Here's the code:

/*
* Pass6
*/

/* People get paid a base salary, plus a percentage of all sales over the base sales amount.
1) Read 3 digit salesman ID number, a float base salary and sales amount for each category.
2) Using a function, compute the commission earned in each category.
3) Print the ID number and base salary.
4) For each category, print description, sales amount, and commission amount.
5) Print total commission paid to the employee and total paid.
6) Continue looping until -999 is inputted for Salesman Number.
*/

#include <iostream>
#include <iomanip>
using namespace std;

/* Universal Floats */
float totalPcCom, totalMemCom, totalZipCom, totalPrintCom, baseSalary, pcSales, memCardSales, zipDriveSales, printerSales, totalCommission;

/* Function Prototype */
double calcComm(pcSales, memCardSales, zipDriveSales, printerSales);

int main ()
{

/* Everything has to be in a loop!!!! */
int salesID;
do
{
/* Output for 3 digit salesman ID number, float base salary and sales amount for each category. */
cout << "Please Enter Salesman ID number or -999 to terminate: " << endl;
cin >> salesID;
cout << "Please Enter Base Salary: " << endl;
cin >> baseSalary;
cout << "Please Enter Personal Computer Sales: " << endl;
cin >> pcSales;
cout << "Please Enter Memory Card Sales: " << endl;
cin >> memCardSales;
cout << "Please Enter Zip Drive Sales: " << endl;
cin >> zipDriveSales;
cout << "Please Enter Printer Sales: " << endl;
cin >> printerSales;

/* Function and Computation Time!!! */
totalCommission = calcComm (pcSales, memCardSales, zipDriveSales, printerSales);

/* Displaying the information for the current salesman */
cout << "Saleman ID # " << salesID << endl;
cout << "Base Salary: " << baseSalary << endl;
cout << "Total PC Commission: " << totalPcCom << endl;
}

/* Checking again if id!=-999 */
while (salesID!=-999);

return 0;
}


double calcCommission (baseSalary, pcSales, memCardSales, zipDriveSales, printerSales)

{
/* Check and see if salesman gets commission from PCs
If so, commission is calculated. If not, commission
is left at zero. */
if (pcSales>=4000)
return totalPcCom = (pcSales - 4000) * .10;
else
return totalPcCom = 0;

/* Check and see about other commissions */
if (memCardSales>=1000)
return totalMemCom = (memCardSales - 1000) * .05;
else
return totalMemCom = 0;

if (zipDriveSales>=800)
return totalZipCom = (zipDriveSales - 800) * .04;
else
return totalZipCom = 0;

if (printerSales>=2000)
return totalPrintCom = (printerSales - 2000) * .08;
else
return totalPrintCom = 0;
}
 

bousozoku

Moderator emeritus
Jun 25, 2002
16,120
2,397
Lard
You seem to be wanting to calculate all of the commissions at once by collecting the information prior to calling the function but you're using return, which escapes the rest of the function.

You could either create separate functions for each type of commission so that you return the proper value for each or you could remove return and use pointers to allow the values to be inserted into the original variables.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.