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..
Related
Line 38 and 43
I don't see the purpose of the second parameter in the deposit() and withdrawal() functions inside the main but I need it there to run those functions. Is there a better way to do this? Any suggestion is appreciated.
int deposit(int cBalance, int dBalance)
{
cout << "How much would you like to deposit?" << endl;
cin >> dBalance;
cout << "Processing Deposit......." << endl;
cBalance += dBalance;
return cBalance;
}
int withdrawl(int cBalance, int wBalance)
{
cout << "How much would you like to withdraw?" << endl;
cin >> wBalance;
cout << "Processing Withdrawl......." << endl;
while (wBalance > cBalance)
{
cout << "NOT ENOUGH FUNDS. Try Again" << endl;
cout << "How much would you like to withdraw?" << endl;
cin >> wBalance;
}
cBalance -= wBalance;
return cBalance;
}
int main()
{
int depositAmount, withdrawAmount;
string moneyActivity, confirmation;
int currentBalance = 100;
confirmation = "YES";
while (confirmation == "YES" || confirmation == "yes")
{
cout << "Yourour current balance is: $" << currentBalance << endl;
cout << "Do you want to deposit or withdraw money? " << endl;
cin >> moneyActivity;
if (moneyActivity == "deposit")
{
currentBalance = deposit(currentBalance, depositAmount);/*depositAmount is not needed*/
cout << "Transaction Successful! Your Final Balance is: $ " << currentBalance << endl;
}
else if (moneyActivity == "withdraw")
{
currentBalance = withdrawl(currentBalance, withdrawAmount);/*withdrawAmount is not needed*/
cout << "Transaction Successful! Your Final Balance is: $ " << currentBalance << endl;
}
cout << "Would You Like To Make More Transactions? YES/NO?" << endl;
cin >> confirmation;
if (confirmation != "YES" && confirmation != "Yes" && confirmation != "yes" && confirmation != "NO" && confirmation != "No" && confirmation != "no")
{
cout << "INVALID RESPONSE. Try Again";
cin >> confirmation;
}
}
cout << "Thank you for using this service :) " << endl;
}
The 2nd parameters should not be parameters at all, they should be local variables inside of the functions.
Also, since you are making main() pass in the currentBalance and modify it upon return, it would be better to pass in the currentBalance by reference instead of by value.
Also, your main while loop could use some tweaking to reduce redundancy (and to fix a small logic hole that allows the user to enter "Yes" without actually continuing the loop).
Try something more like this:
#include <iostream>
#include <string>
#include <algorithm>
#include <limits>
#include <cctype>
using namespace std;
bool iequals(const string& a, const string& b)
{
return equal(a.begin(), a.end(),
b.begin(), b.end(),
[](unsigned char a, unsigned char b) {
return tolower(a) == tolower(b);
}
);
}
int inputNumber(const string &prompt)
{
int number;
cout << prompt << endl;
while (!(cin >> number))
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "BAD INPUT. Try Again" << endl;
cout << prompt << endl;
}
return number;
}
void deposit(int &cBalance)
{
int amount = inputNumber("How much would you like to deposit?");
cout << "Processing Deposit......." << endl;
while (amount <= 0)
{
cout << "NO FUNDS SELECTED. Try Again" << endl;
amount = inputNumber("How much would you like to deposit?");
}
cBalance += amount;
}
void withdrawl(int &cBalance)
{
int amount = inputNumber("How much would you like to withdraw?");
cout << "Processing Withdrawl......." << endl;
while ((amount <= 0) || (amount > cBalance))
{
if (amount <= 0)
cout << "NO FUNDS SELECTED. Try Again" << endl;
else
cout << "NOT ENOUGH FUNDS. Try Again" << endl;
amount = inputNumber("How much would you like to withdraw?");
}
cBalance -= amount;
}
int main()
{
int currentBalance = 100;
string input;
do
{
cout << "Your current balance is: $" << currentBalance << endl;
cout << "Do you want to deposit or withdraw money? " << endl;
cin >> input;
if (iequals(input, "deposit"))
{
deposit(currentBalance);
cout << "Transaction Successful! Your Final Balance is: $ " << currentBalance << endl;
}
else if (iequals(input, "withdraw"))
{
withdrawl(currentBalance);
cout << "Transaction Successful! Your Final Balance is: $ " << currentBalance << endl;
}
cout << "Would You Like To Make Another Transaction? YES/NO?" << endl;
cin >> input;
while (!(iequals(input, "YES") || iequals(input, "NO")))
{
cout << "INVALID RESPONSE. Try Again";
cout << "Would You Like To Make Another Transaction? YES/NO?" << endl;
cin >> input;
}
}
while (iequals(input, "YES"));
cout << "Thank you for using this service :) " << endl;
}
I am making a number guessing game and I do not know how to incorporate a certain number of guesses the users has to get the correct answer. I want the user to have only 3 guesses to guess the number but after 3 guesses, they lose if they do NOT get it correct. Here is my code below:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
srand ( time(NULL) );
cout << "Select a difficulty: 1) Easy, 2) Medium, 3) Hard " << endl;
int userlevel;
int userinput;
int randomNumber;
cin >> userlevel;
{
if (userlevel==1)
cout << "You chose Easy: 3 chances to guess correctly" << endl;
cout << "Pick a number between 1 and 10: " << endl;
cin >> userinput;
randomNumber = rand() % 10 + 1;
if (randomNumber==userinput)
cout << "You, guessed correctly! You win!" << endl;
else
cout << "I'm sorry, that is not correct. You lose." << endl;
}
{
if (userlevel==2)
cout << "You chose Medium: 4 chanaces to guess correctly" << endl;
cout << "Pick a number between 1 and 50: " << endl;
cin >> userinput;
randomNumber = rand() % 50 + 1;
if (randomNumber==userinput)
cout << "You, guessed correctly! You win!" << endl;
else
cout << "I'm sorry, that is not correct. You lose." << endl;
}
{
if (userlevel==3)
cout << "You chose Hard: 5 chances to guess correctly" << endl;
cout << "Pick a number between 1 and 100: " << endl;
cin >> userinput;
randomNumber = rand() % 100 + 1;
if (randomNumber==userinput)
cout << "You, guessed correctly! You win!" << endl;
else
cout << "I'm sorry, that is not correct. You lose." << endl;
}
return 0;
}
You should look into while-loops. It would be used like this:
int main() {
//...everything above this in yours is good
int Number_to_guess = (rand() % 10 + 1);
int NChances = userlevel + 2;
cout << "You have " << NChances << " chances to guess right.\n";
while (NChances != 0)
{
cout << "Guess: ";
cin >> userinput;
if (userinput == Number_to_Guess) {
cout << "You win! Congrats!\n";
break; // this will break out of the while-loop
}
NChances--; // this will count down the chances left
}
if (NChances == 0) {
cout << "Sorry, you lose. Try again next time!\n";
}
return 0;
}
The main think you're missing here is a loop around the guess limit. So after you figure out what level they are, you can say something like the following pseudocode:
While (counter <= 3)
*Your If Statements*
counter = counter +1
Make sure that in the if statement where they guessed the number right, you break them out of the loop early.
Finally, it might make more sense to guess a number before you enter the loop. So, something like they pick the difficulty, the random number is picked depending on what they say, and then the loop begins. The way it is now, a new random number will be created each time through the loop. I'm not sure if that's intended.
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
ok, I have been looking for days now but I cant find anything that will work.
I have a program and I want to make sure that the user enters a integer and not a double.
this program works fine but I need to validate the numOne and numTwo to make sure they are integers and not doubles, (5.5)
int main()
{ //This is where my variables are stored
int numOne, numTwo, answer, rightAnswer, ranNumOne, ranNumTwo;
//this will display to the user to enter a range of numbers to be used
cout << "Please enter a set of numbers to be the range for the problems." << endl;
cout << "Please enter the beginning number." << endl;
cin >> numOne;
cout << "please enter the ending number." << endl;
cin >> numTwo;
//this makes sure that the user entered a integer(if not the program will close)
if (!(cin >> numOne))
{
cout << "You did not enter a integer PLEASE RE-RUN THE PROGRAM AND TRY AGAIN!" << endl;
cin.clear();
cin.ignore(100, '\n');
exit(0);
}
cout << "please enter the ending number." << endl;
cin >> numTwo;
//this makes sure that the user entered a number(if not the program will close)
if (!(cin >> numTwo))
{
cout << "You did not enter a integer PLEASE RE-RUN THE PROGRAM AND TRY AGAIN!" << endl;
cin.clear();
cin.ignore(100, '\n');
exit(0);
}
//this is where the first number is generated
srand(time(0));
ranNumOne = rand() % (numOne - numTwo) + 1;
system("PAUSE");
//this is where the second number is generated
srand(time(0));
ranNumTwo = rand() % (numOne - numTwo) + 1;
//this is where the calculations are done
rightAnswer = ranNumOne + ranNumTwo;
//this displays the problem that was generated
cout << "What is: " << endl;
cout << setw(11) << ranNumOne << endl;
cout << setw(6) << "+" << setw(3) << ranNumTwo << endl;
cout << " -------\n";
cin >> answer;
//this checks to see if the answer is right or not and displays the result
if (answer == rightAnswer)
{
cout << "Your answer was correct! " << endl;
}
else
cout << "The correct answer is: " << rightAnswer << endl;
return 0;
}
Use std:n:ci.fail() to see if it failed.
int numOne;
cin >> numOne;
if(cin.fail())
cout << "Not a number...")
Maybe even a nice template function.
template<typename T>
T inline input(const std::string &errmsg = "") {
T var;
std::cin >> var;
while (std::cin.fail()) {
std::cin.clear();
std::cin.ignore(256, '\n');
std::cout << errmsg;
std::cin >> var;
}
return var;
}
Or not:
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <string>
#include <ctime>
#define DIFF(n1, n2) (n1 > n2 ? n1 - n2 : n2 - n1)
using namespace std;
int input(const string &firstmsg = "", const string &errmsg = "") {
int var;
std::cout << firstmsg;
std::cin >> var;
while (cin.fail()) {
cin.clear();
cin.ignore(256, '\n');
cout << errmsg;
cin >> var;
}
return var;
}
int main(){
//This is where my variables are stored
int numOne, numTwo, answer, rightAnswer, ranNumOne, ranNumTwo;
//this will display to the user to enter a range of numbers to be used
cout << "Please enter a set of numbers to be the range for the problems." << endl << endl;
numOne = input("Please enter the beginning number: ", "Invalid. Enter again: ");
//this asks the user for the second number
numTwo = input("Please enter the ending number: ", "Invalid. Enter again: ");
//this is where the first number is generated
srand(time(0));
ranNumOne = rand() % (DIFF(numOne, numTwo)) + 1; // ensures it will always be positive
system("PAUSE");
//this is where the second number is generated
srand(time(0));
ranNumTwo = rand() % (DIFF(numOne, numTwo)) + 1;
//this is where the calculations are done
rightAnswer = ranNumOne + ranNumTwo;
//this displays the problem that was generated
cout << "What is: " << endl;
cout << setw(11) << ranNumOne << endl;
cout << setw(6) << "+" << setw(3) << ranNumTwo << endl;
cout << " -------\n";
cin >> answer;
//this checks to see if the answer is right or not and displays the result
if (answer == rightAnswer){
cout << "Your answer was correct! " << endl;
}
else
cout << "The correct answer is: " << rightAnswer << endl;
return 0;
}
why not, get the number into a double and then see if that double is an int. ie
double d;
cin>>d;
if (ceil(d) != d)
cout >> " not an integer";
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.