This is a simple one, but don't know why it won't work.

Question

VALUE RETURNING FUNCTIONS WITH ONE OR MORE VALUE PARAMETERS
The pupils of the Annandale High School have to pay a fee for each extramural activity that they want to partake in.
These activities include sport, and other activities such as chess, ballet, etc. The fee for sport activities is R120 p/m
and for other activities, R100 p/m. Pupils get discount if they partake in more than 3 activities. A maximum of 7
activities is allowed. For partaking in 4 or 5 extramural activities, they get a discount of R75 on the total amount
payable for the month. For partaking in 6 or 7 extramural activities, they get a discount of R125. The school also has
a credit system, where pupils get credits for various tasks performed. Taking part in extramural activities also adds
to their number of credits. Your task is to write a program that calculates the pupil’s monthly fee, including the
discount if applicable. The program must also calculate the number of credits that the pupil obtained by partaking in
extramural activities. This process is repeated for the whole class, and the total amount that must be collected by the
teacher must be calculated and displayed. We will develop the program in stages.
You must only submit printouts for question 4c.
Question 4a: Start small
We give the main function below. Your task is to write a function, namely calcFee, that returns a variable of
type float. There are two value parameters of type int namely the number of sport activities and the number of
other activities that the pupil partake in. You’ll notice that we have already defined the const variables that you
will need. The program must calculate the monthly fee for a pupil. No discounts will be handled in this function –
we will deal with that in 4(b). Test your program with different input values, but do not submit any printouts.
Program:

//Assignment 2 Question 4a
#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
int main()
{
int nrSport, nrOther, number;
float totFee = 0, newFee = 0, credits = 0, totSport = 0, totOther = 0;
do
{
cout << "A total number of 7 activities are allowed:-" ;
cout << 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);
96
totFee = calcFee(nrSport, nrOther);
cout.setf(ios::fixed);
cout.precision(2);
cout << endl << "The monthly fee payable is : R" << totFee ;
cout << endl << endl;
return 0;
}

My answer

//Your function calcFee must be inserted here
float calcFee (feeSport + feeOther);
int main()
{
int nrSport, nrOther, number;
float totFee = 0, newFee = 0, credits = 0, totSport = 0, totOther = 0;
do
{
cout << "A total number of 7 activities are allowed:-" ;
cout << 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;
return 0;
}

Recommended Answers

All 9 Replies

Here are all the errors with your code:

02.cpp:2:16: error: ‘feeSport’ was not declared in this scope
02.cpp:2:27: error: ‘feeOther’ was not declared in this scope
02.cpp: In function ‘int main()’:
02.cpp:9:1: error: ‘cout’ was not declared in this scope
02.cpp:10:9: error: ‘endl’ was not declared in this scope
02.cpp:12:1: error: ‘cin’ was not declared in this scope
02.cpp:16:35: error: ‘calcFee’ cannot be used as a function
02.cpp:17:1: error: ‘cout’ was not declared in this scope
02.cpp:17:11: error: ‘ios’ has not been declared
02.cpp:19:9: error: ‘endl’ was not declared in this scope

So, I assume you meant "calcFee cannot be used as a function". This issue with that is that you have not created a function called calcFee.

Read this page which explains how to write a function.
http://cplusplus.com/doc/tutorial/functions/

Here is why you should not use Dev-C++
http://cplusplus.com/articles/36vU7k9E/

Member Avatar for Mouche

Hello. First of all, please use CODE tags in your post so your code is more readable.

Is this your attempt?

float calcFee (feeSport + feeOther);

That looks like an attempt at a function header, but you do not have a full function there.

The function should look similar to this:

float calcFee(int sports_num, int other_num)
{
   // calculate fee using the constants and parameters
   return fee;
}

Next, you need to come up with the equation for the fee.
An example equation in a different scenario would be this:

const float flat_fee = 500.00;
const float price_per_credit = 250.00;
int credits = 10; // You would get this from the user
float tuition = credits * price_per_credit + flat_fee;

Give it a shot and then post your attempt and results.

Considering I'm working with floats, is it okay to put int in my functions?

Yes. If your function says it will return a float you should return a float, but if you want some int in your function, go ahead.

for some reason if i use int i get errors elsewhere but when i use float i get an error straight away.

//Assignment 2 Question 4a
#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(float feeSport, float feeOther)
{
 float calcFee = feeSport + feeOther;
return calcFee;
}
int main()
{
int nrSport, nrOther, number;
float totFee = 0, newFee = 0, credits = 0, totSport = 0, totOther = 0;
do
{
cout << "A total number of 7 activities are allowed:-" ;
cout << 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;
return 0;
}
Member Avatar for Mouche

That's a start. In the future, please post the errors that you're getting.

You are using the same name for the calcFee() function and the calcFee float variable inside the function. They should be different. For example, name the float inside the function "fee".

Also, right now, you are calculating the fee by adding the parameters which are feeSport and feeOther. But you are not giving the function the fees of those activities; you are giving the function the number of each type of activities.

If nrSport is 2 in your main() function, then when the calcFee() function is called, feeSport will become 2, which doesn't make sense. The parameters should be the numbers of each activities.

I am getting even more confused now the c++ language is becoming harder by the day to understand, can you not write how you would work the problem out and explain on top. I don't mean to be having you do my work for me but I have been trying to solve this for the past 5 hours, I can't afford to spend more hours on this as I have other assignments due soon.

When you done if ever you have time please tell me how one gets to learn and understand c++, I'm not the dumbest guy in the world but this course sure makes me feel like one.

Thanks mate! I really appreciate your help thus far.

Member Avatar for Mouche

We're not here to do your work for you no matter how busy you are. We're here to help. I made some suggestions on how to fix your code, so follow them.

You're nearly finished with the calcFee function. Come up with the mathematical equation for the fee using the constants you were given on paper first and then translate that into code.

I learn programming languages the best by starting with a book. After I understand the basic mechanics of the language and the syntax, I try to complete some simple projects in the language. For me, practicing the language by doing projects is the best way to get good at the language. When I don't know how to do something in a language, I look it up online. I also look at snippets online and forums to see how other people are using the language.

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.