Help with C++

Grab your favourite IDE and tinker with the innards of game engines

Postby DarkMortar on Tue Apr 03, 2007 5:10 am

When I wrote in BASIC, i did an entire text rpg with combat system with purely goto statements, you can keep track of ur gotos by making true/false variables of which goto you were at.


... but just say no.
Image
DarkMortar wrote:If you copied and pasted this in notepad, you may be a nerd. If you just highlighted this, you may be intelligent. If you read this like it is, you may become blind.
User avatar
DarkMortar
Senior Member
Senior Member
 
Joined: Wed Feb 09, 2005 12:19 am
Location: California, US.

Postby jaddorii on Tue Apr 03, 2007 8:43 am

Thanks for all the help. But im not that good at C++ so i kinda need some help with the object oriented and Methods stuff. I get that its like a new part of code that gets called from the some other code ... hmm good thing to separate code... but I just cant seem to get it working. Cuse i tried to make a function (its like methods right?) and i got totally messed up.

EDIT:
if((action != 'A') || (action != 'a'))
&&((action != 'D') || (action !='d'))
&&((action != 'W') || (action != 'w'))
{
cout << endl
<< "Invalid input."
<< endl;
input();
}

This part didn't work :S
I got these errors.

error C2143: syntax error : missing ';' before '&&'
error C2143: syntax error : missing ';' before '&&'
warning C4390: ';' : empty controlled statement found; is this the intent?

I tried putting semicolons after the )'s put that didn't work either cus i got the same errors
Dom Dom Dom... another one bites de_dust
User avatar
jaddorii
1337 p0st3r
1337 p0st3r
 
Joined: Sun Jul 16, 2006 10:24 am

Postby jaddorii on Tue Apr 03, 2007 9:06 am

Damnet!! I tried to redo the whole code with methods and stuff but when i launch it i get:

Press any key to continue

No text at all :'( Can i get some1s msn so they could help me ?
Dom Dom Dom... another one bites de_dust
User avatar
jaddorii
1337 p0st3r
1337 p0st3r
 
Joined: Sun Jul 16, 2006 10:24 am

Postby vecima on Tue Apr 03, 2007 2:45 pm

sorry for the double post, it said network error or something.
Last edited by vecima on Tue Apr 03, 2007 2:48 pm, edited 1 time in total.
vecima
1337 p0st3r
1337 p0st3r
 
Joined: Sun Apr 16, 2006 8:48 pm
Location: Jersey Boy

Postby vecima on Tue Apr 03, 2007 2:45 pm

Code: Select all
   if((action != 'A') || (action != 'a'))
   &&((action != 'D') || (action !='d'))
    &&((action != 'W') || (action != 'w'))
   {
      cout << endl
          << "Invalid input."
          << endl;
      input();
   }


that code (that you posted) has messed up ( and ).
you only need the extra ones at the beginning and end, to group everything together. also, you had some ||, but in this case they should all be && (like i posted before). if action is not equal to A AND not equal a AND not equal to D AND not equal to d AND not equal to W AND not equal to w, (they all have to be true for action to be invalid) then we need to get a another input.
try this: (and use the code and /code tags when you post code... like this: [*code]put your code here[*/code] but remove the *s)

Code: Select all
if((action != 'A') && (action != 'a')
 &&(action != 'D') && (action != 'd')
 &&(action != 'W') && (action != 'w'))
{
   cout << endl
   << "Invalid input."
   << endl;
   input();
}
vecima
1337 p0st3r
1337 p0st3r
 
Joined: Sun Apr 16, 2006 8:48 pm
Location: Jersey Boy

Postby vecima on Tue Apr 03, 2007 3:52 pm

ok...

ordinarily i wouldn't just code something for someone, as it is best to attempt to learn it yourself, but you were having trouble seeing how to use methods, and this is a simple enough example that it may warrant this.

a note about methods: methods have a type. in your code you had int main(). this means that your method 'main' has the type of 'int'. it also means that when your 'main' method is called, it will return an integer value (you have 'return 0'). mehtods don't have to have int as their type. they can in fact have any type (primitive or user defined) that you need. if you want a method to just do something, and not necessarily return anything, you can use the 'void' type. methods can also have arguments passed to them. this is something that a method needs to do whatever it is it does. i'll give you an example here. the following method does a simple multiplication by 2 of whatever is passed in.

Code: Select all
int timesTwo(int number) {
    return number * 2;
}


now, hopefully you can see that this method is essentially useless, but it illustrates that our method has the return type of int, and takes one int argument. since 'number' is declared as an argument, it doesn't need to be declared again in the body of the method (the body is between the { and }). when looking at 'number' here, we call it an argument, but when looking at other code that uses this method, what we pass in we call a parameter. the parameter is defined in the code that uses this method, and becomes the methods argument. methods can have as many arguments as you need, but when you call them, the parameters you pass in must match the arguments in type and amount.

