开发者

C++ Programming Error [duplicate]

开发者 https://www.devze.com 2023-03-05 04:08 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: C++ Random number homework question
This question already has answers here: Closed 11 years ago.

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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消