-
November 25th, 2011, 06:18 PM
#1
Void functions
I'm doing some C++ homework, it's for an online class, and my ****ing teacher never answers me back about questions I have. Ultimately I'm having to do it all on my own basically. I feel like I'm close with this one (or maybe not), but a little mixed up. I'm getting an "in function int main" error but don't know where, and "void value not ignored as it ought to be" error on the line: "totalCommission = calcCommission (commission1, commission2, commission3, commission4)". Can anyone help?
------------------------------------------------------------------------------------------------
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
const double commission = 0.1;
double salesAmount = 0.0;
double totalCommission = 0.0;
double sale1 = 0.0;
double sale2 = 0.0;
double sale3 = 0.0;
double sale4 = 0.0;
double commission1 = 0.0;
double commission2 = 0.0;
double commission3 = 0.0;
double commission4 = 0.0;
void calcCommission(double, double, double, double);
void displayCommission();
void getTotalCommission();
cout << "Enter first saleman's total sales: ";
cin >> sale1;
cout << "Enter second saleman's total sales: ";
cin >> sale2;
cout << "Enter third saleman's total sales: ";
cin >> sale3;
cout << "Enter fourth saleman's total sales: ";
cin >> sale4;
salesAmount = sale1 + sale2 + sale3 + sale4;
cout << "Total sales: " << salesAmount << endl;
totalCommission = calcCommission (commission1, commission2, commission3, commission4);
cout << "Commission: " << totalCommission << endl;
system("pause");
return 0;
}
---------------------------------------------------------------------------------------------------
-
November 26th, 2011, 11:04 AM
#2
Re: Void functions
Originally Posted by JohnnyLime
I'm doing some C++ homework, it's for an online class, and my ****ing teacher never answers me back about questions I have. Ultimately I'm having to do it all on my own basically. I feel like I'm close with this one (or maybe not), but a little mixed up.
Use code tags when posting code.
First, even though this is valid syntax, move these definitions before main()
Code:
void calcCommission(double, double, double, double);
void displayCommission();
void getTotalCommission();
Second, look at your functions above -- what do they return? Do they return double? No. Now look how you're calling these functions in main()
Code:
totalCommission = calcCommission (commission1, commission2, commission3, commission4);
How can a function that returns "void" return a double (you assign the return value to a double called totalCommision)? It can't -- that's why you get an error.
Hopefully the fix should be obvious to you at this point.
Regards,
Paul McKenzie
-
November 26th, 2011, 11:10 AM
#3
Re: Void functions
First, please read the announcement "Before you Post". You need to post your code inside code tags so that it will retain the proper formatting. To put your code in code tags, type [ code] (without the space) then paste your code, then type [ /code] (again without the space).
Now to the problem, here's your code (in code tags) with my comments and the portions of your code they refer to in red:
Code:
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
const double commission = 0.1;
double salesAmount = 0.0;
double totalCommission = 0.0;
double sale1 = 0.0;
double sale2 = 0.0;
double sale3 = 0.0;
double sale4 = 0.0;
double commission1 = 0.0;
double commission2 = 0.0;
double commission3 = 0.0;
double commission4 = 0.0; // These three lines are function prototypes, and should not be inside the "main" function
// Also there is no body (implementation) of these functions, so any attempt to call them will
// result in an unresolved external
void calcCommission(double, double, double, double);
void displayCommission();
void getTotalCommission();
cout << "Enter first saleman's total sales: ";
cin >> sale1;
cout << "Enter second saleman's total sales: ";
cin >> sale2;
cout << "Enter third saleman's total sales: ";
cin >> sale3;
cout << "Enter fourth saleman's total sales: ";
cin >> sale4;
salesAmount = sale1 + sale2 + sale3 + sale4;
cout << "Total sales: " << salesAmount << endl; // Here you appear to be calling calcCommission, but there is no body for this function, so it will cause an
// unresolved external.
// Additionally, you are passing 4 variables into the function, but all of them are 0.0, so any calculations
// do on them will not give a valid answer.
// One other point here, your prototype for calcCommission says it returns a void - but this
// code is attempting to assign the return value to "totalCommission"
totalCommission = calcCommission (commission1, commission2, commission3, commission4);
cout << "Commission: " << totalCommission << endl;
system("pause");
return 0; }
Hope this helps.
Be sure to rate those who help!
-------------------------------------------------------------
Karl - WK5M
PP-ASEL-IA (N43CS)
PGP Key: 0xDB02E193
PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|