-3

I keep getting the error message that "'rate' is uninitialized in this function".

Can anyone off the bat see why? I've looked through my code and I'm passing it correctly on my other functions, and the error stems from this function. Any ideas?

double compute_rate(int userAge_array[], char sportType_array[], int index)
{
  double rate;
  if (sportType_array[index] == 'f') {
    if (userAge_array[index] < 25) {
      rate = 68.95;
    }
    else if (userAge_array[index] > 25) {
      rate = 55.95;
    }
  }
  if (sportType_array[index] == 'g') {
    if (userAge_array[index] < 25) {
      rate = 73.95;
    }
    else if (userAge_array[index] > 25) {
      rate = 65.95;
    }
  }
  if (sportType_array[index] == 'h') {
    if (userAge_array[index] < 25) {
      rate = 99.95;
    }
    else if (userAge_array[index] > 25) {
      rate = 92.95;
    }
  }

  return rate;
}
gf807
  • 81
  • 1
  • 2
  • 7

3 Answers3

4

You are returning rate at the end of the function but it may never initialized because all assignments are inside ifs statements which may not be processed at all.

Solution:

assign it at first using some default value that you will accept in case no one of the ifs works:

double rate=0.0;
Humam Helfawi
  • 19,566
  • 15
  • 85
  • 160
  • so your solution worked, but when I try to pass rate to another variable in my next function, I recieve a similair error. "variable 'charge' set but not used" you can see the 2nd function here. I should also mention I tried applying your first solution to this variable with no success. [link](https://gist.github.com/anonymous/ed79f637d0daa0b35a295ddd6136bfdf) – gf807 Apr 26 '17 at 22:27
  • Maybe you should do something with charge in that code. I mean you compute it but then don't do anything with the computed value. – drescherjm Apr 26 '17 at 22:35
  • Didn't realize I made a cout statement for "rate" rather than "charge" in that function. Thank you, code works now. – gf807 Apr 26 '17 at 22:38
1

If sportType_array[index] is not 'f', 'g', or 'h', none of the if blocks will execute. You should change these to if/else if and then add a final else clause when nothing matches.

But more likely, the problem is that userAge_array[index] == 25. You set rate when it's less than 25 or greater than 25, but never set rate when it's exactly equal to 25. Try using else instead of else if, so you cover all cases.

double compute_rate(int userAge_array[], char sportType_array[], int index)
{
  double rate;
  if (sportType_array[index] == 'f') {
    if (userAge_array[index] < 25) {
      rate = 68.95;
    }
    else {
      rate = 55.95;
    }
  }
  else if (sportType_array[index] == 'g') {
    if (userAge_array[index] < 25) {
      rate = 73.95;
    }
    else {
      rate = 65.95;
    }
  }
  else if (sportType_array[index] == 'h') {
    if (userAge_array[index] < 25) {
      rate = 99.95;
    }
    else {
      rate = 92.95;
    }
  }
  else {
      rate = 0.0;
  }

  return rate;
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

If sportType_array[index] is not 'f', 'g', or 'h', the function drops straight through to the return, where it returns rate, which is not initialized.

What value would you like to return if the values in those arrays are incorrect? It would be best to set rate to that, even if it's not supposed to happen.

jdunlop
  • 181
  • 1
  • 9