Coffeehouse Thread

8 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

C++ Bool Help?

Back to Forum: Coffeehouse
  • User profile image
    Game​Designer557

    I'm working on a coin flipping program that uses boolians to determine what message to display to the user, but I've run into a little problem...  It only displays that the coin flips heads.  Here is the Source code.

     

    #include <iostream>
    #include <ctime>
    #include <string>
    
    using namespace std;
    
    void main ()
    {
        srand (time(NULL));
        
        int HowManyFlips = 0;
        int TotalHeads = 0;
        int TotalTails = 0;
        bool CoinResult;
        string UserChoice;
    
        //Find out how many times to flip the coin
        cout <<"How many times do you want to flip the coin?"<< endl;
        cin >> HowManyFlips;
        cout << endl;
    
        //Flip the coin as many times as the user suggests
        while (HowManyFlips != 0)
        {
    
            //Find the Coin side (I'm thinking this is where the problem is but I can't figure out why)
            CoinResult = rand()% 2;
            
    
            //Display Result of coin to user and add to total heads/tails
            if (CoinResult = true)
            {
                cout <<"The coin landed on Heads"<< endl;
                TotalHeads++;
            }
            else
            {
                cout <<"The coin landed on Tails"<< endl;
                TotalTails++;
            }
    
            //reduce Flips so that the process isn't infinite
            HowManyFlips--;
        }
        cout << endl;
    
        //display how many heads/tails were flipped
        cout <<"There are "<<TotalHeads<<" Total Heads"<< endl;
        cout <<"There are "<<TotalTails<<" Total Tails"<< endl;
        cout << endl;
        
    
        //find out if user want's to flip again
        cout <<"Do you want to flip again?(y/n): ";
        cin >> UserChoice;
    
        if (UserChoice == "y")
        {
            cout << endl;
            main();
        }
        else
        {
            return;
        }
    }

     

    If I could get some help please I would be grateful.

  • User profile image
    Blue Ink

    if (CoinResult = true)

    is not the same as

    if (CoinResult == true)

    Classic pitfall; some compilers warn you about this kind of things.

    Two ways to avoid this altogether:

    1) Reverse all your comparisons, so that the constant appears first. For instance,

    if (true = CoinResult)

    would produce a compile time error.

    2) At least for Boolean expressions, use implied conversion to bool:

    if (CoinResult)

     

    HTH

  • User profile image
    Charles

    Homework assignment, eh? Wink

    As Blue ink said, when testing for equality, you use the == operator. = is an assignment operator.

    if a == b means if the value of a is equal to the value of b.

    a = b means assign the value of a to b.

    Now, after doing a = b,  a == b will always return true.

    When you get to overloading operators in your course, you can of course change the behavior either of these operators.

    C

  • User profile image
    evildictait​or

    For a variety of reasons, you should get into the practice of writing

    if(isHeads) { } rather than if(isHeads == true) {}.

    It'll save you much pain and many keystrokes in future, particularly if you ever venture out of using the C++ bool keyword, and start using things like the windows BOOL or BOOLEAN types, for which if(isHeads == TRUE) can lead to all sorts of subtle code errors.

  • User profile image
    Minh

    This must be GCC since VS definitely will warn me about this... I think it might even throw an error

  • User profile image
    Game​Designer557

    @Charles: It's kind of a Homework assignment.  I actually learned basic C++ a year ago but I didn't retain too much so I'm reviewing through c++ For Dummies (Easiest way to get the best results I believe Tongue Out)  Thank you for the tip.  I didn't even notice that I put = instead of ==.

  • User profile image
    Game​Designer557

    @Blue Ink:  Thanks for the catch.  I was looking over the code wondering what was wrong but I didn't even see the = instead of ==.  I also didn't think to make if(CoinResult == true) into just if(CoinResult).  Thanks for the help!

  • User profile image
    evildictait​or

    @Minh - it's a warning not an error (it's C4706) because it's valid C, it's just usually wrong.

    If you put the following in one of your base headers, it'll auto detect these errors and prevent the build until you fix it:

    #pragma warning(  error  : 4706 )// assignment within conditional expression

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.