cout issues

when i run the program, the user is prompted by my cin.. but no matter what they put in, nothing comes out. help!



#include "GoingBroke.h"
#include <math.h>
#include <stdio.h>
#include <cstdlib>
#include <iostream.h>

using namespace std;

int main () {

int a, b, c, x, y, z, n, g, q, u;

cout << "How many coins does each individual player start with?" << endl;
cin >> n;

q=0;

for (g=0; g<100000; g++) {
a==n; b==n; c==n;

q++;

return q;

while (a!=0 && b!=0 && c!=0) {
rand()%2==x; rand()%2==y; rand()%2==x;

q++;

if (x+y+z!=0 && x+y+z!=3) {
if (x==y) {
c+2; a--; b--;
}
else if (x==z) {
b+2; a--; c--;
}
else if (y==z) {
a+2; b--; c--;
}
}
return q;
}
}
cout<< q/100000 << endl;
}
what is this program supposed to do?
its complicated... it has to do with coin flips. 3people flip a coin, person whose coin doesnt match gets one from each of the other 2 people. first one to zero loses, game resets, and it is played a total of 100,000 times. trying to count the average number of turns per game.
what does this mean?

a==n; b==n; c==n

and you are returning something right after the for loop starts, so the rest of the function won't execute

1
2
3
4
5
6
for (g=0; g<100000; g++) {
a==n; b==n; c==n;

q++;

return q;
Last edited on
changed that, thanks


for (g=0; g<100000; g++) {
a=n; b=n; c=n;

q++;

still not getting an output though
you have another return statement as well. Try putting the return at the end of the function.
put return 0 at end of funtion, between cout and final }

still no cout.
did you remove the second return q statement?

I compiled it and ran it.

I commented out a==n; b==n; c==n; because I didn't understand what was meant by it. You are comparing them, but not in an if statement? Did you overload the '==' in your class?

and also, I changed
rand()%2==x; rand()%2==y; rand()%2==x;
to
x=rand()%2; y=rand()%2;

I at least got a display when I did it. Not sure what it's supposed to be. I got 54.
Last edited on
#include <math.h>
#include <stdio.h>
#include <cstdlib>
#include <iostream>

using namespace std;

int main () {

int a, b, c, x, y, z, n, g, q, u;

cout << "How many coins does each individual player start with?" << endl;
cin >> n;

q=0;

for (g=0; g<100000; g++) {
a=n; b=n; c=n;

q++;

while (a!=0 && b!=0 && c!=0) {
x=rand()%2; y=rand()%2; z=rand()%2;

q++;

if (x+y+z!=0 && x+y+z!=3) {
if (x==y) {
c+2; a--; b--;
}
else if (x==z) {
b+2; a--; c--;
}
else if (y==z) {
a+2; b--; c--;
}
}
return q;
}
}
cout<< q/100000 << endl;
return 0;
}

why am i not getting cout.
you still haven't removed the return q statement.

1
2
3
4
5
6
7
8
9
10
else if (y==z) {
a+2; b--; c--;
}
}
return q;
}
}
cout<< q/100000 << endl;
return 0;
}
Thank you so much!!!
Did it work right?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <math.h>
#include <stdio.h>
#include <cstdlib>
#include <iostream>

using namespace std;

int main () {

int a, b, c, x, y, z, n, g, q, u;

cout << "How many coins does each individual player start with? ";
cin >> n;

q=0;
for (g=0; g<100000; g++) 
{
   a=n; b=n; c=n;
   q++;

   while (a!=0 && b!=0 && c!=0) 
   {
      x=rand()%2; y=rand()%2; z=rand()%2;

      q++;

      if (x+y+z!=0 && x+y+z!=3) 
      {
         if (x==y) 
         {
            c+2; a--; b--;
         }
         else if (x==z) {
            b+2; a--; c--;
         }
         else if (y==z) {
            a+2; b--; c--;
         }
      }
   }
}
cout<< q/100000 << endl;
return 0;
}


I compiled it with all warnings displayed and got the following messages:

test.cpp: In function `int main()':
test.cpp:31: warning: statement has no effect
test.cpp:34: warning: statement has no effect
test.cpp:37: warning: statement has no effect
test.cpp:10: warning: unused variable 'u'


Check these.
My file was named test.
Last edited on
mine works fine except the answers are slightly off of the ones my teacher says i should have, any ideas?

#include <math.h>
#include <stdio.h>
#include <cstdlib>
#include <iostream>

using namespace std;

int main () {

float a, b, c, x, y, z, n, g, q, u;

cout << "How many coins does each individual player start with?" << endl;
cin >> n;
cout << "How many games will be played?" << endl;
cin >> u;

q=0;

for (g=0; g<u; g++) { //sets number of games to whatever user entered;
a=n; b=n; c=n; //all players, a,b,c, begin with number of coins
//previously entered
q++;

while (a!=0 && b!=0 && c!=0) { //while all users have coins
x=rand()%2; y=rand()%2; z=rand()%2; //assign each person heads(0) or
//tails(1) randomly
q++;

if (x+y+z!=0 && x+y+z!=3) { //if coins are not all 0 or all 1:
if (x==y) { //if players a and b have same result, they
c+2; a--; b--; //award player c their coins from that turn
}
else if (x==z) { //if players a and c have same result, they
b+2; a--; c--; //award player b their coins from that turn
}
else if (y==z) { //if players b and c have the same result, they
a+2; b--; c--; //award player a their coins from that turn
}
}
}
}
cout<< q/u << endl; //tells user the total number of turns, q, from all cumulative games
return 0; //divided by the total number of games, providing the average number
} //of turns per game
Look at the warnings in @TheJJJunk's e-mail. The statements without effect are c+2;, b+2;, and a+2;. You are performing an arithmetic operation and then ignoring the results.

You should be using the += operator instead. That will store the result back into the variable in question.

Your bug will cause shorter execution runs because the coins are being discarded rather than being passed from player to player.
Topic archived. No new replies allowed.