How to get rid off the warning

When I compile my code, I receive this warning, "tp.cpp: In function ‘int single_roll_sim(int, double)’:
tp.cpp:103:1: warning: control reaches end of non-void function [-Wreturn-type]
}"


Can anyone help me to get rid off this warning.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
 
#include <iostream>   // Basic I/O => cin, cout, etc.
#include <cstdlib>    // Other helpful functions => rand(), RANDMAX
#include <ctime>      // for srand function

using namespace std;

// Prototype/declaration of a function that returns a
// uniform random number between [0,1]
double rand_uniform();

// Prototype/declaration of a function that will perform a single
// simulation of two rolls of paper and users
// Returns the number of squares left on the non-empty roll
int single_roll_sim(int N, double p);

int main(int argc, char *argv[])
{
  srand(time(0));                // different seeds 
  const int num_sims = 10;  // number of trials for our simulation

  int N;                // initial number of squares on each roll
  double Avg; 
  
  double p;             // probability of a big-chooser
  //double r;             // randomly generated number from 0 to 1

  int total_squares = 0;    // for each trial we'll keeping a running sum
                        // of the squares left on the non-empty roll

  // Do some error checking to make sure the user
  // entered the correct number of arguments to the program
  if(argc < 3){
    cout << "Usage: " << argv[0] << " N p" << endl;
    cout << "\tN = number of squares on each roll to start" << endl;
    cout << "\tp = probability of a big-chooser" << endl;
    return 1;
  }
  
  N = atoi(argv[1]);   // convert the argument to an integer
  p = atof(argv[2]);   // convert the argument to a floating point

     // Add your code here
  
     for ( int i = 0; i < num_sims; i++)      //Sums squares
     {
        
        total_squares += single_roll_sim(N, p);
        
     }

  cout << "Total squares = " << total_squares << endl;

  Avg = total_squares / num_sims;            //Average squares

  cout << "Int. # of Squares "<< N << ", Prob. of Big Chooser "<< p << ", Result "<< Avg << endl;

  return 0;
}

// return the number of squares on the non-empty roll
 int single_roll_sim(int N, double p)
 { 
  int rolls [2] = {N,N};
  int rem_1 = 0;
  int rem_2 = 0;
  double r;	

   while ( rolls[0] != 0 && rolls[1] != 0 ) //Repeat until Roll1 or Roll2 = 0
   { 
      r = rand_uniform();           //Generates random number from 0 to 1

          if ( r<= p)               //If random # less than p subtract 1 from the first element
          {                        
               rolls[0]=rolls[0]-1;
	  }
          else if (r > p)           //If random # more than p subtract 1 from the second element
          {
               rolls[1]=rolls[1]-1;
	  }
   }

	if (rolls[0] != 0)
	{
                rem_1 = rolls[0];
		return rem_1;            //Returns non zero value
	}
	else if (rolls[1] != 0)
	{
		rem_2 = rolls[1];        //Returns non zero value
		return rem_2;
	}
}

// returns a uniformly-distributed number in the range [0,1]
double rand_uniform()
{
  double r = rand() * 1.0/RAND_MAX; //Random number between 0 and 1
  return r;
}
closed account (D80DSL3A)
Clearly either line 86 or line 91 will be reached, due to the while condition, but the compiler doesn't realize that. Execution may reach line 92 as far as the compiler is concerned, in which case there is no return value.
Insert a return statement after line 92. Consider returning an illegal value (like -1) to indicate something went wrong. return -1;// should never actually execute
Last edited on
According the logic I think I see (I didn't run it), wouldn't an else statement be sufficient instead of the else if at line 88?

Edit: Oh wait, I see, if the input parameter N is less than 0, the while loop will be infinite.
Either way, I think having the last else if be just an else should still work.

You should probably return -1 at the beginning if N < 0.
Last edited on
closed account (D80DSL3A)
Ganado wrote:
...wouldn't an else statement be sufficient instead of the else if at line 88?

I agree. Likewise for line 77.
At a job long ago part of the coding standard was never to have more than one point of return from a function. I believe this is too restrictive, but I still encounter it every now and then when I'm free-lancing.

Another popular one is that every if-else block be terminated with a final else.

You will someetimes find places where these rules are enforced by the tools, and whether you agree with them or not you have to comply or you can't check-in your work, pass code reviews, etc.

I won't name names, but you know yourselves...

Anyhow, I got used to this kind of writing which solves both problems and your compiler warning:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
	int retVal = -1;
	...
	if (rolls[0] != 0)
	{
                retVal = rolls[0];
	}
	else if (rolls[1] != 0)
	{
		retVal = rolls[1];
	}
	else
	{
		retVal = -1;
		// handle unexpected result here
	}
	
	return retVal;
Topic archived. No new replies allowed.