take a look :)

hi this is my first big project as a beginner. :)
it's an RPG Menu ( Nothing special yet!) that i thought you could take a look at
and see what you think.
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <string> 
using namespace std;

// prototyping functions
void Intro(void);
void ClassIntro(void);


int main()
{
    Intro();
    cout << "" << endl;
    ClassIntro();
    
string PlayerName;
cout << "Enter your player name: ";
getline (cin, PlayerName);       
cout << "\nYour players name is " << PlayerName;
cout << endl <<  "Is that correct: <yes>/<no> ";
string yerno; 
cin >> yerno;        
if (yerno == "yes"){ cout << endl << "\nThank you. ";}

cout << endl << "Would you like to be a: "
     << endl << "1. Mage " << endl
     << "2. Warrior " << endl;
int Choice;
int strength;
int magic; 
cin >> Choice;
if (Choice == 1)
{
           strength = 10; magic = 30;
cout << endl << "You are a Mage. Your main ability is magic and your strength is "     
 << strength << "\nAnd your magic is " << magic << endl; 
 
}
if (Choice == 2)
{
strength = 30; magic = 10;
cout << endl 
<< "You are a Warrior. Your main ability is brute force and your strength is "
<< strength << "\nAnd your magic is " << magic << endl; 
           
}

    system("PAUSE");
    return 0;
}

void Intro(void)
{
  cout << "This RPG is text based and it is my first proper program that\n"
       << "includes functions and arrays. Hope you enjoy it.\n"
       << endl;

   return;

}

void ClassIntro(void)
{
     cout << "In this game. There are two classes that you can choose from.\n"
          << "They include. A warrior ans a mage. Each have different \n"
          << "attributes and make your character perform differently.\n"
     << endl; return;
}


this program was to illustrate that i can use functions and arrays( Not that i have used arrays yet! LOL.) but i will improve it and make it better. but, any comments?
Oh, and please no lectures on system ("pause");
it's annoying.
You're right System("PAUSE"); is annoying and here is why... Nah, I'm just messing with you.

There's not really a lot to comment on here other then including the 'void' with functions that do not take arguments was something we left behind with 'C'. And your two if then's for the class selection would scale better (be easier to add more classes) if they were switch cases.
Thank you. I will replace stuff with classes, but i am a newbie at C++ so i don't want to skip ahead. i basically did this program as a function practice thing.

Okay i will leave the arguments for function blank, i put 'void' because it was just the way i learnt.
Last edited on
Your code doesn't work correctly when you type in "no" instead of "yes". Also, you might want to add more validation code so the program does not exit until the number is entered.
yeah thanks. i have now sorted that out. i have also added classes which are easy as hell to learn. thank you all.
if youre saying its just the way you learned then perhaps someone should explain return types, but mostly because i like giving lectures.

generally when you write a function youre going to want to get some kind of data back from it, unless its just a function to print something like you made in the above code, which is fine. however lets say you want a function to add two numbers then you would need to change the return type. if it was void, your function would perform the calculation but wouldnt give it back to you as can be seen here:
1
2
3
4
5
6

void addNums(int num1, int num2)
{
	int sum = num1 + num2;
	return;
}


if we wanted to actually get the sum of the two numbers then we would have to change the return type from "void" to "int" so the function would then look like this:
1
2
3
4
5
int addNums(int num1, int num2)
{
	int sum = num1 + num2;
	return sum;
}


two things to note about return types:
1. if your return type isnt void then you MUST return something.
2. whatever you return must match the return type, that is to say you couldnt return 2.3 if the return type was int or else you would get an error. however if you changed the return type to double it would function (no pun intended) perfectly.

also note that whenever returning a value it will be lost unless you store it in a variable. using the above mentioned function to get this result your main function would look something like this:
1
2
3
4
5
6
7
8
9
10

int main(void)
{
	int num1 = 3;
	int num2 = 5;
	int sum = addNums(num1, num2);

	cout << sum;
	return 0;
}

here its storing the sum of num1 and num2 (8) in the sum variable and then just printing it ofcourse. alternatively if you just want to print the value you could do something like this:
 
cout << addNums(num1, num2);


:o how interesting that the return type of main is int and it happens to return an integer value (0).... omg!

note however that you cant return an array, but you can still return a pointer. be careful though because if its a pointer to an array defined in the function youll run into trouble because the array will be deleted at the end of the function call :(

obviously theres a lot more intricacies to return types and a HELL of a lot more to functions but hope dis helped <33 :)
Last edited on
Thank you Ascii, why on earth you on the beginner section lol.
but anyways thank you for that demo. :)
hi, again.
i have replaced my if's and that with a class:

class Mage 
{
      public: 
      int health;
      int strength;
      int magic;
      void attack()
{
cout << "You are attacking..." << endl;
};
};
  Mage M; 
  M.health=100;
  M.strength=10;
  M.magic=30;

class Warrior
{
public:
       int health;
       int strength;
       int magic;      
       void attack()
       { cout << "You are attacking..." << endl;
            };
            };
      
Warrior W;
  W.health=100;
  W.strength=30;
  W.magic=10;
}      


but, how would you make a class a choice so if you wanted a warrior class how would you 'select' it. as a user point of view.
Last edited on
You don't need to create an instance of your class, and to initialize it you better use make use of constructors, e.g:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Mage
{
public:
         Mage(): health(100), strength(10), magic(30) {}; // construct Mage with default attributes
         Mage(int h, int s, int m): health(h), strength(s), magic(m) {}; // construct Mage with custom attributes
         //other functions, especially getters 
         void attack() { std::cout << "Attack!" << std::endl; }; 
private:
         int health, strength, magic;
};

// in main you use your class like this
std::cout << "Choice: 1)Mage 2)Warrior 3)Troll " << std::endl;
std::cin >> choice;
if (/* choice == Mage */) 
          Mage mage; // gives you a generic mage or 
          Mage Gandalf(200, 50, 250); // etc...
else /* other menu choices */
haha thanks but im not a very good programmer yet :o i just finished the chapter on functions in my book LOL!

anyhow i appreciate it :)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
class Warrior
{
    private:
        int Health;
        int Strength;
        int Magic;
    public:
        void Attack();
        Warrior();
        ~Warrior(){};
};

Warrior::Warrior()
{
    Health = 100;
    Strength = 20;
    Magic = 10;
}

class Mage
{
    private:
        int Health;
        int Strength;
        int Magic;
    public:
        void Attack();
        Mage();
        ~Mage(){};
};

Mage::Mage()
{
    Health = 100;
    Strength = 10;
    Magic = 20;
}

void Warrior::Attack()
{
    cout << "You are attacking..." << endl;
}

void Mage::Attack()
{
    cout << "You are attacking..." << endl;
}

int main()
{
    Warrior ProtectionWarrior;
    Mage FireMage;
    ProtectionWarrior.Attack();
    FireMage.Attack();
    system("pause");
    return 0;
}


Output:

You are attacking...
You are attacking...


This is not the most efficient way, but it is a lot cleaner. When you get into inheritance you can make it more efficient.
Topic archived. No new replies allowed.