The code runs but the problem is that the the discount value is not calculated wrong.
Which part did I miss here.

Here is the question:

You now have to add a function discFee that has three value parameters - two of type int representing the
number of sport and other activities, and one of type float representing the monthly fee before discount. The
function returns the discounted fee of type float. Remember that the discount differs depending the number of
activities. You also have to add the function calcCredit that has two value parameters of type int, representing
the number of sport and other activities respectively. The function calculates the number of credits obtained in a
variable of type float. We give the changed main function below. Test your program with these input values, but
do not submit any printouts.

``````//Assignment 2 Question 4b
#include <iostream>
using namespace std;
const float feeSport = 120.00;
const float feeOther = 100.00;
const float discount = 50.00;
const float creditS = 0.75;
const float creditO = 0.50;
// your function calcFee must be inserted here
float calcFee (int NumSport, int NumOther){
float totFee;
totFee = NumSport * 120 + NumOther * 100;
return (totFee);}
// your function discFee must be inserted here
float discFee (int numSport, int numOther, float monthFee){

int newFee;

if ((numSport + numOther == 4 )|| (numSport + numOther == 5)){

float newFee;
float Disc = 75.00;
monthFee = (feeSport * numSport) + (feeOther * numOther);
newFee = monthFee - Disc;
}

if ((numSport + numOther == 6 )|| (numSport + numOther == 7)){

float newFee;
float Disc = 125.00;
monthFee = (feeSport * numSport) + (feeOther * numOther);
newFee = monthFee - Disc;
}

return (newFee);
}
// your function calcCredit must be inserted here
float calcCredit (int numSport, int numOther){

float credit;

credit = (numSport * creditS) + (numOther * creditO);

return (credit);
}
int main()
{
int nrSport, nrOther;
float totFee = 0, newFee = 0, credits = 0, totSport = 0, totOther = 0;
do
{
cout << "A total number of 7 activities are allowed:-" << endl <<endl;
cout << "Please enter the number of sport activities : " << endl;
cin >> nrSport;
cout << "Please enter the number of other activities : " << endl;
cin >> nrOther;
}while (nrSport + nrOther > 7);
totFee = calcFee(nrSport, nrOther);
cout.setf(ios::fixed);
cout.precision(2);
cout << endl << "The monthly fee payable is : R" << totFee ;
cout << endl << endl;
newFee = discFee(nrSport, nrOther, totFee);
cout << endl << "The discounted fee is : R" << newFee ;
cout << endl << endl;
credits = calcCredit(nrSport, nrOther);
cout << endl << "The credits obtained : " << credits ;
cout << endl << endl;
return 0;
}``````

It has to do with the additional declarations inside the `if` statements, and the rules for scoping of variables. By re-declaring the variable `newFee` inside of the `if` statements (as a different type, at that), you are masking the original declaration of `newFee` at the beginning of the function. Therefore, the `newFee` variables inside the `if` statements are entirely new variables, ones which are only in scope for the `if` statements they are inside. When the `if` statement ends, that variable vanishes, and the one scoped at the beginning of the function - which never gets set - becomes the one returned.

The solution is simply to eliminate the superfluous declarations. Here's a corrected version, with some additional fixes for you as well:

``````// your function discFee must be inserted here

// you only need to get numSport and numOther from the caller;
// monthFee is calculated inside the function
// returns -1 if the total number of activities is less than 0 or greater than 7
float discFee (int sports, int otherActivities){
float discount;

int activities = sports + otherActivities;

// monthFee is the calculated monthly fee, as per calcFee()
float monthlyFee = calcFee(sports, otherActivities);

// you only need to set the value of discount in the if statements
if ((activities >= 0) && (activities < 4)){
discount = 0.00;
}

if ((activities == 4 ) || (activities == 5)){
discount = 75.00;
}

else if ((activities == 6 ) || (activities == 7)){
discount = 125.00;
}
// all other values for activities are invalid,
// return an error
else {
return -1.00;
}

return (monthlyFee - discount);
}``````

It has to do with the additional declarations inside the `if` statements, and the rules for scoping of variables. By re-declaring the variable `newFee` inside of the `if` statements (as a different type, at that), you are masking the original declaration of `newFee` at the beginning of the function. Therefore, the `newFee` variables inside the `if` statements are entirely new variables, ones which are only in scope for the `if` statements they are inside. When the `if` statement ends, that variable vanishes, and the one scoped at the beginning of the function - which never gets set - becomes the one returned.

The solution is simply to eliminate the superfluous declarations. Here's a corrected version, with some additional fixes for you as well:

``````// your function discFee must be inserted here

// you only need to get numSport and numOther from the caller;
// monthFee is calculated inside the function
// returns -1 if the total number of activities is greater than 7
float discFee (int sports, int otherActivities){
float discount;

int activities = sports + otherActivities;

// monthFee is the calculated monthly fee, as per calcFee()
float monthlyFee = calcFee(sports, otherActivities);

// you only need to set the value of discount in the if statements
if ((activities == 4 )|| (activities == 5)){
discount = 75.00;
}

else if ((activities == 6 )|| (activities == 7)){
discount = 125.00;
}

else {
discount = 0.00;
}

return (monthlyFee - discount);
}``````

