• Advertisement
Sign in to follow this  

coin flip problem

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

If you intended to correct an error in the post then please contact us.

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)
            {
                cout << "Heads";
            }
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.

 

reply's would be gratefully welcomed.

Share this post


Link to post
Share on other sites
Advertisement

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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)
{
   cout << "Heads";
}

Share this post


Link to post
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)
            {
                cout << "Heads";
            }
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 this post


Link to post
Share on other sites

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

Share this post


Link to post
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 this post


Link to post
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.

 

Quite helpful.

 

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 this post


Link to post
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

Share this post


Link to post
Share on other sites

 

 

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
You would wrap the whole thing in parenthesis. I.e.:
if (!(choice == 1 || choice == 2))

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

 

still gives me the infinite loop on an invalid input

Share this post


Link to post
Share on other sites

 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.

Share this post


Link to post
Share on other sites

If you want to get a little adventurous, try running your debugger.  I assume you have one available.  Step your code and see the values it takes at each step.

Share this post


Link to post
Share on other sites

Could you post the current state of your code?

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

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))
 {
        cin >> choice;
        cout << "please enter 1 or 2";
 }
 if (choice == 1)
    {
            srand ( time( NULL ) );
            randRange( 1 , 10 );
            if (randRange( 1,10) >= 5)
            {
                cout << "Heads";
            }
else
            {
            cout << "Tails";
            }
    }
else if (choice == 2)
            {
            cout << "Boooo";
            return 0;
            }

}

Share this post


Link to post
Share on other sites

Bugs:

- If people dont type in the right number you print out what to do only after they should have typed it in again, would be better the other way around.

- You uselessly call your randRange function twice without using the result of the first call.

 

Possible improvements:

- You can move the choice variable into main(), it doesnt need to be a global variable.

- Please try to consistently indent everything, as this makes reading the code easier. For example the "else" should be on same level as the "if" and all statements inside a block on same indendation level.

- If you want to modify it later with a loop to throw more than one coin its safer to put the srand call at the beginning of main so you dont accidently call it more than once. Calling it more than once would make the quality of random numbers worse.

Edited by wintertime

Share this post


Link to post
Share on other sites
As SiCrane and Washu pointed out, you need to check if 'cin' failed.
 
When you do:
int myInt = 0;
std::cin >> myInt;
std::cin wants to give 'myInt' a number, because ints can only hold numbers. But if the user types in something that isn't a number, like a letter or a period, std::cin needs some way to inform your code that something went wrong. To do this, std::cin has several 'flags' that are set on it: whether it failed, whether it ran out of input, or whether something else went wrong. [documentation]
 
Instead of checking each flag, to make sure nothing went wrong, std::cin, for convenience, also has a customized special use of the '!' operator. If (!cin) is true, that means something went wrong.
 
This code works:
#include <ctime>
#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

//Don't declare variables outside of functions (these are called 'globals'), it's a bad practice
//that while making things easier earlier on in programming, actually causes problems with more
//advanced programming.
//int choice;

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

int main ()
{
    //srand() only needs to be called once for your entire program, so it's best to just
    //do so near the program start.
    srand ( time( NULL ) );
    
    cout << "flip a coin?\n1: Yes\n2: No\n";
    
    //Declare variables as close as possible to when you first use them, and also initialize 
    //them to a good default value - this makes it easier to detect bugs when they occur.
    int choice = 0; 
    cin >> choice;
    
    while (!(choice == 1 || choice == 2) )
    {
        //Incase of problems getting input, we check for if something went wrong with 'cin'.
        //'cin' has a special use of the '!' operator built in, that when used on 'cin',
        //lets you know if something went wrong.
        if(!cin)
        {
            std::cout << "Invalid input was given - it wasn't a number." << std::endl;
            
            //Since 'cin' was marked with a flag that said it got invalid input,
            //we need to clear that flag to let 'cin' know we saw and responded to the problem.
            cin.clear();
        }
        
        cin >> choice;
        cout << "please enter 1 or 2";
    }

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

[Edit:] The OP's code indentation issues come from mixing spaces and tabs, and then the tabs in his IDE being a different number of spaces from the tabs in GameDev.net's code box. Happens to me alot too! I wish IDEs automaticly converted spaces to tabs when you copied text (I think my IDE only does that on file save) - but that'd probably cause other problems. =) Edited by Servant of the Lord

Share this post


Link to post
Share on other sites

I was going to say the same, cout should be before the cin, it looks like the program is in an infinite loop but it isn't.

 

Also, some suggestions about the rest of the code:

  • You should change the ">= 5" for either a ">= 6" or "a > 5". With ">= 5" you're taking 1, 2, 3 or 4 for "tails" and 5, 6, 7, 8, 9 and 10 for "Heads".
  • Also, you can change it for something like "if (randRange(0,1) == 0)", it looks much clearer that there's a 50-50 chance for each value.
  • If you checked that after the "while" the variable "choice" can be only a 1 or a 2, you can write if (choice == 1) { ... flip the coin ... } else { cout << "booo" }, you don't have to check again, if it's not a 1 then it can only be a 2. If you need to check the same variable for different values it's better to use a switch statement instead.
  • The line "return 0;" shouldn't be there, it's only being executed if you typed "2". It should be only at the end of the main function if you'll always return a 0.
Edited by DiegoSLTS

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement