Good luck with your adventures in C++ ...
[php]// haids or tails (Dev C++)
#include
#include // for setw(), not used here
#include
#include
using namespace std; // for std::cin, std::cout etc.
int main()
{
int rn;
srand(time(0));
for (int counter = 0; counter <= 10; counter++)
{
rn = 1 + rand() % 2;
if (rn == 1)
cout << "Heads" << endl;
else
cout << "Tails" << endl;
}
cin.get(); // wait
return 0;
}
[/php]
vegaseat
DaniWeb's Hypocrite
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
is they a way to display the total number of heads and tails, from what i can gather it should be something like this:
int heads;
int tails
{
if (toss == 1)
cout <<"heads "<< endl;
heads = heads + 1
else
cout << "tails" <
Ja, that would work, but you have to initialize your heads and tails counters to zero. Not every compiler will do that for you! I know it's early, but you need to {} your if else statements!
vegaseat
DaniWeb's Hypocrite
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
If you want to keep track of your total heads and tails the code should look similar to this:
[php]// haids or tails (Dev C++)
#include
#include
#include
using namespace std; // for std::cin, std::cout etc.
int main()
{
int rn, heads = 0, tails = 0;
srand(time(0));
for (int counter = 0; counter <= 10; counter++)
{
rn = 1 + rand() % 2;
if (rn == 1)
{
cout << "Heads" << endl;
heads++;
}
else
{
cout << "Tails" << endl;
tails++;
}
}
cout << "there are a total of " << heads << " heads";
cout << " and " << tails << " tails" << endl;
cin.get(); // wait
return 0;
}
[/php]
vegaseat
DaniWeb's Hypocrite
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417