I'm making a simple coin flip game and i'm not sure how to get it to print out a result if that makes sense. basically I'm not sure what to make the while condition to see if the answer is true or false. here is what i have so far. any help would be appreciated
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
srand(static_cast<unsigned int>(time(0)));
char answer;
int bank = 10;
int guess;
int h = 0;
int t = 1;
cout << "Welcome to the coin flip game. It cost a dollar to play." << endl;
cout << "If you guess correctly you will will $2.00" << endl;
cout << "Do you want to play <y/n>" << endl;
cin >> answer;
int flip = rand() % 3;
guess = flip;
while (toupper(answer) == 'Y')
{
cout << "Your bank is $" << bank << endl;
cout << "Enter heads or tails <h/t>" << endl;
cin >> guess;
while (guess == h)
{
bank++;
cout << "Winner, the flip came up " << flip << endl;
cout << "Would you like to play again <y/n>?" << endl;
cin >> answer;
}
}
return 0;
}
its suppose to look like this
and these are the guidelines
Here goes
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
srand(static_cast<unsigned int>(time(0)));
char answer;
int bank=10;
char guess;
char result;
cout << "Welcome to the coin flip game. It cost a dollar to play." << endl;
cout << "If you guess correctly you will will $2.00" << endl;
cout << "Do you want to play <y/n>" << endl;
cin >> answer;
while (toupper(answer) == 'Y')
{
bank--;
cout << "Your bank is $" << bank << endl;
cout << "Enter heads or tails (h/t)" << endl;
cin >> guess;
if(rand()%2==1)
result='t';
else
result='h';
if(guess==result)
{
bank+=2;
cout << "Winner, the flip came up ";
}
else
cout << "Sorry, you lose. The coin flip came up ";
if(result=='t')
cout << "Tails" << endl;
else
cout << "Heads" << endl;
cout << "Would you like to play again (y/n)?" << endl;
cin >> answer;
}
cout << "Thanks for playing, your bank is $" << bank << endl;
return 0;
}
Good luck!
Since you put C++ as languange, I would strongly recommend you to review modern (C++11) random engines.
Here is a good sample
Related
The program does not ignore the string question and interferes with the while statement. When I answer the first question I cannot answer the question "Would you like to ask another question? Answer Y/N:" because the program ends. I already put cin.ignore so I don't know why it is not ignoring the answer to the first question
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;
int rollDie();
int main()
{
string question;
char repeat;
do
{
cout << "Hello, I am the magic 8-ball! Ask me a question and I will give you an answer" << endl;
cout << "Your question: ";
cin >> question;
cin.ignore();
cout << endl << endl;
int roll = rollDie() % 5 + 1;
if (roll == 5)
cout << "Answer: It is certain " << endl;
if (roll == 4)
cout << "Answer: Reply hazy, Try Again " << endl;
if (roll == 3)
cout << "Answer: Don't count on it " << endl;
if (roll == 2)
cout << "Answer: Signs point to yes " << endl;
if (roll == 1)
cout << "Answer: My sources say no " << endl;
cout << "Would you like to ask another question? Answer Y/N: ";
cin >> repeat;
} while (repeat == 'Y' || repeat == 'y');
cout << endl << endl;
system("pause");
return 0;
}
int rollDie()
{
srand(time(NULL));
return rand();
}
Don't use cin >> question. This will only read to the next whitespace. If you type beyond that, other cin statments will read them.
Instead, use std::getline():
std::getline(std::cin, question);
I'm having a problem with the random generated number result not matching with the user input and it only outputs the first statements instead of falling to else if the user guessed wrong.
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <string>
using namespace std;
int main()
{
int bank = 10;
int heads = 0;
int tails = 1;
char response;
string headsTails;
int coinToss = rand() % 2;
srand(static_cast<int>(time(0)));
cout << "Welcome to the coin flip game. It cost a dollar to play. " << endl;
cout << "If you guess correctly you will win $2.00 dollars " << endl;
cout << "Do you want to play? (Y/N) " << endl;
cin >> response;
while (toupper(response) == 'Y')
{
cout << "Your bank is $" << bank << " dollars." << endl;
cout << "Enter head or tails (H/T)" << endl;
cin >> response;
coinToss = rand() % 2;
headsTails = coinToss ? "Heads" : "Tails";
if (coinToss == heads || coinToss == tails)
{
cout << "Winner! The coin flip came up " << headsTails << endl;
cout << "Would you like to play again? (Y/N) " << endl;
cin >> response;
bank += 2;
}
else
{
cout << "Sorry, you lose. The coin flip came up " << headsTails <<
endl;
cout << "Would you like to play again? (Y/N) " << endl;
cin >> response;
bank -= 1;
}
}
cout << "Thanks for playing! Your bank is " << bank << endl;
cout << "Please come again! " << endl;
return 0;
}
if (coinToss == heads || coinToss == tails)
That condition is wrong, it will always evaluate to true since you set coinToss to 0 or 1 yourself.
You have to use the response variable you ask of the user, something like:
int guess = response == 'T' ? 1 : 0; // convert the user input to the same values used throughout the program.
if(guess == coinToss)
//won..
else
//lost..
I need help with my code I will put it all in if anyone can clean it up so it looks nice but I will then highlight what I need help with.
#include <Windows.h>
#include <stdlib.h>
#include "stdafx.h"
#include <string>
#include <iostream>
#include <ctime>
using namespace std;
int main() {
string ready;
system("#echo off");
system("cls");
cout << "ready to play? (y/n to play.): " << endl;
cin >> ready;
if (ready != "y")
do {
cout << "O.K. Goodbye!" << endl;
return 0;
} while (ready == "y");
cout << "OK!" << endl;
system("pause");
system("cls");
int number = 0;
int min = 1;
int max = 125;
int userinput;
srand(time(0));
number = rand() % (max - min + 1) + min;
int guesses_left = 10;
cout << "please try to guess a number between 1 and 125, you currently have: " << guesses_left << " guesses left" << endl;
cin >> userinput;
if (userinput > number){
cout << "Sorry your guess is too high, please guess again.";
cout << endl;
cout << "you now have: " << guesses_left << " guesses left. Please choose again.";
}
else if (userinput < number){
cout << "Soory, your guess is too low, please guess again." << endl;
cout << "You now have: " << guesses_left << " guesses left. Please choose again.";
}
else if (userinput == number){
cout << "congrats you won :)... Here have a prize as you won with: " << guesses_left << " guesses left." << endl << "no but seriously WELL DONE!!! :D";
system("cd C:\Program Files\Internet Explorer");
system("iexplore https://media.property118.com/wp-content/uploads/2013/12/Best-Property-Forum.jpg");
return 0;
}
return 0; }
The bit I am struggling with is this
cout << "please try to guess a number between 1 and 125, you currently have: " << guesses_left << " guesses left" << endl;
cin >> userinput;
if (userinput > number){
cout << "Sorry your guess is too high, please guess again.";
cout << endl;
cout << "you now have: " << guesses_left << " guesses left. Please choose again.";
}
else if (userinput < number){
cout << "Soory, your guess is too low, please guess again." << endl;
cout << "You now have: " << guesses_left << " guesses left. Please choose again.";
}
else if (userinput == number){
cout << "congrats you won :)... Here have a prize as you won with: " << guesses_left << " guesses left." << endl << "no but seriously WELL DONE!!! :D";
system("cd C:\Program Files\Internet Explorer");
system("iexplore https://media.property118.com/wp-content/uploads/2013/12/Best-Property-Forum.jpg");
return 0;
}
I am trying to loop this segment of code until I am bothered to add in you losing lives in the game. So if I get guess higher it should say it is higher and then loop back to give me another go. I have only started learning C++ yesterday and this is all mostly my code (some of it is adapted from other peoples posts like the srand and number = rand thing are other peoples)
I changed your "y/n" while loop to this :
cout << "ready to play? (y/n to play.): " << endl;
cin >> ready;
if (ready != "y")
{
cout << "O.K. Goodbye!" << endl;
return 0;
}
else
cout << "OK!" << endl;
I am also giving you a while version if you want to force the user to press either 'y' or 'n' to go further :
cout << "ready to play? (y/n to play.): " << endl;
cin >> ready;
while ( ready != 'y' && ready != 'n')
{
cout << "Only answer with 'y' or 'n' "<< endl;
cin >> ready;
}
and the game loop where user guesses to this :
int guesses_left = 10;
cout << "please try to guess a number between 1 and 125, you currently have: " << guesses_left << " guesses left" << endl;
while (guesses_left != 0)
{
cin >> userinput;
if (userinput > number){
cout << "Sorry your guess is too high, please guess again.";
cout << endl;
--guesses_left;
cout << "you now have: " << guesses_left << " guesses left. Please choose again.";
}
else if (userinput < number){
cout << "Soory, your guess is too low, please guess again." << endl;
--guesses_left;
cout << "You now have: " << guesses_left << " guesses left. Please choose again.";
}
else if (userinput == number){
cout << "congrats you won :)... Here have a prize as you won with: " << guesses_left << " guesses left." << endl << "no but seriously WELL DONE!!! :D";
system("cd C:\Program Files\Internet Explorer");
system("iexplore https://media.property118.com/wp-content/uploads/2013/12/Best-Property-Forum.jpg");
return 0;
}
}
cout << "You have used up all your guesses." << endl;
return 0;
So I'm trying to create a feature that will let me quit my game once you've completed it, or you have the option to play again. The replay option works, but whenever I try the quit part, it just goes back to the start of the game.
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
using namespace std;
int main() {
SetConsoleTitle("Guess the number! | v1.0");
int replay;
replay = 1;
int g, n;
play:
g = NULL;
srand(time(NULL));
n = rand() % 20 + 1;
system("cls");
cout << "Number guessing game" << endl;
cout << "********************" << endl;
cout << endl;
cout << "The random number has been generated. It is between 1 and 20" << endl;
system("pause");
while (g != n) {
system("cls");
cout << "Number Guessing Game" << endl;
cout << "********************" << endl;
cout << endl;
cout << "Type a guess between 1 and 20 then press ENTER" << endl;
cout << "Your guess: ";
cin >> g;
}
if (g = 1113) {
goto debugskip;
}
debugskip:
system("cls");
cout << "You have found the number!" << endl;
cout << "The number was " << n << endl;
cout << "Would you like to play again? 1 for yes, 2 for no.";
cin >> replay;
if (replay = 1) {
goto play;
}
else if (replay = 2) {
exit(1);
}
return 0;
}
You are using assignment operator instead of equals operator in your ifs.
Change
if (replay = 1) {
for
if (replay == 1) {
And do the same in the other places with the same problem.
I am struggling to get an input validation working for my game.
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
int iGumballs;
int iUserguess;
int iGuesses = 0;
while (true)
{
system("CLS");
cin.clear();
iGuesses = 0;
srand(static_cast<unsigned int>(time(0)));
iGumballs = rand()%1000+1;
cout << "How many gumballs are in the bumball jar? You guess! 1-1000" << endl;
do
{
cout << "Enter your guess: ";
cin >> iUserguess;
if(iUserguess > iGumballs)
{
cout << "Too high!" << endl << endl;
}
if(iUserguess < iGumballs)
{
cout << "Too low!" << endl << endl;
}
iGuesses ++;
}
while(iUserguess > iGumballs || iUserguess < iGumballs);
cout << "You guessed the right amout of gumballs" << endl << endl;
cout << "You took " << iGuesses << " guesses" << endl << endl;
system ("pause");
}
return 0;
}
I basically want the program to display
Your Guess: Sorry, incorrect input - try again
When the user enters a number less then 1, and higher then 1000, as well as some sort of validation which makes sure a number is entered instead of a letter or symbol. I tried cin.fail() but I couldn't get it quite to work.
Thanks,
John
You will need some test to see if is a number or not, try this:
#include <iostream>
#include <string>
#include <ctime>
#include <boost/lexical_cast.hpp> //dependency that can be optional
using namespace std;
bool is_number(const std::string& s)
{
std::string::const_iterator it = s.begin();
while (it != s.end() && std::isdigit(*it)) ++it;
return !s.empty() && it == s.end();
}
int main()
{
int iGumballs;
std::string iUserguessStr;
int iUserguess;
int iGuesses = 0;
while (true)
{
system("CLS");
cin.clear();
iGuesses = 0;
srand(static_cast<unsigned int>(time(0)));
iGumballs = rand()%1000+1;
cout << "How many gumballs are in the bumball jar? You guess! 1-1000" << endl;
do
{
cout << "Enter your guess: ";
cin >> iUserguessStr;
if(is_number(iUserguessStr))
iUserguess = boost::lexical_cast<int>(iUserguessStr); //you can make your own or maybe use lexical cast to transform a string into integer
else
continue; //put some fancy message here warning the user
if(iUserguess > iGumballs)
{
cout << "Too high!" << endl << endl;
}
if(iUserguess < iGumballs)
{
cout << "Too low!" << endl << endl;
}
iGuesses ++;
}
while(iUserguess > iGumballs || iUserguess < iGumballs);
cout << "You guessed the right amout of gumballs" << endl << endl;
cout << "You took " << iGuesses << " guesses" << endl << endl;
system ("pause");
}
return 0;
}
My answer is based on a related problem: How to determine if a string is a number with C++?
Yo can use if(cin) to check the state of the input stream and since operator>> will return the input stream that was passed to it you can use if(cin>>iUserguess)
If cin is in a failed state -maybe because the user entered a non number- the expression if(cin>>iUserguess) will evaluate to false.
If the user enters a non number you will need to call cin.clear() to clear the stream state and cin.ignore() to discard the input, before trying to read a number again.
so using your example, it could be changed to this:
#include <iostream>
#include <ctime>
#include <limits>
using namespace std;
int main()
{
int iGumballs;
int iUserguess;
int iGuesses = 0;
while (true)
{
system("CLS");
iGuesses = 0;
srand(static_cast<unsigned int>(time(0)));
iGumballs = rand()%1000+1;
cout << "How many gumballs are in the bumball jar? You guess! 1-1000" << endl;
do
{
cout << "Enter your guess: ";
if(cin >> iUserguess)
{
iGuesses ++;
if(iUserguess > iGumballs)
{
cout << "Too high!" << endl << endl;
continue;
}
if(iUserguess < iGumballs)
{
cout << "Too low!" << endl << endl;
continue;
}
}
else
{
cout<<"incorrect input - try again\n\n";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
continue;
}
}
while(iUserguess > iGumballs || iUserguess < iGumballs);
cout << "You guessed the right amout of gumballs" << endl << endl;
cout << "You took " << iGuesses << " guesses" << endl << endl;
system ("pause");
}
return 0;
}
To validate characters you could use the try-catch structure.
-First read in a string and try Typecasting it and handle the errors with try-catch.
-Then use conditions to make sure the input is in range.
-If the input isn't valid, you can write an error message to display.