Name: Anonymous 2006-06-28 19:11
What is wrong with this code? I think it has to do with the char variable but it just messes up whenever it askes for a y or n input.
/
#include <stdafx.h>
#include <iostream>
#include <iomanip>
#include <cstdlib> //for rand() function
#include <ctime> // for srand() function
using namespace std;
int main()
{
int frequencyHeads = 0;
int frequencyTails = 0;
int loopCount = 0; //count the repetition of coin toss
int coinToss; // 1 for head, 2 for tail
char yesNo;
srand( time( NULL )); //seed value for the random number generator
//format floating-point numbers
cout << fixed << showpoint << setprecision(2);
cout << "Do you want to continue the game? "
<< " (Y for yes and N for no.)\n";
cin >> yesNo;
//Table heading
cout << "\n\nFrequency of heads "
<< "Frequency of tails "
<< "Ratio of heads / tails\n";
cout << "------------------ ------------------ "
<< "----------------------\n";
//Add your code here whatever you see fits.
while(yesNo == 'y') {
cout << "\n\n" << frequencyHeads
<< " " << frequencyTails
<< " " << coinToss;
while( loopCount < 100)//do not modify this loop body
{
//generate a random number, 1 or 2
coinToss = rand() % 2 + 1;
if( coinToss == 1 )
frequencyHeads++;
if( coinToss == 2 )
frequencyTails++;
loopCount++;
}//No modification-Restriction ends here.
//Add your code again from this point on
//to complete the program
cout << "Do you want to continue the game? "
<< " (Y for yes and N for no.)\n";
cin >> yesNo;
}
cout << "GameOver: Thank you!";
cout << endl << endl;
return 0;
}
/
#include <stdafx.h>
#include <iostream>
#include <iomanip>
#include <cstdlib> //for rand() function
#include <ctime> // for srand() function
using namespace std;
int main()
{
int frequencyHeads = 0;
int frequencyTails = 0;
int loopCount = 0; //count the repetition of coin toss
int coinToss; // 1 for head, 2 for tail
char yesNo;
srand( time( NULL )); //seed value for the random number generator
//format floating-point numbers
cout << fixed << showpoint << setprecision(2);
cout << "Do you want to continue the game? "
<< " (Y for yes and N for no.)\n";
cin >> yesNo;
//Table heading
cout << "\n\nFrequency of heads "
<< "Frequency of tails "
<< "Ratio of heads / tails\n";
cout << "------------------ ------------------ "
<< "----------------------\n";
//Add your code here whatever you see fits.
while(yesNo == 'y') {
cout << "\n\n" << frequencyHeads
<< " " << frequencyTails
<< " " << coinToss;
while( loopCount < 100)//do not modify this loop body
{
//generate a random number, 1 or 2
coinToss = rand() % 2 + 1;
if( coinToss == 1 )
frequencyHeads++;
if( coinToss == 2 )
frequencyTails++;
loopCount++;
}//No modification-Restriction ends here.
//Add your code again from this point on
//to complete the program
cout << "Do you want to continue the game? "
<< " (Y for yes and N for no.)\n";
cin >> yesNo;
}
cout << "GameOver: Thank you!";
cout << endl << endl;
return 0;
}