cs161AWinter2024/flipgame.cpp

47 lines
895 B
C++

#include <iostream>
#include <stdlib.h>
using namespace std;
// change this to add a variable that counts
// how many times you were right
int main(){
string input;
int guess = -1;
int wins = 0;
int games = 0;
srand(time(0));
int coin = -1;
//flip the coin
coin = rand() % 2;
cout << "Guess heads or tails: ";
cin >> input;
while(input != "quit"){
if(input == "heads"){
guess = 0;
games = games + 1;
}
else if(input == "tails"){
guess = 1;
games = games + 1;
}
else {
guess = -1;
}
if(guess == coin){
cout << "You were right" << endl;
wins = wins + 1;
}
else if(guess != -1){
cout << "Nope" << endl;
}
coin = rand() % 2;
cout << "Guess heads or tails: ";
cin >> input;
}
cout << "You were right: " << 100*(float)wins/games << "% of the time" << endl;
return 0;
}