void FUNCTIONS WITH DIFFERENT TYPES AND NUMBERS OF PARAMETERS
Ms Smart, a lecturer at a famous university, wants to compare the results of male and female students. She decides
to calculate four averages, namely
the average year mark for male students,
the average year mark for female students,
the average exam mark for male students
the average exam mark for female students.
Write a C++ program to help Ms Smart with this. The program is developed in three steps. You have to submit
printouts for 5c only.
Question 5a: Three reference parameters
Write a void function inputAndValidate where the results of one student are input and validated. The
function should have three reference parameters, namely a parameter that stores the sex of the student, i.e. male or
female, as a character ‘m’ or ‘f’ (thus of type char), a parameter that stores the year mark of the student (of type
int) and a parameter that stores the exam mark of the student (of type int).

// Assignment 2 Question 5a
#include <iostream>
using namespace std;
// The required function inputAndValidate should be inserted here.
void inputAndValidate (char Sex, int YearMark, int ExamMark){

char sex;
int yearMark, examMark;

do {cout << "Please enter your sex, m if you are male and f if you are female ";
    cin >> sex;
    cout << "This student is ";
if (sex == 'm')
cout << "male and has a year mark of";
else
cout << "female and has a year mark of ";
cout << yearMark << " and an exam mark of "
<< examMark << endl;}
while (sex != 'm' || sex != 'f');
}

int main( )
{
int yearMark, examMark;
char sex;
inputAndValidate(sex, yearMark, examMark);
cout << "This student is ";
if (sex == 'm')
cout << "male and has a year mark of";
else
cout << "female and has a year mark of ";
cout << yearMark << " and an exam mark of "
<< examMark << endl;
return 0;
}

What is actually going wrong? I can see a number of issues, but I would like to know what you specifically are trying to fix.

What is actually going wrong? I can see a number of issues, but I would like to know what you specifically are trying to fix.

Thanks for the quick reply. Basically I'm trying this:

Write a void function inputAndValidate where the results of one student are input and validated. The
function should have three reference parameters, namely a parameter that stores the sex of the student, i.e. male or
female, as a character ‘m’ or ‘f’ (thus of type char), a parameter that stores the year mark of the student (of type
int) and a parameter that stores the exam mark of the student (of type int).
98
The first parameter has to be validated by using a do..while loop: the program should keep on displaying a
prompting message until the user enters either m or f.
We give the main function below. Test your program to make sure that it works correctly.

Oh, I understand this. What I mean is, what does the code you've already written do? does it compile? If it does, does it run, and how does it behave when it runs?

As I said, I've already noted a number of issues, starting with the parameter list. The arguments are all supposed to be reference values, meaning the start of the function should look like this:

void inputAndValidate (char& Sex, int& YearMark, int& ExamMark)

The ampersand means that they are reference values, that it to say, the variable refers to the argument itself, not just it's value. This lets the function change the actual value of the variables passed to it.

Okay what happens is that if I type m or f the function works, but asks me to enter a value again. If I type a wrong value it does not recognize that instead it gives me a value for male or female.

I did try to put the refs earlier but then I had other issues. Tell me if I try this should I have the void function at the top and at the bottom of my main function ?

whatever you have done in main part of the program, You have repeated the same thing in thing in that function.


void inputAndValidate (char Sex, int YearMark, int ExamMark){
char sex;
int yearMark, examMark;

no need to redeclare
your first error you have Sex YearMark and ExamMark in the parameter list of inputAndValidate() while you are using lowercase of s in Sex y in YearMark and e in ExamMark.

2)I am unable to understand are you taking input from keyboard(user) if yes you haven't done cin.
if no then you should pass parameter values in your inputAndValidate() function not variables without any value.

3)when you pass value of sex whether its m of f in inputAndValidate() funtion then no need to use do while function in inputAndValidate() funtion. I am not understanding why you are using this loop, as its already confirmed at the time of sending parameter to the function until you are not taking input from user(keyboard).

It doesn't really matter if it is above or below the main() function, though if it is below it, you'd have to add a separate function prototype before the main() function. For now, it's easier to keep it above the main() .

What sort of problems do you get when you declare the variables as references? Be as specific as possible, and if you can, paste in the error messages you get.

What cse.avinash said about the re-declarations is correct; you don't need or want to declare those variables there. You want to use the parameters as the values for the input, not local variables.

I could also add that while you are reading in the student's sex from cin , you don't read in the year and exam marks. As a result, when you go to print them out, those will be zero (at best) or garbage (at worst).

It doesn't really matter if it is above or below the main() function, though if it is below it, you'd have to add a separate function prototype before the main() function. For now, it's easier to keep it above the main() .

What sort of problems do you get when you declare the variables as references? Be as specific as possible, and if you can, paste in the error messages you get.

While I give this program a shot again, Just how do I go about approaching this one. I assume the row of number is Sports and Other activities.

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

Question 4c: 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

Hey Vusuzumi, I'm just catching up with this topic, and I think you've started at least three threads for the same program and basic flow questions (about handling your gender-validation issue). If any are now resolved, please mark those threads "solved". And in the future, please follow up with your revised code and new questions in the same thread, so those of us trying to help aren't chasing your thought-flow around, and potentially answering your question in one thread when somebody else has already answered in another thread. Thanks!

As far as this one, I'll reword the assignment: you need to edit your existing main() function so that it first prompts the user for the number of students (10 in the first line of the example input), then puts the rest of the functionality into a loop which executes that many times. Finally, add a couple of lines into your code so that a total-amount-of-money is accumulated for all ten (or how ever many) students is computed, so that the class teacher knows how much money he/she should have once all ten (or how ever many) students have paid the proper fee.

Does that help?

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.