-7

Possible Duplicate:
C++ Random number homework question

Here's the code:

float getTwoRandomNumbers (int Min, int Max, int & number1, int & number2);

void main()
{
    cout << "The two random numbers are " << getTwoRandomNumbers << endl;
}

float getTwoRandomNumbers (int Min, int Max, int & number1, int & number2)
{
   int loopNumber, number;

   for (loopNumber = 0; loopNumber <= 200; loopNumber++)
   { 
       number = rand(); 

       if (loopNumber < 100 && number >= Min && number <= Max)
       {
          number1 = number;
          cout << number1 << endl;
       }

       if (loopNumber > 100 && number >= Min && number <= Max)
       {
           number2 = number;
           cout << number2 << endl;
       }
       return getTwoRandomNumbers;
   }
}

And the error:

.cpp (36)  :  error C2440 : 'return'  : cannot convert from 'float (__cdecl  *)(int,int,int, &,int&)' to 'float'
      There is no context in which this conversion is possible.
Community
  • 1
  • 1
Probs
  • 23
  • 5
  • 10
    Seriously, get a C++ book and read that. A function is called with `YourFunc(args_here)`, simply `YourFunc` creates a function pointer. – Xeo May 10 '11 at 01:32
  • And that's ignoring the bad return as well. – Brian Roach May 10 '11 at 01:34
  • The reason for your error is that you are trying to return getTwoRandomNumbers, which is a reference to the function with the same name, this cannot be used as a return value for a function which returns a float. – Pirooz May 10 '11 at 01:35
  • Even if you fixed the parentheses, this becomes an endless loop - what is the return value (float) used for in getTwoRandomNumbers, and why is it called again at the end of the method? – Joe May 10 '11 at 01:35
  • 4
    Wasn't this thread just closed? http://stackoverflow.com/questions/5943831/c-random-number-homework-question – Gio Borje May 10 '11 at 01:36
  • what i am trying to do is return two random numbers that never displays the other EVER. – Probs May 10 '11 at 01:36
  • every time it does compile (i.e. if i were to say return number2 it will display 00401040) not two separate #s. and @Gio Borje- yes it was but i forgot to input the code in the question so people got the inference that i was asking them to do it for me. – Probs May 10 '11 at 01:40
  • Here's another hint: "return getTwoRandomNumbers" - what exactly this is returning. getTwoRandomNUmbers is the name of a function, not a variable. – cost May 10 '11 at 01:44
  • 1
    Looks like a VB programmer who doesn't know C++. Returning the name of the function is a dead giveaway. – Paul Z May 10 '11 at 01:45
  • @Paul Z Really, you can do that in VB? Seems like a confusing implementation (never used VB myself) – cost May 10 '11 at 01:46
  • @cost It's a traditional VBism that the name of a function is a "variable" inside the function. You return a value by arranging to leave the function after assigning the right value to the name of the function. Clearly this didn't work for the student in C++, but he found this "magic" line that gives the same effect, if you declare a variable with the name of the function at the top of the function (because the variable declaration hides the recursive declaration in some, overly-forgiving, compilers). – Paul Z May 10 '11 at 01:58

2 Answers2

3

Another homework question? Seems to be the season for them. Here's a point in the right direction: getTwoRandomNumbers takes some parameters. Look at the function declaration, it's asking for something. Other than that, listen to the first comment on your main question. I highly suggest doing some basic reading on C++.

Update: People seem to not like this question, though it isn't such a bad one. Your first issue to address is that you're returning the name of the function, not the variable you're storing the random number in. I can't laugh at this mistake, I've made similar ones myself (and still do on occasion).

Once you've done that, your getTwoRandomNumbers function has some values in needs passed to it to function. You have it spelled out in your function declaration there. Reading a few chapters in an into c++ book would do you a lot more overall good then asking people here for help on what you're doing wrong. I get the impression you don't have a full understanding of the concepts behind c++.

Update 2: Annnnnnd one last thing. Always remember, when you want to use a variable, you have to declare it first. All of them, not just most of them.

cost
  • 4,420
  • 8
  • 48
  • 80
  • 1
    On your update: We don't like it because it's the same one for the 4th time now, with our general hint being "get a good C++ book" every time. All answers are very likely for naught if the basic understanding is missing. – Xeo May 10 '11 at 02:00
  • 1
    @Xeo Looking at his posting history, it looks like he's trying to last minute do all of his programming assignments. Especially since he seems to have posted this same question 3 times without making ANY changes. I wonder when it's due – cost May 10 '11 at 02:07
  • To me it looks like he's trying to do that whole learning-how-to-program thing at the end of the semester, after not paying attention or showing up. – Brian Roach May 10 '11 at 02:26
0

In one sentence, you are not returning a value of the type float (which you declared), you are returning a value that is a function.

Mark Lalor
  • 7,820
  • 18
  • 67
  • 106
  • 1
    That's what I thought at first, but look through the rest of his code... not passing any parameters, using undeclared ints, returning a function. It's a mess. – cost May 10 '11 at 01:53