Hold on one second there mate! In the question they specifically say that there should be three parameters in the discount function, but I see you only have two. Please explain.

Sorry for the repeated edits; every time I saved it, I noticed something else that could be improved upon. I think that it's in a decent state now.

OK, I did mess up a bit; the professor is assuming that you already calculated the monthly fees, and are passing it into the the function. I missed that, but it's easy enough to fix:

``````// your function discFee must be inserted here

// returns -1 if the total number of activities is less than 0 or greater than 7
float discFee (int sports, int otherActivities, float monthlyFee){
float discount;

int activities = sports + otherActivities;

// you only need to set the value of discount in the if statements
if ((activities >= 0) && (activities < 4)){
discount = 0.00;
}

if ((activities == 4 ) || (activities == 5)){
discount = 75.00;
}

else if ((activities == 6 ) || (activities == 7)){
discount = 125.00;
}
// all other values for activities are invalid,
// return an error
else {
return -1.00;
}

return (monthlyFee - discount);
}``````

You don't need to calculated the monthly fee inside this function; it's already been done by `calcFee()` , and the result of that is what the `main()` function is passing into `discFee()` .

OK, I did mess up a bit; the professor is assuming that you already calculated the monthly fees, and are passing it into the the function. I missed that, but it's easy enough to fix:

``````// your function discFee must be inserted here

// returns -1 if the total number of activities is less than 0 or greater than 7
float discFee (int sports, int otherActivities, float monthlyFee){
float discount;

int activities = sports + otherActivities;

// you only need to set the value of discount in the if statements
if ((activities >= 0) && (activities < 4)){
discount = 0.00;
}

if ((activities == 4 ) || (activities == 5)){
discount = 75.00;
}

else if ((activities == 6 ) || (activities == 7)){
discount = 125.00;
}
// all other values for activities are invalid,
// return an error
else {
return -1.00;
}

return (monthlyFee - discount);
}``````

You don't need to calculated the monthly fee inside this function; it's already been done by `calcFee()` , and the result of that is what the `main()` function is passing into `discFee()` .

Thank this is exciting stuff, This is helping me to learn in a big way. I am now going to try your solution.

Just another thing, was there another way this could have been done without using if statements, seeing they never asked us to use it in our functions?

Thank you very much I really appreciate the help I'm getting from you. "I'll be back"

It could be done with a `switch()` statement, and very likely I would have done it that way if I were writing it myself, though I wasn't certain if you were familiar with those yet. There are also various esoteric tricks one could do with `while()` , or `for()` , or the ternary operator, but I wouldn't think your professor was expecting you to do something like that.

I can't think of any way to do it without some form of conditional statement, however.

OK, I did mess up a bit; the professor is assuming that you already calculated the monthly fees, and are passing it into the the function. I missed that, but it's easy enough to fix:

``````// your function discFee must be inserted here

// returns -1 if the total number of activities is less than 0 or greater than 7
float discFee (int sports, int otherActivities, float monthlyFee){
float discount;

int activities = sports + otherActivities;

// you only need to set the value of discount in the if statements
if ((activities >= 0) && (activities < 4)){
discount = 0.00;
}

if ((activities == 4 ) || (activities == 5)){
discount = 75.00;

}

else if ((activities == 6 ) || (activities == 7)){
discount = 125.00;
}
// all other values for activities are invalid,
// return an error
else {
return -1.00;
}

return (monthlyFee - discount);
}``````

You don't need to calculated the monthly fee inside this function; it's already been done by `calcFee()` , and the result of that is what the `main()` function is passing into `discFee()` .

Okay the is something still wrong here with the discount amount. the discounts should never be more than R125 but your function allows this. Can we look at the code again?

While we work out why the discouts are screwy, can you tell me what they are asking in this question? More specifically with the row of numbers that look random. What row represents what?

The main function is the one from the previous question.

Add a loop to the function
We do not give the main function here. You may use the main function in 4(b), but you need to adapt it for this
part of the question. Additionally, prompt the user to enter the number of pupils in the class, and add a loop in the
main function to read in the number of activities for the whole class. The program also has to calculate and display
the total amount of money that the teacher has to collect from the class. Define a float variable totClass for
this purpose and initialise it to 0. Test you program with the following input values and submit the printout of the
program and the output.
10
1 2
2 2
3 0
4 5
0 7
1 4
3 4
0 0
2 5
1 1
3 3

Hi Vusumuzi,
What do you mean by "discounts look screwy"? What inputs are you sending into the function, and what result are you getting back?

The "random numbers" look to me like they represent "number of students" (10) and "number of sports activities" and "number of other activities" for each student. There are 11 pairs of input values, because the row with 4 and 5 should generate an error and prompt the user to re-input the values.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.