coin toss program?

This is a discussion on coin toss program? within the C++ Programming forums, part of the General Programming Boards category; here is the problem: each of three people tosses a coin. if all three tosses are heads or all three ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    7

    coin toss program?

    here is the problem:
    each of three people tosses a coin. if all three tosses are heads or all three tosses are tails, the game is a draw. if two of the tosses are one type and the third one is different, the third one loses. write a program to score this game. 0 represents a head and 1 represents a tail.
    here is what i have thus far:

    Code:
    # include <iostream>
    using namespace std;
    
    int main (void)
    int first, second, third;
    cout << "input the first player's toss:  ";
    cin >> first;
    cout << "input the second player's toss:  ";
    cin >> second;
    cout << "input the third player's toss:  ";
    cin >> third;
    
    if (first=1)
    {first=second && third;}
    cout << "the game is a draw." << endl;
    
    if (first=0)
    {first=second && third;}
    cout << "the game is a draw." << endl;
    
    if (first=1)
    {second=0;
    third=0;}
    cout << "the loser is player 1." << endl;
    __________________________________
    I know that this is not finished, however I'm not sure what is the easiest way to use "if" statements in this algorithm. any help would be great appreciated, thanks.

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    A couple of quick hints:

    1. Put curly braces ('{', '}') around your main function like this:
    Code:
    int main()
    {
    ... all your code for main() ...
    }
    2. To compare two integers in an if statement, use '==', not '=', like this:
    Code:
    if (first == second)
    ...
    3. You can't use && like you do in English. For example, in English you might say, "If first equals 0 and first equals second and third ...". In C++, you must use, "If first is equal to 0 and first is equal to second and first is equal to third ..." like this:
    Code:
    if (first == 0 && first == second && first == third)
    ...
    Hope that helps get you started. Another word of advice - your code doesn't completely answer the question yet, which you probably already knew, but I would recommend getting a small portion to compile and run at a time. So don't code any more until you get this running.

  3. #3
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Thumbs up Right on, jlou !

    but I would recommend getting a small portion to compile and run at a time. So don't code any more until you get this running.
    This is excellent advice!

    Beginners should start with an empty (or almost empty) main function, test-compiling, and test-running each time a few lines of code are added. This makes it much easier to track-down errors.

    One or two mistakes can generate many (even hundreds) of compiler error messages. Compilers can get confused, 'cause they don't know what you're trying to do. Linker errors can be even more tricky to track-down, because linker error messages don't reference line numbers.

    You have to write enough code each time, so that your program compiles... your brackets have to match... functions that have non-void return types have to return something... you can't call a function that you haven't written yet, etc. It takes some practice to learn how to "develop" your code in a sequence that is testable as you "expand" it. But, once you get the "hang" of developing your programs this way, programming will be much more fun!

    It is also helpful to add "extra" (temporary) cout statements, so you can see what your code is doing before it is finished.

    This might seem silly... like running your spelling checker every time you've written a couple of words, but ALL programmers test-compile and test-run as their code is developed. As you gain experience, you will be able to write more lines of code before testing. Sometimes a programmer will write a temporary driver function just to test-out another function that will be part of the final program.

  4. #4
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Wink I looked into my crystal ball...

    A couple of assignments you are likely to see in the future (or on a test) are:
    1- Make the program "flip the coin" for you. For this you'll need the rand() (random) function.

    2- Put the program in a loop that asks "Do you want to play again"? If you haven't studied loops yet, there are 3 types of loops in C++, for-loops, while-loops, and do-while-loops.

    More of my standard comments for beginners:
    The if statement (Decision making and "branching") that you have already learned, and loops (doing things over-and-over), are the foundations that make all programming worthwhile. There's plenty more to learn, but these are the most important concepts!

  5. #5
    ...
    ... is offline
    Registered User
    Join Date
    Feb 2002
    Posts
    465
    another quick comment, in your if statements, you seem to be putting one check in the parenthesis, and a seperate check in the body of the if (the curly braces).

    for an if statement, you do all initial checks in the parenthesis. in the curly braces you put the code that you want executed if those checks pass.

    as an example, here is your first check rewritten correctly:

    Code:
    if ( first == 1 && first == second && first == third )
    {
         cout << "the game is a draw." << endl;
    }
    I came up with a cool phrase to put down here, but i forgot it...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 01:27 PM
  2. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  3. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 11:58 PM
  4. Replies: 15
    Last Post: 05-13-2006, 10:28 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21