Hi There,

Hope you can help with this. I have created a void function which has to add the totals and return the values to the main section for display. For some reason it does not return the totals. I tried everything I know. I also put displays in the function to see if it gets the parameters. It does but does not bring back the value. Please help.

#include <iostream>
#include <string>
using namespace std;

void inputAndValidate(char Position, int Points)
{
 while((Position == 'B') || (Position == 'F'))
   {
    cout << "Pleas e enter the position: ";
    cin >> Position;
    cout << "Please enter the Points: ";
    cin >> Points;
   }
}

void updateCorrectTotal(char posi, int poin, int tFoward, int tBack)
{
 if (posi == 'F')
    tFoward += poin;
 else    
    tBack += poin;
 }  

//
int main()
{
 char position;
 int points, totForward, totBack;

 totForward = 0;
 totBack = 0;
 points = 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;

 return 0;

}

Recommended Answers

All 5 Replies

Void function does not return the value

Correct, that's what a void function does.

If you want a function to return a value, you shouldn't make it void.
example:

int timestwo(int in)
{
    return in*2;
}

int main ()
{
    int orig = 4;
    int k = timestwo(orig);
    cout << orig << " times two is : " <<  k ;
    cin.get();
    return 0;
}

next time you post code, please use [code=cpp] code here [/code] tags instead of quote-tags

Hi niek_e,

I have change the function to this but still I can't get the value.

int updateCorrectTotal(char posi, int poin, int tFoward, int tBack)
{
 if (posi == 'F')
    return tFoward += poin; 
 else {
    return tBack += poin;
}

You have to make it equal to something such as : int numberyouwant = updateCorrectTotal(position, points, totForward, totBack); That should help.

Cameron

Hi niek_e,

I have change the function to this but still I can't get the value.

int updateCorrectTotal(char posi, int poin, int tFoward, int tBack)
{
 if (posi == 'F')
    return tFoward += poin; 
 else {
    return tBack += poin;
}

It seems like you might intend to modify tForward and tBack in that function. In that case you have to pass those arguments by reference instead of by value, which is now happening.
To pass by reference declare the argument with a reference type, see below

int updateCorrectTotal(char posi, int poin, int [B]&[/B] tFoward, int [B]&[/B] tBack)
{
 if (posi == 'F')
    // the variable that tFoward references, will be incremented by poin
    return tFoward += poin; 
 else {
    // the variable that tBack references, will be incremented by poin
    return tBack += poin;
}

Thanks Guyz,

I used mitrmkar's suggestion, it worked also with a void function.

Thanks to you all.

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.