The code is:
#include <iostream>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
using namespace std;
int createCard() {
if (rand() % 67 < 10)
return 11;
else
return rand() % 10 + 1;
}
bool is_number(const std::string& s)
{
string::const_iterator it = s.begin();
while (it != s.end() && std::isdigit(*it)) ++it;
return !s.empty() && it == s.end();
}
int ask4num(){
string askq = "0";
int reti;
cout << "How many cards would you like to find today? ";
getline (std::cin, askq);
if(!is_number(askq)){
cout << "Pick a number between 1 and 10. ";
return ask4num();
}
reti = stoi(askq);
if(reti > 10 || reti < 1){
cout << "Pick and number between 1 and 10. ";
return ask4num();
}
return reti;
}
int main() {
srand(time(NULL));
// game stats
int playerWinning = true;
int playerSkill = 9;
int playerScore = 0;
string playerName = "";
int cardCount = 0;
int cardsCollected = 0;
// title
cout << "Welcome to Baseball Card Collector." << endl << "Press [ENTER] to Start and Continue.";
cin.get();
// player name
cout << "Please enter your name: ";
cin >> playerName;
// ask
cardCount = ask4num();
if (cardCount == 0){
cout << "You searched for zero cards. You didn't even go outside. ";
}
else{
cout << "You're in luck, " << playerName << ", you found exactly " << cardCount << " cards!" << endl;
}
// main game loop
while (playerWinning && cardsCollected < cardCount) {
// create a random card
int cardRarity = createCard();
// card player and check card
if (cardRarity > 10) {
cout << endl << "You found a Mickey Mantle card worth 500 bucks!" << endl;
playerScore = playerScore + 500;
}
else {
cout << endl << "You found a card! " << cardsCollected + 1 << endl;
}
cout << "Checking Authenticity..." << endl;
sleep(2);
// fake card
if (playerSkill < cardRarity) {
playerWinning = false;
cout << "You found a fake card!." << endl;
}
// real card
else {
if (playerSkill - cardRarity > 7) {
cout << "You found a Michael Jordan card worth 25 bucks!" << endl;
playerScore = playerScore + 25;
}
else if (playerSkill - cardRarity > 5) {
cout << "You found a Mike Trout card worth 40 bucks!" << endl;
playerScore = playerScore + 40;
}
else if (playerSkill - cardRarity > 0) {
cout << "You found a Ken Griffey card worth 20 bucks!" << endl;
playerScore = playerScore + 20;
}
else {
cout << "You found another card, but it was too destroyed to make out the player." << endl;
}
cardsCollected++;
}
cout << endl;
sleep(1);
}
//end game
if (cardsCollected != cardCount){
// lost
cout << "You sold a fake card, and now the buyer is after you. Too bad!" << endl;
}
cout << "Cards Collected: " << cardsCollected << endl;
cout << "Final payout: " << playerScore << endl << endl;
}
it is a game i created using a format of someone else's zombie game. i am a beginner to c++ and wanted to try to make my own thing but still have it framed right.
i started working on it and had my dad test it out for me doing stupid user things to make sure im prepared for every type of answer submitted.
string askq = "0";
int reti;
cout << "How many cards would you like to find today? ";
getline (std::cin, askq);
if(!is_number(askq)){
cout << "Pick a number between 1 and 10. ";
return ask4num();
}
this function^^^
is called here
// ask
cardCount = ask4num();
but whenever i run the code, it repeats itself and says
How many cards would you like to find today? Pick a number between 1 and 10. How many cards would you like to find today?
it is only supposed to repeat the "how many cards" part and say the "pick a number" part if you type in a response that isn't a digit.
for some reason it is saying the "pick a number" part and and repeating the question before there is even a response. it is suppose to wait for the cin, but instead repeats anyways.
any idea why this is so?
i apologize for my sloppy organization, this is the first thing ive made