# coin toss program?

• 09-16-2003
girliegti
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.
• 09-16-2003
jlou
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.
• 09-16-2003
DougDbug
Right on, jlou !
Quote:

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.

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.
• 09-16-2003
DougDbug
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!
• 09-17-2003
...
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; }```