• Create Account

## coin flip problem

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

30 replies to this topic

### #1Mr Stx  Members

116
Like
1Likes
Like

Posted 11 September 2013 - 09:24 AM

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.

### #2CaptainKraft  Members

266
Like
3Likes
Like

Posted 11 September 2013 - 09:31 AM

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.

### #3Cosmic314  Members

1902
Like
2Likes
Like

Posted 11 September 2013 - 09:45 AM

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.

### #4Glass_Knife  Moderators

8612
Like
3Likes
Like

Posted 11 September 2013 - 09:46 AM

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 }

I think, therefore I am. I think? - "George Carlin"
My Website: Indie Game Programming

My Book: http://amzn.com/1305076532

### #5Matias Goldberg  Members

9075
Like
2Likes
Like

Posted 11 September 2013 - 09:48 AM

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)
{
}

### #6Mr Stx  Members

116
Like
1Likes
Like

Posted 11 September 2013 - 11:43 AM

#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 ?

### #7Washu  Senior Moderators

7714
Like
1Likes
Like

Posted 11 September 2013 - 11:54 AM

As for the invalid input, check if the input stream is still valid. If its not you have a few choices, one of which would be to simply flush the stream.

Read up on std::basic_ios::clear, std::basic_ios::fail, and std::basic_istream::ignore.

Additionally, choice should not be a global.

Edited by Washu, 11 September 2013 - 11:58 AM.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.
ScapeCode - Blog | SlimDX

### #8ApochPiQ  Moderators

21413
Like
3Likes
Like

Posted 11 September 2013 - 12:02 PM

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.
Wielder of the Sacred Wands

### #9Cosmic314  Members

1902
Like
1Likes
Like

Posted 11 September 2013 - 12:18 PM

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)


### #10Mr Stx  Members

116
Like
0Likes
Like

Posted 11 September 2013 - 12:46 PM

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

### #11SiCrane  Moderators

11529
Like
2Likes
Like

Posted 11 September 2013 - 12:51 PM

Not either 1 or 2 would be !(choice == 1 || choice ==2).

### #12IndyOfComo  Members

835
Like
1Likes
Like

Posted 11 September 2013 - 12:56 PM

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?

Also, Mr Stx, I would highly suggest tackling one error at a time and not adding code when you know you have an error. This practice should make it easier to fix things as you find them. Above, you have changed the main line to loop, but you also still have the problem of treating randRange() as a variable. Now, this error has nothing to do with your infinite loop--the replies you've gotten on that should be helpful--but that just happens to be the case in this situation. If you add too much all at one time, it can become much more difficult (as a beginner) to figure out where the problem is really coming from. So fix the randRange() thing, then straighten out your boolean algebra(woot!), and then move on.

While I was typing this I see you have another post. I put in a link above, I don't know how helpful it will be.

Here is my technical background info.

### #13IndyOfComo  Members

835
Like
0Likes
Like

Posted 11 September 2013 - 01:01 PM

Not either 1 or 2 would be !(choice == 1 || choice ==2).

Take a closer look at how you wrote your code, and answer each question for yourself in each situation.

while ( choice != 1 || choice != 2)

input     test 1   --   test 2   --   summary

1           F               T              (F || T) = T

2           T               F              (T || F) = T

3           T               T              (T || T) = T

Step through SiCrane's code and see what you get in each of these cases.

Here is my technical background info.

### #14Mr Stx  Members

116
Like
0Likes
Like

Posted 11 September 2013 - 01:08 PM

Thanks guys ill spend a few hours looking into the information you have kindly supplied me and if I'm still stuck ill post again.

### #15Mr Stx  Members

116
Like
0Likes
Like

Posted 11 September 2013 - 02:50 PM

ok I have done a good bit of research and have the information provided in this thread great starting points, I have come across a hurdle regarding the "!(choice == 1 || choice ==2)" statement, I understand the concept of it but the compiler doesn't allow me to enter this as the "!" isn't in brackets.

### #16SiCrane  Moderators

11529
Like
0Likes
Like

Posted 11 September 2013 - 02:55 PM

You would wrap the whole thing in parenthesis. I.e.:
if (!(choice == 1 || choice == 2))


5832
Like
0Likes
Like

Posted 11 September 2013 - 02:56 PM

if(!(choice == 1 || choice ==2))

should work.

Are you doing

if !(choice == 1 || choice ==2)

that won't work, "if" needs brackets straight away.

Edited by Paradigm Shifter, 11 September 2013 - 02:57 PM.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

### #18Mr Stx  Members

116
Like
0Likes
Like

Posted 11 September 2013 - 04:15 PM

while (!( choice == 1 || choice == 2))

still gives me the infinite loop on an invalid input

### #19Servant of the Lord  Members

33567
Like
0Likes
Like

Posted 11 September 2013 - 04:38 PM

Could you post the current state of your code?

It's perfectly fine to abbreviate my username to 'Servant' or 'SotL' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames -

### #20SiCrane  Moderators

11529
Like
1Likes
Like

Posted 11 September 2013 - 04:39 PM

while (!( choice == 1 || choice == 2))

still gives me the infinite loop on an invalid input

You mean on things that aren't integers? That's when you go back up to Washu's post and read about fail() and clear(). Short version: if a stream contains something that it wasn't expecting, like a letter when you're trying to extract an int, the stream sets the fail bit and causes all subsequent reads to fail until you handle the error. Personally, rather than read integers directly from the stream, I prefer to read lines into a string with std::getline() and dump those strings into a stringstream and extract from that.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.