Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,431 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,920 people online right now. Registration is fast and FREE... Join Now!




Is there a better way of doing this?

 
Reply to this topicStart new topic

Is there a better way of doing this?

Trake
10 Jan, 2008 - 05:43 PM
Post #1

D.I.C Head
**

Joined: 29 Jun, 2007
Posts: 60


My Contributions
Hi all, I have just started creating a small game in C++ and below is the code I have so far (not much I know). But before I go further I want to know if what I am doing is using best practices, especially the error checking.

Thanks for any help

Trake

CODE
#include <stdlib.h>
#include <iostream>
#include <string>
#include <math.h>
#include <time.h>
#include <windows.h>

//Global variables
int difficulty = 0;

void playerCreate();

int main()
{
    srand ( time(NULL) ); //start srand

    char newGame; //local variable for getting start or exit

    while (newGame != 'q' && newGame != 'Q')
    {
        std::cout << "Hello, and welcome to Adventure Land.\nWould you like to start a new game? (q to quit)\n";
        std::cin >> newGame;            
        if (newGame == 'y' || newGame == 'Y')
        {
            playerCreate();
        }
        else
        {
            std::cout << "\nThat is not a valid choice please choose again.\n";
        }

    }
    std::cout << "\nThank you for playing!\n";
    return 0;
}

void playerCreate()
{
    system("cls"); //clear screen

    std::cout << "\nPlease select a difficulty from 1(easiest) to 3(hardest).\n";
    while (!(difficulty >=1 && difficulty <= 3))
    {
    std::cin >> difficulty;
    if (!(difficulty >=1 && difficulty <= 3))
    {
        std::cout << "\nThat is not a valid choice please choose again.\n";
    }
    }


}

User is offlineProfile CardPM
+Quote Post

VernonDozier
RE: Is There A Better Way Of Doing This?
10 Jan, 2008 - 11:39 PM
Post #2

New D.I.C Head
*

Joined: 6 Jan, 2008
Posts: 46

I ran it. It worked. As far as "best practices" go, a lot of people discourage global variables like how you used "difficulty" in your solution. Me, I use 'em, but I think I'm in the minority as far as that best practice goes. Also, you're comparing newGame to a value before it is initialized. It's unlikely, but what if the last value in that memory address was the ascii value of 'q' or 'Q'? You should initialize it to something before comparing just to be sure. Same thing goes with "difficulty". Though the odds are way against the chance that it was 1, 2, or 3 before you declared it, since you didn't initialize it, it could have been and the user would never be prompted. You could initialize both to 0 off the bat just to be safe.
User is offlineProfile CardPM
+Quote Post

mmakrzem
RE: Is There A Better Way Of Doing This?
11 Jan, 2008 - 09:26 AM
Post #3

New D.I.C Head
*

Joined: 11 Jan, 2008
Posts: 27


My Contributions
"best" is very relative. Depends on what you plan on doing with it I guess.

I avoid using global variables, and put everything inside of a class. Each class has its own .h and .cpp file.

That way if I need to reuse code in the future, I can just grab the pieces I need without have to go into code, and cut and paste.
User is offlineProfile CardPM
+Quote Post

Trake
RE: Is There A Better Way Of Doing This?
13 Jan, 2008 - 04:06 AM
Post #4

D.I.C Head
**

Joined: 29 Jun, 2007
Posts: 60


My Contributions
Thanks for the advice both, when it comes to classes, is it better to use classes over structs for something like this? Or what are the advatages/disadvantages of both?
I have tried to use classes before, but I end up setting things as public which makes me think that a struct with public as default is better?

Thanks in advance

Trake
User is offlineProfile CardPM
+Quote Post

baavgai
RE: Is There A Better Way Of Doing This?
13 Jan, 2008 - 07:18 AM
Post #5

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,047



Thanked: 106 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
In C++, classes are generally preferred, with structs being consider just little above primitives for usage cases. In reality, structs and classes can be near identical, so it's more an expression of intent.

You could do something like this:
CODE

#include <iostream>
#include <string>
#include <cctype>

using namespace std;

class Player {
private:
    int difficulty;
public:
    Player(int difficulty);
    int getDifficulty();
    void display(ostream& out);
};
Player::Player(int difficulty) { this->difficulty = difficulty; }
int Player::getDifficulty() { return this->difficulty; }
void Player::display(ostream& out) {
    out << "Player: " << this->difficulty << endl;
}


Player * playerCreate() {
    cout << endl;
    cout << "Please select a difficulty from 1(easiest) to 3(hardest)." << endl;
    
    int difficulty = -1;
    while (true) {
        cin >> difficulty;
        if (difficulty >=1 && difficulty <= 3) {
            return new Player(difficulty);
        }
        cout << "\nThat is not a valid choice please choose again.\n";
    }
}


char getResponseChar() {
    char ch;
    cin >> ch;
    return tolower(ch);
}


int main() {
    Player *player = NULL;
    srand ( time(NULL) ); //start srand

    while (true) {
        cout << "Hello, and welcome to Adventure Land." << endl;
        cout << "Would you like to start a new game? (q to quit)" << endl;
        char newGame = getResponseChar();
        if (newGame == 'y') {
            player = playerCreate();
            player->display(cout);
            delete(player);
        } else if (newGame =='q') {
            break;
        } else {
            cout << endl << "That is not a valid choice please choose again." << endl;
        }
    }
    cout << endl << "Thank you for playing!" << endl;
    return 0;
}


User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/5/08 05:00AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month