Public Group

coin flip problem

This topic is 2133 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hi all i'm new, I started learning a week or so ago and have joined to the forum so I can get some help and eventually contribute to the community. I am reading a number of books and online guides and have come across a problem I cannot solve when creating a simple coin flip randomizer.

#include <ctime>
#include <cstdlib>
#include <iostream>

using namespace std;

int choice;

int randRange (int low, int high)
{
return rand() % ( high - low ) + low;
}

int main ()
{

cout << "flip a coin? \n\n1: Yes\n\n2: No\n";
cin >> choice;
if (choice == 1)
{

srand ( time( NULL ) );
randRange( 1 , 10 );
if (randRange >= 5)
{
}
else
{
cout << "Tails";
}
}
else if (choice == 2)
{
cout << "Boooo";
return 0;
}

}



I am getting a error on the if (randRange >= 5) line, the compiler says that c++ forbids a comparison between pointer and integer, but as far as I am aware I am not using a pointer.

Share on other sites

My experience with C++ is pretty limited but I think I might be able to help here.

It looks like you are calling the randRange function before entering the if statement, and you are assuming that the result is stored in a variable that is called randRange. What you need to do is either store the return value of the function call into a variable, or just call the function within the if statement. I prefer the latter, and here's a small example:

if (randRange(1, 10) >= 5) {
// do stuff here
}


That should solve your current problem.

Share on other sites

You are using 'randRange' as if it were an 'int' variable.  However, 'randRange' is a function so in this context it will return a function pointer.  A function pointer is an address in memory where the code for a function resides.  Essentially your initial attempt is comparing code to a data type.  CaptainKraft's solution will fix that particular compiler issue because you are now comparing against the result of performing that function, which produces an int and comparing it to an int type.

Share on other sites

The randRange() is a function.  You have to assign the result of the function to a new variable, and try that.  (just using the function name the compiler thinks you're using a function pointer.  Don't worry about that.  Try something like this:


int randomNumber = randRange( 1 , 10 );
if (randomNumber >= 5) { ... more code here }

Share on other sites
Hi!

The problem is that "randRange" is the name of a function. So are you comparing a function against the number 5 (cows vs apples)

Like CaptainKraft said, changing it to
"if (randRange(1, 10) >= 5)" will execute the function randRange, and compare its return value with the number 5. Since you're comparing numbers with numbers, it will work.

Note this pattern is often considered hard to read, so it's clearer if you assign the return value to a temporary variable and then do the comparison:
int flipCoinResult = randRange( 1 , 10 );
if (flipCoinResult >= 5)
{
}

Share on other sites
#include <ctime>
#include <cstdlib>
#include <iostream>

using namespace std;

int choice;
int randRange (int low, int high)

{
return rand() % ( high - low ) + low;
}

int main ()
{

cout << "flip a coin? \n\n1: Yes\n\n2: No\n";
cin >> choice;

while ( choice != 1 || choice != 2)
{
cout << "please enter 1 or 2";
cin >> choice;
}
if (choice == 1)
{
srand ( time( NULL ) );
randRange( 1 , 10 );
if (randRange( 1,10) >= 5)
{
}
else
{
cout << "Tails";
}
}
else if (choice == 2)
{
cout << "Boooo";
return 0;
}

}



Thanks very much for the quick reply, a quick follow up question. I have been struggling with while loops for the last few days, the above script is my current attempt to loop the script if the user input is not one of the acceptable values, from what i have read it would seem that a do-while loop would apply but i should be able to do it with a while statement?

currently the program runs and gets stuck in a infinite loop if i enter an invalid input. i thought the prompt to enter an input would solve this, what am i doing wrong :)?

Share on other sites
Your loop continues as long as you don't enter 1 *or* you don't enter 2. To visualize that, here's a table:

1  |   2   | !=1  |  !=2  |  Loop?
-----------------------------------
Y  |   N   |   N  |   Y   |  Yes
N  |   Y   |   Y  |   N   |  Yes
N  |   N   |   Y  |   Y   |  Yes

So in other words, you will always loop forever, no matter what.

Share on other sites

Your loop continues as long as you don't enter 1 *or* you don't enter 2. To visualize that, here's a table:

1  |   2   | !=1  |  !=2  |  Loop?
-----------------------------------
Y  |   N   |   N  |   Y   |  Yes
N  |   Y   |   Y  |   N   |  Yes
N  |   N   |   Y  |   Y   |  Yes

So in other words, you will always loop forever, no matter what.

I find if you have trouble with testing the negative condition in your head, do this:

Determine which is easier to state in your mind.  In this case, "I want to exit the loop when choice is 1 or 2".  Then just negate this logic for the condition of staying in this loop:

"I want to stay in the loop when !(choice == 1 || choice == 2)".

You can also apply DeMorgan's rule to alter the check.  DeMorgan's rule is:

!(A && B) = !A || !B
!(A || B) = !A && !B


Thus the condition can also appear as:

!(choice == 1 || choice == 2) -> !(choice == 1) && !(choice == 2)


Share on other sites

I am having trouble comprehending this. Currently I think I am asking: When the user input is not either 1 or 2 then ask the question again and retrieve another response, if I am not asking this, how would it be asked?

thanks

• 11
• 9
• 17
• 13
• 11