0
101 Jul 07, 2006 at 20:14

I decided to write a simple text based game just incase someone’s interested in playing around with C++.

My compiler is dev C++ and i highly reccomend it. Click here if your interested in dev C++.

This is the sourcode:

#include <iostream>
using namespace std;

int main()
{
cout <<"\tWelcome to my text based game!\n";

cout <<"1 - Human\n";
cout <<"2 - Orc\n";
int pickRace;
cin >>pickRace;

switch (pickRace)
{
case 1:
cout <<"You picked the Human race.\n";
break;
case 2:
cout <<"You picked the Orc race.\n";
break;
default:
cout <<"Error - Invalid input; only 1 or 2 allowed.\n";
}

int difficulty;
cout <<"\nPick your level difficulty: \n";
cout <<"1 - Easy\n";
cout <<"2 - Medium\n";
cout <<"3 - Hard\n";

cout <<"Pick your level difficulty: ";
cin >>difficulty;

switch (difficulty)
{
case 1:
cout <<"You picked Easy.\n\n";
break;
case 2:
cout <<"You picked Medium.\n\n";
break;
case 3:
cout <<"You picked Hard.\n\n";
break;
default:
cout <<"Error - Invalid input; only 1,2 or 3 allowed.\n";
}

system("PAUSE");
return 1;
}


I will explain some of the code in here just incase your new to C++.

#include <iostream>
using namespace std;

int main()
{
cout <<"\t\tWelcome to my game!\n\n";

system("PAUSE");
return 1;
}


the ‘cout’ command shows the user that runs your .exe file the content you entered within the “”. Never forget the ; after your done with the content within the “”!
the ‘system(“PAUSE”);’ command obviously stops the DOS window from quickly showing and automaticaly closing.
the ‘return 1;’ command indicates the computer that the program ended without any problems. So it’s quite useful to write it! :lol:
the ‘\t\t’ within the “” after ‘cout <<’ moves the text you entered right after it a little bit to the right so the more ‘\t’ you enter the more the text moves to the right.

now, lets write something cool.
heres the code of an interesting program:

#include <iostream>
using namespace std;

int main()
{
cout <<"\t\tWelcome to my game!";

char name[100];
cout <<"What is your name? ";
cin >>name;
cout <<"Hello, "<<name<<"!";

system("PAUSE");
return 1;
}


Now we know how to use the cout command.
the ‘char’ command creates a variable. everything you enter right after the char part automaticaly becomes the name of the variable. the ‘[100]’ part i added means that the user can enter up to 100 characters in the text box.
the ‘cin’ command is basicaly a text box. The user can enter content in it that can be displayed later on. In order to use the content written by the user you must add the following code in a cout command:

"<<name<<"


in this example, it is:

cout <<"Hello, "<<name"!";


As you can see i added the ‘”<<name<<”’ part in the cout command. ‘name’ is in this case the name of the variable you wrote:

char name[100];


I’ll be editing this page later on when i find time, ill add more cool tricks soon =).

PS this is my first topic, hopefully it’s appropriate!

#### 25 Replies

0
101 Jul 07, 2006 at 21:03

@Alex007152

the ‘return 1;’ command indicates the computer that the program ended without any problems. So it’s quite useful to write it! :lol:

Is it compiler specific or main should return 0 to tell the OS that the program ended without any problem?

0
101 Jul 07, 2006 at 21:17

main should always return 0 on success. Non-zero specifies an error condition.

Most build tools will break on non-zero, for instance.

That said, in a stand-alone application, what you return doesn’t really matter. Noone will be checking your return code anyhow.

Still, get in the habit of returning ‘0’ for success. There’s various reasons this can be confusing (boolean values vs. system expected return codes), but just keep in mind that you want to return 0 to the OS.

0
139 Jul 08, 2006 at 00:09

Also, please use [ code ] tags rather than [ quote ] for code, in the future ;)

0
101 Jul 08, 2006 at 08:44

Thanks all, i will edit the first post. It’s just that i used ‘1’ instead of ‘0’ for this program as i got used to it due to several tutorials but now i’ve noticed what’s going on with it.

Will use code tags instead next time Reedbeta :p.

0
102 Jul 09, 2006 at 21:44

You might consider using a std::string instead of a char array to hold the user name input. Also, I think your program will break if the user enters a name with embedded white space.

0
101 Jul 09, 2006 at 21:49

[muse]
You know, it might be interesting/beneficial to make a Wiki post of something like this “game”, and then slowly refactor it, using the Wiki history as a way to catalogue the change history.

People could tune in and see the good/bad engineering practices, etc.
[/muse]

0
101 Jul 10, 2006 at 08:14

@monjardin

You might consider using a std::string instead of a char array to hold the user name input. Also, I think your program will break if the user enters a name with embedded white space.

Thanks for the advice. I’ve tested the program several times and no problems appeared.
@eddie

[muse]
You know, it might be interesting/beneficial to make a Wiki post of something like this “game”, and then slowly refactor it, using the Wiki history as a way to catalogue the change history. People could tune in and see the good/bad engineering practices, etc.
[/muse]

I see. I know this is no game, the point in it is to show the user the very very basics. Thanks for the advice, eddie.

0
101 Jul 10, 2006 at 15:13

@Alex007152

