0

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

user4581301
  • 33,082
  • 7
  • 33
  • 54
bleachedb
  • 15
  • 5
  • Unrelated: If you aren't already, take advantage of the debugging tool that came with your development tools. Being able to step through your code and see exactly what it does on a given line is invaluable. Any bad assumptions you made with be made painfully obvious when the debugger shows you something else happening. Sometimes seeing the real effect of your code gives you enough information to solve the problem outright, but when it doesn't it usually allows you to narrow down the problem, tune your experiments better, and make targeted websearches. – user4581301 Sep 15 '22 at 22:55
  • Side note: I see being included, but not . Sometimes you'll find includes but you can't count on this. For example, [here is the default compiler from Microsoft's Visual Studio tripping over the lack of ](https://godbolt.org/z/f6hE65oq3) (and a few other things because, as you can expect, unistd.h and POSIX/Unix support in general isn't high on the priority list of a compiler building programs for Windows). – user4581301 Sep 15 '22 at 23:04
  • And that brings up another note: Instead of the POSIX-specific `sleep` use [`std::this_thread::sleep_for`](https://en.cppreference.com/w/cpp/thread/sleep_for) from . It will work anywhere a modern C++ compiler is supported. If you can hew close to Standard C++ you'll have fewer problems moving your programs around. – user4581301 Sep 15 '22 at 23:04
  • And good on you for using `string`, by the way. A lot of new programmers shoot themselves in the foot trying to figure out more advanced stuff like `char` arrays and pointers in their first few programs and then head off to Java and Python because C++ seems stupid and primitive. Any language would seem stupid and primitive if you're going to program like people did in the 1970s. – user4581301 Sep 15 '22 at 23:07
  • @user4581301 thank you! i had been using an online C++ compiler at the time and it was very limited. (cpp.sh) im in a C++ class in school, and i dont really have access to downloading anything like Geany or Quincy (we use these on the computers in the classroom, but not on our laptops) i tried using a different online compiler and the debugger did help! i didn't yet figure out the problem but i will get to it now. thank you for your help, and ofc i'd use 'string' , keep it simple! – bleachedb Oct 31 '22 at 13:43
  • `string`, sorry it wouldn't let me edit it anymore :/ – bleachedb Oct 31 '22 at 13:49
  • If you're forced to use online tools, https://www.onlinegdb.com/ is an online debugger. If you can, start the work with your laptop and install a good and simple IDE to make the debugging easy. Online tools will get there eventually, but right now they're not as good as what you can get for a PC or Mac. – user4581301 Oct 31 '22 at 15:43
  • thats actually what i do use! very helpful for debugging. – bleachedb Oct 31 '22 at 18:14

0 Answers0