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";
char userName[100];
cout <<"\nPlease enter your username: ";
cin >>userName;
cout <<"Hello, "<<userName<<"!\n\n";
cout <<"Please pick your race: \n";
cout <<"1 - Human\n";
cout <<"2 - Orc\n";
int pickRace;
cout <<"Pick your race: ";
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++.
before you start, make sure you downloaded dev C++.
lets start with a nice introduction to the user:
#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!












