I decided to give this a go again today.

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;
}

Here is what I've done:

//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 (int NumSport, int NumOther);

float totFee;

totFee = NumSport * 120 + NumOther * 100;

return (totFee);



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;
}

I think my problem here is creating a function or the equation for my function.

Recommended Answers

All 8 Replies

Guys feel free to scrutinize how I've done and show me other ways I could have done it, of cause following what the question required us to do. My problem here was that I did not have the curly brackets around my my equation. Let us have a look how it's done or how it could be done.

//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 (int NumSport, int NumOther){

float totFee;

totFee = NumSport * 120 + NumOther * 100;

return totFee;}



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;
}

EDIT: Apparently, you posted your corrected version just minutes before I posted this answer. You seem to have the basic idea now, yes.

You're on the right track from where you'd been, but you still haven't quite grasped the function syntax. Specifically, you need to remove the semi-colon at the end of the function header, and put braces around the body of the function:

float calcFee (int NumSport, int NumOther)
{
    float totFee;

    totFee = NumSport * 120 + NumOther * 100;

    return (totFee);
}

Basically, when you write a function, the form of it goes something like this:

<return-type> <function-name> ( <parameter-list> )
{  
    <body>;
}

You always have to have the braces around the body of the function.

Here is a more or less working version of your code (some unneeded variables have been removed):

//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 (int NumSport, int NumOther)
{
    float totFee;

    totFee = NumSport * 120 + NumOther * 100;

    return (totFee);
}


int main()
{
    int nrSport, nrOther;
    float totFee = 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;
}

This will compile and run; I leave it to you to determine if it actually solves the assignment's requirements.

EDIT: Apparently, you posted your corrected version just minutes before I posted this answer. You seem to have the basic idea now, yes.

You're on the right track from where you'd been, but you still haven't quite grasped the function syntax. Specifically, you need to remove the semi-colon at the end of the function header, and put braces around the body of the function:

float calcFee (int NumSport, int NumOther)
{
    float totFee;

    totFee = NumSport * 120 + NumOther * 100;

    return (totFee);
}

Basically, when you write a function, the form of it goes something like this:

<return-type> <function-name> ( <parameter-list> )
{  
    <body>;
}

You always have to have the braces around the body of the function.

Here is a more or less working version of your code (some unneeded variables have been removed):

//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 (int NumSport, int NumOther)
{
    float totFee;

    totFee = NumSport * 120 + NumOther * 100;

    return (totFee);
}


int main()
{
    int nrSport, nrOther;
    float totFee = 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;
}

This will compile and run; I leave it to you to determine if it actually solves the assignment's requirements.

Thanks bro! I suppose I should give myself a pat on the back for this one atleast now I have an idea of what a function is.

Now I have to finish the rest of the questions and go on to others.

Tell me, is there always someone ready to answer questions on this forum cause I kind of work 24hrs?

Not really; it isn't like a paid help line would be where there is an guarantee of a response. You might get an answer in two minutes, or in two days, or never. That's just the nature of a public forum.

Now, there are members from all around the world, and for a basic question odds are good someone can get to it fairly quickly, but I wouldn't say that it is something you should rely upon in a real time crunch, especially as the questions get more complicated.

The discounts very how should I make the equation on my discFee function. Should I maybe use an if statement?

//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
// your function discFee must be inserted here
// your function calcCredit must be inserted here
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 ;
COS1511/101/3/2011
97
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;
}

As a side note, you really need to learn how to indent your code for readability. While there are many different indent styles, and you're under no absolute obligation to use a specific one, it is important that you indent the code in some consistent manner that others will be able to follow easily. Fortunately, most IDEs (e.g., Code::Blocks, Visual C++ Express) and programming editors (e.g., EMACS, Notepad++, Geany) have an auto-indent feature of some sort, so you may be able to apply that and save a lot of effort.

As a side note, you really need to learn how to indent your code for readability. While there are many different indent styles, and you're under no absolute obligation to use a specific one, it is important that you indent the code in some consistent manner that others will be able to follow easily. Fortunately, most IDEs (e.g., Code::Blocks, Visual C++ Express) and programming editors (e.g., EMACS, Notepad++, Geany) have an auto-indent feature of some sort, so you may be able to apply that and save a lot of effort.

Will try sometime in the future. Please respond to my previous post.

Yes, you should use an if-then-else-if structure in your discFee() function. The usage of the function is provided at line 31 above -- that should give you a pretty good idea how the function should be declared: it will take three arguments instead of two.

Also, in your calcFee() function, why not use the constants feeSport and feeOther provided for you, instead of the "magic" numbers 100 and 120?

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.