Hi, this is my first attempt at actually making a game that is actually playable, instead of something boring that is just the same thing over and over.

The problem that I am having is that when the function startGame(); is called, the program closes. There are no errors, because I did have it recall the printPossibleChoices(); function and that worked. It's just the startGame(); doesn't appear to call the askForPlace(); function. Help?

Also something else that I dont understand is how to make the quitGame function erm... work. When the program runs, if I press 5 to close it straight away, then press 1 to say yes close it, it closes straight away. But if I do something else first like create new players then close it, it sometimes takes like 8 repetitions of pressing 5, 1, 5, 1.. to get the program to close.

#include <iostream>
#include <fstream>

using namespace std;

int playerCreateChoiceOrNot;
string playerName;
string newPlayerFileName;
int areYouSureYouWantToQuit;
string location;
int startGame();
int printPossibleChoices();
int askForPlace();
int inCasino();
int playRoulette();
int playSlots();


int quitGame(){
    cout << "Are you sure you want to quit? 0 = no, 1 = yes: ";
    cin >> areYouSureYouWantToQuit;
    if(areYouSureYouWantToQuit == 0){
                               cout << "\n";
                               printPossibleChoices();
    }
    if (areYouSureYouWantToQuit != 1 && areYouSureYouWantToQuit != 0){
         cout << "Please enter a valid choice.\n";
         quitGame();
    }
    if(areYouSureYouWantToQuit == 1){
         system("pause");
    }
}

int startGame(){
    int askForPlace();
}

int deletePlayer(){
    cout << "Can not delete players.\n\n";
}

int getPlayerList(){
    ifstream playerFile("C:\\Dev-Cpp\\programs\\game\\data\\players.txt");
    cout << "Current players:\n\n";
    while(playerFile >> playerName){
            cout << playerName << "\n";
    }
}

int printPossibleChoices(){
    cout << "1. Choose a player.\n2. Create a new player.\n3. List current players.\n4. Delete a player.\n5. Quit.\n\n";
    cin >> playerCreateChoiceOrNot;
    if(playerCreateChoiceOrNot == 1){
                               cout << "Enter a player's name: ";
                               cin >> playerName;
                               cin.get();
                               startGame();
    }
    else if(playerCreateChoiceOrNot == 2){
                               cout << "Enter the name of the player: ";
                               cin >> playerName;
                               
                               ofstream playerFile("C:\\Dev-Cpp\\programs\\game\\data\\players.txt", ios_base::app);
                               
                               playerFile << playerName << "\n";
                               
                               playerFile.close();
                               
                               newPlayerFileName = playerName + ".txt";
                               
                               
                               ofstream newPlayerFile;
                               
                               
                               newPlayerFile.open(newPlayerFileName.c_str());
                               
                               printPossibleChoices();
                               cout << "\n\n";
    }
    else if(playerCreateChoiceOrNot == 3){
                               getPlayerList();
                               printPossibleChoices();
                               cout << "\n\n";
    }
    else if(playerCreateChoiceOrNot == 4){
                               deletePlayer();
                               cout << "\n\n";
    }
    else if(playerCreateChoiceOrNot == 5){
                               quitGame();
    }
    else{
         cout << "Please enter a valid choice.\n";
         printPossibleChoices();
    }
}

int main(){
    printPossibleChoices();
}

int askForPlace(){
    int destination;
    cout << "Where would you like to go?\n\n1. Casino\n2. Bank\n3. Sports arena\n\n";
    cin >> destination;
    cout << "\n\n";
    if(destination == 1){
                   location = "Casino";
                   inCasino(); 
    }
    if(destination == 2){
                   location = "Bank";
                   cout << "You are in the " << location;
    }
    if(destination == 3){
                   location == "Sports arena";
                   cout << "You are in the " << location;
    }
    
}

int inCasino(){
    int secondDestination;
    cout << "You are in the " << location << "\n\nWhat would you like to play?\n\n1. Roulette\n2. Slot machines\n3. Go back";
    cin >> secondDestination;
    if(secondDestination == 1){
                         playRoulette();
    }
    if(secondDestination == 2){
                         playSlots();
    }
    if(secondDestination == 3){
                         askForPlace();
    }
}

int playRoulette(){
    
}

int playSlots(){
    
}

Dani AI

Generated

Short diagnosis and reliable fixes

The crash when "starting" comes from how control flows, not the IO library. startGame currently declares a local prototype instead of invoking the place-selection function, and many functions are declared to return int but never return a value. The combination of recursive menu calls, missing returns and unhandled bad input makes program state unpredictable — which also explains the intermittent "press 5 then 1 many times" quit behavior.

Simpler, more robust structure (example)

#include <iostream>
#include <limits>

int main() {
    bool running = true;
    while (running) {
        std::cout << "1) Choose player  5) Quit\n> ";
        int choice;
        if (!(std::cin >> choice)) {
            std::cin.clear();
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
            std::cout << "Please enter a number.\n";
            continue;
        }
        switch (choice) {
        case 1: /* call choosePlayer() */ break;
        case 5: {
            std::cout << "Quit? 1=yes 0=no: ";
            int q; std::cin >> q;
            if (q == 1) running = false;
            break;
        }
        default: std::cout << "Invalid choice.\n";
        }
    }
    return 0;
}

Targeted fixes and checks

  • Make menu functions void (or return clear status codes) instead of int with no return.
  • In startGame call the function: askForPlace(); (do not declare a prototype there).
  • Fix location == "Sports arena"; — use assignment =.
  • Use std::getline for player names (lets names have spaces) and always validate cin input, clearing and ignoring on failure.
  • Stop using recursion to redisplay menus; use the main loop and set a flag to exit (this eliminates the weird multi-press quit problem). Use return 0; at the end of main for a clean exit.

Notes tied to earlier replies

  • was right to flag the mistaken prototype and the bad return types; follow that and switch to loop-based flow.
  • ’s idea of returning from the call can "work" but is brittle when menus are recursive — the loop approach is safer.
  • ’s point about returning from main is worth following: returning a status is the standard, predictable way to exit.

Recommended Answers

All 4 Replies

On line 36 of what you posted you have int askForPlace(); you need to remove "int".

When compiling this I got lots of warnings because you have a bunch of functions that are defined to return an int but do not return anything. If you want them to return nothing then make them void functions otherwise you have to return a value or it will give a junk value.

And your main() function should look something like this

int main()
{
    /* code */
    return 0; //always return at the end of main
}

On line 36 of what you posted you have int askForPlace(); you need to remove "int".

When compiling this I got lots of warnings because you have a bunch of functions that are defined to return an int but do not return anything. If you want them to return nothing then make them void functions otherwise you have to return a value or it will give a junk value.

And your main() function should look something like this

int main()
{
    /* code */
    return 0; //always return at the end of main
}

I've been told to return 0 at the end in pretty much every tutorial I've seen but I don't because I have no idea what the point of it is and it works fine without it.

Also, lol dunno why I put int :P

IF you want quit to work right you need to add return 0; after line 91 in your else if block.

else if (playerCreateChoiceOrNot == 5)
{
    quit();
    return 0;
}
commented: Thanks for your help. +0

I've been told to return 0 at the end in pretty much every tutorial I've seen but I don't because I have no idea what the point of it is and it works fine without it.

Then go into history or literature and forget programming. If every tutorial says do it, what makes you think you don't have to?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.