Quick help with void function (C++)

acidrain

Executive Member
Joined
Jan 7, 2007
Messages
6,965
Reaction score
1,762
Location
At a computer
Hi guys,

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
 
I don't really know any C, but just looking at it, I'd say the function is copying the points, forward and position value into a function specific local variable and changes that.

Your original vars in the main function are therefore still at what they were when you intialized it.

You'd have to create pointers to the int to fix it.
 
I don't really know any C, but just looking at it, I'd say the function is copying the points, forward and position value into a function specific local variable and changes that.

Your original vars in the main function are therefore still at what they were when you intialized it.

You'd have to create pointers to the int to fix it.

Guess since you dont know C very well, asking for an example is a bit too much?
 
Guess since you dont know C very well, asking for an example is a bit too much?
Nope, can't give an example in code. But basically what happens is each function copies all the variables it gets passed. It then works on the copies leaving the variable you see in the main function untouched.

I think you need to put a * somewhere...but I really don't know. Google for pointers and you should find something suitable.
 
You're close, but this is probably more correct

void updateCorrectTotal (char position, int points, int &totForward, int &totBack )
{
if ( position == 'F' )
totForward += points;
else if ( position == 'B' )
totBack += points;
}

You need the &'s on the parameters to pass by reference , your arguments were in the wrong order and checking for the 'B' is just a good idea.
 
You're close, but this is probably more correct

void updateCorrectTotal (char position, int points, int &totForward, int &totBack )

Ummm...


void updateCorrectTotal (char position, int points, int *totForward, int *totBack )


you'd call that as

int tFwd, tBack;

updateCorrectTotal ( 2, 3, &tFwd, &tBack);
 
by wrong order, you mean char position, int points MUST be first? If so can you briefly explain why.

Im pretty much new to the void statement... only started on it today so im still a bit confused with "calling", "actual parameters", "formal parameters", "passing" etc etc. Books explains a bit but not when it comes to more complex scenarios.

Also any idea if there is a website with visual vids on it? Visual is always easier to understand.

But other than that, works like a charm now so thanks boet. Much appreciated.

Now for the last subquestion. Need to put in a for loop, but thats p!ss easy
 
Final programme works 100%... thanks again everyone for your help. Might be back tho cos i got one question left :p which has a very scary title "Decide for yourself"
 
lol
acidrain -

drop me a pm with your email -
we can exchange answers -

No 6 was a real pain.
 
I think my problem is, when the deadline gets closer and closer... i just completely lose my mind
 
You're close, but this is probably more correct

void updateCorrectTotal (char position, int points, int &totForward, int &totBack )
{
if ( position == 'F' )
totForward += points;
else if ( position == 'B' )
totBack += points;
}

You need the &'s on the parameters to pass by reference , your arguments were in the wrong order and checking for the 'B' is just a good idea.
Ummm...


void updateCorrectTotal (char position, int points, int *totForward, int *totBack )


you'd call that as

int tFwd, tBack;

updateCorrectTotal ( 2, 3, &tFwd, &tBack);
Nope, yours is incorrect, and Deenem's is 100% correct. The spec called for a void return type function with 2 value parameters [one char and the other int] and an additional 2 by-reference int parameters, however in yours you are passing pointers to int's using the address of operator on the 2 int parameters at the end when invoking the function, that is C style coding, C++ allows for by-reference parameters and is safer than using pointers which could be NULL or uninitialsed.
There's another cinfusing part... the "&" and the "*". Study guide explains only &.
In C++, & has more than one purpose, when declaring a function prototype, and applying & to either a return type or to one or more parameters, the return type and/or parameters are said to be passed by reference, some examples:
Code:
//both ui and uc are by-reference
void fncByRefParams(unsigned int& ui, unsigned char& uc);

//the return type is passed by reference - be careful with these to not return something that only has scope within the function's definition, i.e. it goes out of scope and is invalidated when the function returns
unsigned int& fncByRefRetVal();
 
Last edited:
Well its finally been submitted. A whole 47min before due date. Thanks guys and especially thank you to BT6LW for taking some time on msn to guide me... but as mentioned, you public appreciation will be viewed in OT :D

Nice to have a fellow MyBB going to the same uni and doing the same degree ( i think same )
 
* = pointer to
& = address of / pass-by-reference if used in prototype

I hope you understand what you did and didn't just copy/paste! I suggest you start with your assignments a few days before the due date.

PS: I am registered for the same subject and I live almost just around the corner from you. Been programming for a few years now and already know most of this stuff...
 
Top
Sign up to the MyBroadband newsletter
X