• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Mr Stx

coin flip problem

30 posts in this topic

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.

1

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.

2

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";
}
2

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 :)?

1

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
1

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)
1

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

0

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.

1

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.

0

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.

0

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.

0

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))
0

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
0

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

0

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.

1

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.

0

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;
            }

}

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
1

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
1

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
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0