Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and the Yahoo Answers website is now in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

?
Lv 4

How to create a function in C++ that generates a random number and tells the user if they have entered a number too high/too low?

Update:

I don't have enough characters to paste my code here, so I will try to get a screenshot uploaded somewhere for you to view it.

Update 2:

The following are links to screenshots of my code:

http://s14.postimg.org/wo0ru5yip/image.png

http://s14.postimg.org/jxwjh2qkh/image.png

Thanks in advance for your answers.

Update 3:

Oh also I understand that in the main function, I've written 'random(input, output)' - I just haven't updated it to correspond with the function definition above.

2 Answers

Relevance
  • ?
    Lv 5
    7 years ago

    First you only need one function parameter and a "void" function is better also you don't need loop.

    Now generate a random number and compare it to the parameter if the entered number is geater, print "The entered number is greater than the random number"

    Else if the random number greater than the number passed to the function then print "The entered number is less than the random number"

    Else print "they are equal".

  • ?
    Lv 7
    7 years ago

    void c_rand(int j){

    int r;

    r=rand();

    if(j> r )

    cout << "too big\n";

    else if (j<r)

    cout << "too small\n';

    else

    cout << "you got it\n";

    return;

    }

    int main(void){

    srand(time(0));

    c_rand(10);

    return 0;

    }

Still have questions? Get your answers by asking now.