acidrain
Executive Member
Hi guys,
I need some help with a specific section of my code: the question is as follows:
This is what i wrote and im sure there is an error with my updateCorrectTotal function since i get 0 and 0 as my output for forwards and backs so hoping you can point me in the right direction.
My code:
Thanks in advance
I need some help with a specific section of my code: the question is as follows:
Question 5b: Two value parameters and two reference parameters
Write a void function updateCorrectTotal that updates either the total number of points scored by forwards
or the total number of points scored by backs. There should be two value parameters: one stores the position of the
player (thus of type char) and one stores the number of points scored by that player (thus of type int). There
should furthermore be two reference parameters (both of type int), namely the two totals. Every time the function
is called, one of these totals will be changed by adding the number of points scored by the player - which total
depends on whether the position is F or B. We give the main function below. Test your program but do not submit
printouts.
// Question 5b Assignment 3
#include <iostream>
#include <string>
using namespace std;
// the required functions inputAndValidate and updateCorrectTotal should be
// inserted here
int main( )
{
char position;
int points, totForward, totBack;
40
totForward = 0; // total number of points scored by forwards
totBack = 0; // total number of points scored by backs
inputAndValidate(position, points);
updateCorrectTotal(position, points, totForward, totBack);
cout << "Total number of points scored by forwards: "
<< totForward << endl;
cout << "Total number of points scored by backs: "
<< totBack << endl;
return 0;
This is what i wrote and im sure there is an error with my updateCorrectTotal function since i get 0 and 0 as my output for forwards and backs so hoping you can point me in the right direction.
My code:
//Question 5a, Assignment 3
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
void inputAndValidate(char & position, int & points)
{
do
{
cout << "Input the players position (F or B): ";
cin >> position;
if (position != 'F' && position != 'B')
cout << "Please enter either F or B" << endl;
}
while ( position != 'F' && position != 'B' );
cout << "Input the players score: ";
cin >> points;
}
void updateCorrectTotal (int totForward, int totBack, char position, int points )
{
if ( position == 'F' )
totForward += points;
else
totBack += points;
}
int main()
{
char position;
int points, totForward, totBack;
totForward = 0;
totBack = 0;
inputAndValidate (position, points);
updateCorrectTotal (position, points, totForward, totBack);
cout << "Total Number of points scored by forwards: "
<< totForward << endl;
cout << "Total number of points scored by backs: "
<< totBack << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
Thanks in advance