0

I am new to programming. Here is part of my assignment, which requires using pass-by-reference. After I compile it and type in the values for win, draw and loss respectively,it returns me nothing. I don't know whether it is due to the problem in calling the function or the floating point.

void Cfunction(int win, int draw, int loss, float& point)

{
point = win * 2.5f + draw * 1 + loss * 0;
}

int main(void)
    {
        int win, draw, loss;
        float point;
        cout << "Please input the game record in the following order: win draw loss " << endl;
        cin >> win >> draw >> loss;

        Cfunction(win, draw, loss, point);
        cout << "The total score for the team is " << point << endl;

    }
userA ng
  • 53
  • 2
  • 3
    Please attempt some debugging – David Heffernan Sep 28 '15 at 07:14
  • 4
    You aren't using doubles anywhere. Have you tried debugging the code? What are the input values? BTW, a *function* that doesn't return a value isn't a good idea. – Panagiotis Kanavos Sep 28 '15 at 07:14
  • 3
    Your code works for me. What numbers are you inputting? – SingerOfTheFall Sep 28 '15 at 07:15
  • 3
    @PanagiotisKanavos: I assume "assignment that requires pass by reference" is a requirement on the function written, in other words, teacher doing "do it this way because I say so". – Mats Petersson Sep 28 '15 at 07:16
  • 2
    "it returns me nothing" - what does it mean ? – borisbn Sep 28 '15 at 07:17
  • 1
    You should also check for successful input, a la `if (cin >> win >> draw >> loss) { Cfunction... / cout... } else cerr << "invalid input - next time, enter three numbers\n";`. – Tony Delroy Sep 28 '15 at 07:20
  • never use `cin >>` - it only ends in confusion. You'd be better off asking for the 3 values separately, reading a whole line from cin with getline, and then getting the (single) number from the string. If you can't do that, then at least print out the values you get so you can check they are what you think they are.. – Tom Tanner Sep 28 '15 at 07:25
  • This code works as expected - http://ideone.com/sXyc6G – borisbn Sep 28 '15 at 08:28

2 Answers2

1

Look good to me.

You could verify that your cin >> ... has finished by adding a cout << "calculatin total score...." << std::endl;.

(Note: std::cin >> wins has the wins variable passed by reference, too :))

Indeed, as @David Hefferman suggested, learn to use the debugger. Will save you a huge amount of time in the (very near) future.

xtofl
  • 40,723
  • 12
  • 105
  • 192
0

Looks fine to me too. You do know that you have to add the numbers one by one on their own lines, e.g. 5 , 3 , 4 ?

rockworm
  • 11
  • 2