now to use this method, you add a call to it in whatever code you need:

Code: Select all
int main()
{
    // here the number is delcared.
    int someNumberToBeMultiplied;

    int newNumber;

    ... // other code here

    // here the number is initialized.
    cin >> someNumberToBeMultiplied;

    // here the number is 'passed' in as a parameter.
    newNumber = timesTwo(someNumberToBeMultiplied);

    ... // other code here
}


the following is your code done with methods. i wrote this up in Notepad, as i don't have a c++ compiler on my computer at work. this may not compile, but the point isn't just for you to copy it anyway, the point is for you to see the syntax of and logic behind using methods. this is NOT the object oriented way of doing this, but i think it will be helpful and useful to understand methods, and then understand how they fit into object oriented programming. to really benefit from object oriented programming here, you have to think about how complex this is really going to get. if each soldier will never be more than a number, you're fine already, but if they will have different amounts of 'attack power' or skill, or different equipment, then you should go for a class to define them. we'll look at that later.

this example also uses global variables. this generally isn't such a good practice, but it lets you understand a bit about scope. since these variables are outside all of the methods, the methods can all 'see' them. i was unable to determine the use of some of your global variables, but perhaps they are for future expansion of this.

Code: Select all
// Ett medival spel

#include <iostream>
#include <ctime>
#include <iomanip>
#include <cstdlib>
#include <time.h>
using namespace std;

void input();
void attack();
void defend();
void wait();

srand(time(0));


// The following variables are global.  they are accessable by
// any method including main.  in this case however, main
// does not need to access these.

// Allied Starting Forces

int FootSoldiers = 500;
int HorseMen = 100;
int Cannons = 50;
int Citizen = 400;


// Enemy Starting Forces

int EneSold = 1000;
int EneHors = 500;
int EneCans = 100;


// Allied Killing Dices

int SoldDice = rand() % 250; // Dice for allied soldiers dying
int HorsDice = rand() % 50; // Allied Horse Men diying
int CanDice = rand() % 5; // Allied Cannon destroyed


// Gold Dices

int GoldDice = rand() % 20000;
int GoldDiceDefend = rand() % 2000;
int SpyGoldDice = rand() % 200000;
int WaitGold = rand() % 20000;


// Enemy Killing Dices

int KillDiceSold = rand() % 400;
int KillDiceHors = rand() % 200;
int KillDiceCan = rand() % 50;


int main()
{
   cout << endl
   << "Hi and welcome to this Medival Game." << endl
   << "You will be starting with 500 Footsoldiers, 100 Horse Men, 50 Cannons," << endl
   << "400 citizens and 1000 gold." << endl
   << "Throughout the game you can take the following actions:" << endl
   << endl << endl //Give some space
   << "Attack:" << endl
   << "Attack your enemy to steal gold and capture prisoners." << endl << endl << endl //More space
   << "Defend:" << endl
   << "Defend your castle and send out spy's." << endl << endl << endl
   << "Wait:" << endl
   << "Wait for your citizens to train and join ur army, but be carefull you will be -" << endl
   << "vurnable against attacks."
   << endl;
   
   input();

   return 0;
}


//***************************************************************
// input()
// the method that gets called when input is needed.
//***************************************************************
void input()
{
   cout << endl
   << "What would u like to do next?" << endl
   << "A = Attack." << endl
   << "D = Defend." << endl
   << "W = Wait." << endl
   << "Q = Quit." << endl
   << endl;
   cin >> action2;

   if((action != 'A') && (action != 'a') &&
      (action != 'D') && (action != 'd') &&
      (action != 'W') && (action != 'w') &&
      (action != 'Q') && (action != 'q'))
   {
      cout << endl << "The input was invalid.  Please try again.";
      input();
   }
   
   if((action2 == 'A') || (action2 == 'a'))
   {
      attack();
   }
   if((action2 == 'D') || (action2 == 'd'))
   {
      defend();
   }
   if((action2 == 'W') || (action2 == 'w'))
   {
      wait();
   }
   else
   {
      return 0;
   }
}