Thanks all, i will edit the first post. It’s just that i used ‘1’ instead of ‘0’ for this program as i got used to it due to several tutorials but now i’ve noticed what’s going on with it.

As pointed out, 0 is standard for success. The best reason I have heard for this is that there is only one way for the program to succeed, but multiple ways to fail. So returning a non-zero value gives more scope to return meaningful information when the program exits.

0
101 Jul 14, 2006 at 14:08

You may want to consider a loop around input options, something like:

bool validInput = true;
int pickRace;
do
{
cout <<"1 - Human\n";
cout <<"2 - Orc\n";
cin >>pickRace;

switch (pickRace)
{
case 1:
cout <<"You picked the Human race.\n";
break;
case 2:
cout <<"You picked the Orc race.\n";
break;
default:
cout <<"Error - Invalid input; only 1 or 2 allowed.\n";
validInput = false;
}
} while (!validInput);

0
102 Jul 14, 2006 at 16:10

You forgot to set validInput to true on a correct entry. ;)

0
101 Jul 14, 2006 at 16:15

Personally, I’d do:

bool validInput=false;
int pickRace;
while(!validInput)
{
cout <<"1 - Human\n";
cout <<"2 - Orc\n";
cin >>pickRace;

switch (pickRace)
{
case 1:
cout <<"You picked the Human race.\n";
break;
case 2:
cout <<"You picked the Orc race.\n";
break;
default:
cout <<"Error - Invalid input; only 1 or 2 allowed.\n";
continue;
}
validInput = true;
}

0
101 Jul 14, 2006 at 16:20

@monjardin

You forgot to set validInput to true on a correct entry. ;)

0
102 Jul 14, 2006 at 18:29

@eddie

If the user enters an incorrect character in Jare’s code then you get an infinite loop. validInput never gets set back to true. The break statement inside the switch block has no effect on the do-while loop. Right?
A continue is a different story, hence your code works.

0
101 Jul 14, 2006 at 18:33

Ah, right. Hence why I wouldn’t write that code. ;)

On another note, I find in practice I very rarely use do…whiles() actually.

0
102 Jul 14, 2006 at 18:46

If you take your approach a step further, you don’t even need the validInput variable:

int pickRace;
for (;;)
{
cout <<"1 - Human\n";
cout <<"2 - Orc\n";
cin >>pickRace;

switch (pickRace)
{
case 1:
cout <<"You picked the Human race.\n";
break;
case 2:
cout <<"You picked the Orc race.\n";
break;
default:
cout <<"Error - Invalid input; only 1 or 2 allowed.\n";
continue;
}
break;
}

0
101 Jul 14, 2006 at 20:08

You prefer the for( ; ; ), eh? :)

Any reason?

0
101 Jul 14, 2006 at 20:54

Because then you can write;

#define EVER ;;

// ...

for( EVER )
{
if( someCondition )
break;
}

0
101 Jul 14, 2006 at 20:56

Heh, that’s cute. ;)

Granted, defines like that are the tool of the devil. ;)

0
139 Jul 14, 2006 at 21:21

I actually saw that on one of those crappy motivation posters. It said

#define EVER ;;
for (EVER)
{
learn();
think();
grow();
}


How silly is that?

0
101 Jul 14, 2006 at 21:24

@Reedbeta

How silly is that?

I know! How can we be expected to learn, think or grow without knowing the function bodies? All it’ll result in is a linker error!

<groan> :)

0
101 Jul 16, 2006 at 22:00

Ah, but continue’s are evil! :) And it’s better to

#define forever for( ; ; )

anyway. ;) Anyway, the point was that checking for an error condition and continuing anyway instead of providing a way to correct the error is bad practice most of the times.

0
101 Jul 16, 2006 at 23:08

@Jare

Ah, but continue’s are evil! :) And it’s better to

#define forever for( ; ; )

anyway. ;) Anyway, the point was that checking for an error condition and continuing anyway instead of providing a way to correct the error is bad practice most of the times.

I hope you’re being sarcastic. :)

There’s nothing evil about continue – in fact they’re quite useful rather than using nesting to bloat out your code horizontally.

#define’s are nasty, as they aren’t scoped well. I forevermore can’t use the identifier ‘forever’ in any of my code without coming up with some crazy syntax error, or worse yet, an unintentional infinite loop. Besides, ‘while(1)’ is barely more keystrokes, and doesn’t cloud any identifiers.

That said, your final point is right. Make sure to get valid input before proceeding. :)

0
101 Jul 18, 2006 at 22:46

@eddie

I hope you’re being sarcastic. :) There’s nothing evil about continue

Tongue-in-cheek rather than all-out sarcastic. I admit I have a certain irrational phobia to continues, akin to the outright hatred of gotos that most modern software people have. I only like continue statements when they are VERY visible, and in small loops, but that’s a wholly different topic not really worthy of a discussion.

0
101 Jul 18, 2006 at 23:12

Fair enough. ;) I can see the issue with continues at times, but personally I loathe defines that aren’t truly necessary. :)

0
102 Jul 19, 2006 at 13:12

I think you both have some well-founded paranoias. :p

I’m having to pick apart some US Government C-code with liberal use of gotos at the moment. I’d don’t think they’ve ever heard of encapsulation. :( Goto code can be incredible difficult to reuse.

Continues on the other hand can cause confusion. However, I’d use either or both if they were the best way to solve a problem. :)