问题
I am trying to count consecutive heads in a coin toss. Unfortunately, my counter for consecutive heads is not incrementing properly. Any ideas? Code and sample output below:
#include <iostream>
#include <string>
#include "random.h"
using namespace std;
string FlipCoin (string flip);
int main() {
string flip;
int consecutiveHeads = 0;
int totalFlips = 0;
while (consecutiveHeads<3) {
totalFlips++;
if (FlipCoin(flip) == "heads") {
consecutiveHeads++;
} else {
consecutiveHeads = 0;
}
cout <<totalFlips<<" "<< FlipCoin(flip) << " " << consecutiveHeads <<endl;
}
cout <<"It took "<< totalFlips <<" coin flips to get 3 consecutive heads."<< endl;
return 0;
}
string FlipCoin(string flip) {
if (randomChance(0.50)) {
return "heads";
} else {
return "tails";
}
}
Output:
1 heads 1
2 tails 0
3 tails 1
4 heads 2
5 heads 3
It took 5 coin flips to get 3 consecutive heads.
回答1:
Each call to FlipCoin(flip) generates a new random number. You call it twice, so it's generating two different random numbers. You should call FlipCoin(flip) once and store it in a variable.
...
string result = FlipCoin(flip);
if (result == "heads") {
consecutiveHeads++;
} else
consecutiveHeads = 0;
}
cout <<"It took "<< totalFlips <<" coin flips to get 3 consecutive heads."<< endl;
...
As somebody else mentioned, the flip variable in your main is uninitialized and unused. It's best to remove it. It was suggested that you pass flip as a reference in your FlipCoin function (using &). This most definitely has uses but it's not necessary. The simplest revision might be:
string FlipCoin() {
if (randomChance(0.50)) {
return "heads";
} else {
return "tails";
}
}
PS: If you remove the flip parameter from the function, you must also replace every occurence of FlipCoin(flip); with FlipCoin();
回答2:
The problem is you're calling FlipCoin() twice in each iteration: first to compare with "heads", and then again to display what happened. You need to call it once, and put it in a variable:
while (consecutiveHeads<3) {
totalFlips++;
string curFlip = FlipCoin(flip);
if (curFlip == "heads") {
consecutiveHeads++;
} else {
consecutiveHeads = 0;
}
cout <<totalFlips<<" "<< curFlip << " " << consecutiveHeads <<endl;
}
P.S. What is the argument to FlipCoin() for?
回答3:
Each time you call FlipCoin function you flip. Thus, you flip twice in each iteration of your while function. You may consider passing flip as reference to FlipCoin:
#include <iostream>
#include <string>
#include "random.h"
using namespace std;
void FlipCoin (string &flip);
int main() {
string flip;
int consecutiveHeads = 0;
int totalFlips = 0;
while (consecutiveHeads<3) {
totalFlips++;
FlipCoin(flip);
if (flip == "heads") {
consecutiveHeads++;
} else {
consecutiveHeads = 0;
}
cout <<totalFlips<<" "<< flip << " " << consecutiveHeads <<endl;
}
cout <<"It took "<< totalFlips <<" coin flips to get 3 consecutive heads."<< endl;
return 0;
}
void FlipCoin(string &flip) {
if (randomChance(0.50)) {
flip = "heads";
} else {
flip = "tails";
}
}
来源:https://stackoverflow.com/questions/18566294/c-coin-flip-program-error