//***************************************************************
// attack()
// the method that gets called when the player chooses to attack.
//***************************************************************
void attack()
{
   srand(time(0)); // Initialize random number generator.
   
   int SoldDice = rand() % 250;       // Dice for allied soldiers dying
   int HorsDice = rand() % 50;       // Allied Horse Men diying
   int CanDice = rand() % 5;       // Allied Cannon destroyed

   int GoldDice = rand() % 20000;       // Gold Gained

   int KillDiceSold = rand() % 500;   // Enemy Soldiers Killed
   int KillDiceHors = rand() % 100;   // Enemy Horse Men killed
   int KillDiceCan = rand() % 20;       // Enemy Cannons Destoyed

   cout << endl
   << "Your army attacks the enemy." << endl << endl
   << "===============================================================================" << endl
   << "You loose: " << SoldDice << " Footsoldiers, " << HorsDice << " Horse Men and " << CanDice << " Cannons." << endl
   << "===============================================================================" << endl
   << endl
   << endl
   << "===============================================================================" << endl
   << "You gained: "<< GoldDice << " gold from the enemy." << endl
   << "===============================================================================" << endl
   << "You killed: " << KillDiceSold << " enemy Footsoldiers, " << KillDiceHors << " enemy Horse Men and " << KillDiceCan << " enemy Cannons" << endl
   << "===============================================================================" << endl
   << endl; // To make some space
      
   int NewSold = FootSoldiers - SoldDice;
   int NewHorse = HorseMen - HorsDice;
   int NewCan = Cannons - CanDice;
   int NewGold = Gold + GoldDice;
   
   cout << endl
   << "===============================================================================" << endl
   << "Now you've got " << NewSold << " Footsoldiers, " << NewHorse << " Horse Men and " << NewCan << " Cannons." << endl
   << "===============================================================================" << endl
   << "Now you've got " << NewGold << " coins of gold." << endl
   << "==============================================================================="
   << endl;
   
   input();
   
}


//***************************************************************
// wait()
// the method that gets called when the player chooses to wait.
//***************************************************************
void wait()
{
   srand(time(0)); // Initialize random number generator.
   
   int SoldDice = rand() % 50;       // Dice for allied soldiers dying
   int HorsDice = rand() % 10;       // Allied Horse Men diying
   int CanDice = rand() % 10;       // Allied Cannon destroyed
      
   int GoldDice = rand() % 20000;       // Gold Gained

   int KillDiceSold = rand() % 10;    // Enemy Soldiers Killed
   int KillDiceHors = rand() % 5;       // Enemy Horse Men killed
   int KillDiceCan = rand() % 1;       // Enemy Cannons Destoyed

   cout << endl
   << "Your army waits for recruits." << endl
   << "===============================================================================" << endl
   << "You loose: " << endl
   << SoldDice << " Footsoldiers, " << endl
   << HorsDice << " Horse Men and " << endl
   << CanDice << " Cannons." << endl
   << "===============================================================================" << endl
   << endl
   << "===============================================================================" << endl
   << "You gained: "<< GoldDice << " gold from different taxes." << endl
   << "===============================================================================" << endl
   << "You killed: " << KillDiceSold << " enemy Footsoldiers, " << KillDiceHors << " enemy Horse Men and " << KillDiceCan << " enemy Cannons" << endl
   << "==============================================================================="
   << endl; // To make some space

   int NewSold = FootSoldiers - SoldDice;
   int NewHorse = HorseMen - HorsDice;
   int NewCan = Cannons - CanDice;
   int NewGold = Gold + GoldDice;

   cout << endl
   << "===============================================================================" << endl
   << "Now you've got " << NewSold << " Footsoldiers, " << NewHorse << " Horse Men and " << NewCan << " Cannons." << endl
   << "===============================================================================" << endl
   << "Now you've got " << NewGold << " coins of gold." << endl
   << "==============================================================================="
   << endl;
   
   input();   
}


//***************************************************************
// defend()
// the method that gets called when the player chooses to defend.
//***************************************************************
void defend()
{
   srand(time(0)); // Initialize random number generator.
   
   int SoldDice = rand() % 100; // Dice for allied soldiers dying
   int HorsDice = rand() % 10; // Allied Horse Men diying
   int CanDice = rand() % 10; // Allied Cannon destroyed
   
   int GoldDice = rand() % 20000; // Gold Gained
   
   int KillDiceSold = rand() % 750; // Enemy Soldiers Killed
   int KillDiceHors = rand() % 200; // Enemy Horse Men killed
   int KillDiceCan = rand() % 5; // Enemy Cannons Destoyed

   cout << endl
   << "Your army defends the castle." << endl
   << "===============================================================================" << endl
   << "You loose: " << endl
   << SoldDice << " Footsoldiers, " << endl
   << HorsDice << " Horse Men and " << endl
   << CanDice << " Cannons." << endl
   << "===============================================================================" << endl
   << endl
   << "===============================================================================" << endl
   << "You gained: "<< GoldDice << " gold from the enemy." << endl
   << "===============================================================================" << endl
   << "You killed: " << KillDiceSold << " enemy Footsoldiers, " << KillDiceHors << " enemy Horse Men and " << KillDiceCan << " enemy Cannons" << endl
   << "==============================================================================="
   << endl; // To make some space
      
   int NewSold = FootSoldiers - SoldDice;
   int NewHorse = HorseMen - HorsDice;
   int NewCan = Cannons - CanDice;
   int NewGold = Gold + GoldDice;
      
   cout << endl
   << "===============================================================================" << endl
   << "Now you've got " << NewSold << " Footsoldiers, " << NewHorse << " Horse Men and " << NewCan << " Cannons." << endl
   << "===============================================================================" << endl
   << "Now you've got " << NewGold << " coins of gold." << endl
   << "==============================================================================="
   << endl;
   
   input();   
}
vecima
1337 p0st3r
1337 p0st3r
 
