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(){
    
}

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.