Joined: Sun Apr 16, 2006 8:48 pm
Location: Jersey Boy

Postby longshanks on Tue Apr 03, 2007 4:24 pm

I'm glad to see someone trying to learn programming the way it should be done.. by starting simple. It's aggravating seeing so many people just diving straight into Source without any knowledge whatsoever.

Although I have to agree with most of the other programmers here, you should really create some simple object classes with their own methods and you will find you can make your game much more complicated while not making your code more difficult to understand. I have to admit though the way you are doing things is the way I started too. For your next attempt I recommend you try creating some simple OO stuff so you can start learning the basics of programming that way, and create your main game loop that does all the relevant logic.

BTW Why don't we have a programming forum on Interlopers? There are enough people here who understand this stuff.
longshanks
Turbine Entertainment Software
 
Joined: Mon Nov 14, 2005 11:39 pm
Location: BOSTON!!!

Postby jaddorii on Tue Apr 03, 2007 4:30 pm

Hmm shouldn't all the methods return all the values of Soldiers killed, Gold gained etc. and store it in the main or something ?
Dom Dom Dom... another one bites de_dust
User avatar
jaddorii
1337 p0st3r
1337 p0st3r
 
Joined: Sun Jul 16, 2006 10:24 am

Postby vecima on Tue Apr 03, 2007 5:44 pm

jaddorii wrote:Hmm shouldn't all the methods return all the values of Soldiers killed, Gold gained etc. and store it in the main or something ?


in this case, those things are global (i think... like i said, i didn't bother looking at every single variable (you have a lot - probably more than needed) so they don't need to return to the main method.

longshanks is right. go for object oriented stuff. i can help, but it would really be better to read something about it, then come back with questions if you have any. ask google.
vecima
1337 p0st3r
1337 p0st3r
 
Joined: Sun Apr 16, 2006 8:48 pm
Location: Jersey Boy

Postby Forceflow on Tue Apr 03, 2007 9:50 pm

longshanks wrote:BTW Why don't we have a programming forum on Interlopers? There are enough people here who understand this stuff.

I agree. This is by far the the Source-related board with the most coders on I have seen.
User avatar
Forceflow
1337 p0st3r
1337 p0st3r
 
Joined: Sun Jul 16, 2006 12:13 pm
Location: Belgium

Postby longshanks on Tue Apr 03, 2007 10:17 pm

The only other place that is active with coders is VERC, all the other coding forums are dead (hlcoders, wavelength etc..)

Interlopers needs a coding forum!
longshanks
Turbine Entertainment Software
 
Joined: Mon Nov 14, 2005 11:39 pm
Location: BOSTON!!!

Postby BlekksPoncho on Wed Apr 04, 2007 12:30 am

Yeah it definetely needs a coding forum. And jaddorii if you want to learn some oop c# is a great language for it. It isn't that different to c++ in some ways, I learnt some c++ then more indepth on c# then applied my oop knowledge back to c++, not nessecary, but I just found it helful.
-----------------------
I'm not drunk, I always eat plastic.
User avatar
BlekksPoncho
Sir Post-a-lot
Sir Post-a-lot
 
Joined: Sat Apr 01, 2006 10:48 pm
Location: England - UK

Postby bobthehobo on Wed Apr 04, 2007 3:16 am

Somebody PM Blink and ask.
I would love to see that, I'm trying to break into C++ myself (despite only having knowledge of VB :oops: ).
User avatar
bobthehobo
Pheropod
Pheropod
 
Joined: Thu Sep 08, 2005 9:27 pm

Postby Blink on Wed Apr 04, 2007 8:53 am

Done! ;-)
User avatar
Blink
Cool 'n that
Cool 'n that
 
Joined: Fri Oct 08, 2004 4:16 pm
Location: UK

Postby jaddorii on Wed Apr 04, 2007 10:18 am

WoW! Cool! I didn't know my request for help would end up with a coding forum :lol: anyway thanks for all the help. Im gonna re-do the code with OOP and see if it works. (also "upgrading" to modern combat :P)
Dom Dom Dom... another one bites de_dust
User avatar
jaddorii
1337 p0st3r
1337 p0st3r
 
Joined: Sun Jul 16, 2006 10:24 am
PreviousNext

Return to Programming

Who is online

Users browsing this forum: No registered users