# simple code errors

This topic is 3111 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello all, I have been away from this for a while and as a result I seem to have forgotten some simple coding. I started a game to see if I could get my knowledge of it back but it seems I need some help. I have to following code, and it seems to be doing something silly. //set up system #include <iostream> #include "Playerset.h" using namespace std;
void Login()
{
std::cout << "Welcome to the arena, \n";
std::cin >> name;
//start a new game
New();
}

void New()
{
//set attribute points to 10 and request an attribute to increase
atp = 10;
std::cout << "Welcome " << name << ". It appears you are new here, Just fill out this form to let us know more about you. /n";
std::cout << "/n It is very simple just press the fist letter of the atpribute you want to increase by one, do so until you have";
std::cout << " run out of atpribute points.";
std::cin >> atb;
// calculate new attribute point totals
Playeratp();
}

void Playeratp()
{
while atp > 0;
{
// take one from total attribute points
atp = atp - 1;
switch (atb)
{
case 'b':
{
brn++;
break;
}
case 'a':
{
agi++;
break;
}
case 'e':
{
end++;
break;
}
case 'w':
{
wil++;
break;
}
case 'i':
{
inte++;
break;
}
case 'l':
{
luk++;
break;
}
case 'c':
{
cha++;
break;
}
case 's':
{
spd++;
break;
}
}
}
}

void main()
{
}



##### Share on other sites
You need to define the New() function before the Login() function. Otherwise the compiler can't find the right definition of New when you call it in Login.

##### Share on other sites
Make sure to prototype your functions at the top, so you don't have to worry about the order you place them in.

//set up system#include <iostream>#include "Playerset.h"using namespace std; //<---- FYI you don't need this if you're going to                     //access everything with std:: anyway.// Function prototypesvoid Login();void New();// So on, so forth.// ...void Login(){	// display main menu	std::cout << "Welcome to the arena, \n";	std::cout << "Please Sign in. \n";	std::cin >> name;	//start a new game	New();}

Also try to use source tags when you're posting code on here, makes it much easier to read.

##### Share on other sites
oh duh, thanks guys!
and I wasn't aware about the std thing, I'll remember that.

Also I'm sorry but what do you mean by source tags? is it something to tell you which language I'm coding in? Forgive my ignorance.

Cheers,

##### Share on other sites
Quote:
 Original post by ScobboAlso I'm sorry but what do you mean by source tags? is it something to tell you which language I'm coding in? Forgive my ignorance.
If you enclose your source code in [source][/source] tags, it will preserve indentation, provide syntax colouring, and put your code in a white scroll box (like Satharis' example).

##### Share on other sites
Also your forgetting to pass arguments
std::cout << "Welcome " << name << ". It appears you are new here, Just fill out this form to let us know more about you. /n";

won't work because you never initialized name or passed it along.

##### Share on other sites
Quote:
 Original post by Shadowwoelfwon't work because you never initialized name or passed it along.

Possible, though if you look at the top he included a custom header. Unless he states otherwise, i was going under the assumption he declares all the variables there. There's quite a few that aren't visibly declared in the code he pasted, so one can assume they have global scope as well.

##### Share on other sites
Hey, Oh i did it in the code tags all it did was change the font. Thanks I'll keep that in mind, on other forums it was code, that's where I got it from. and yes, all seemingly undeclared variables are defined in the custom header shown below.

#include <string>using namespace std;// set up variablesstring name;char atb;int atp;// player attributesint brn = 1;int agi = 1;int end = 1;int wil = 1;int inte = 1;int luk = 1;int cha = 1;int spd = 1;//player skillsint atk;int def;int wep;int arm;int matk;int mdef;int mwep;int marm;int hit;//enemy attributesint estr;int eagi;int eend;int ewil;int einte;int eluk;int espd;int echa;//enemy skillsint eatk;int edef;int ewep;int earm;int ematk;int emdef;int emwep;int emarm;int ehit;

##### Share on other sites
Quote:
 Original post by SatharisMake sure to prototype your functions at the top, so you don't have to worry about the order you place them in.

My normal advice is to do the exact opposite: put functions in order, so that you don't have to prototype them. There are two reasons for this:

1) If you determine that you need to change the function's signature (i.e. return type, name or argument types), you only have to make the change in one place.

2) Any cyclic dependencies (mutual recursion) will be highlighted automatically, because you'll be forced to prototype them (since there's no order you can put them in that satisfies the compiler directly). :)

To the OP:

1) A newline is encoded with '\n'. '/n' is just a slash and a letter n.

2) "while atp > 0;" isn't legal; you need parentheses around the condition. However, "while (atp > 0);" is a loop by itself which either does nothing (if atp is less than or equal to 0 beforehand) or is stuck forever (if atp is greater than 0 beforehand). It's identical to "while (atp > 0) {}". You don't want the semicolon there. :)

By the way, there is no compiler error caused by the semicolon followed by an opening brace; it's legal (and often useful) in C++ to just group statements together between braces with no if/for/while/etc.

##### Share on other sites
Quote:
Original post by Zahlman
Quote:
 Original post by SatharisMake sure to prototype your functions at the top, so you don't have to worry about the order you place them in.

My normal advice is to do the exact opposite: put functions in order, so that you don't have to prototype them. There are two reasons for this:

1) If you determine that you need to change the function's signature (i.e. return type, name or argument types), you only have to make the change in one place.

2) Any cyclic dependencies (mutual recursion) will be highlighted automatically, because you'll be forced to prototype them (since there's no order you can put them in that satisfies the compiler directly). :)

Quoted for truth. That way, your code of free functions will always have a nicely readable "order":

int foo () {    return 0;}int bar () {    return foo();}int main () {    std::cout << bar() << std::endl;}

as compared to wild style:
int bar () {    return foo();}int main () {    std::cout << bar() << std::endl;}int foo () {    return 0;}

This is still nice for only three functions, but imagine real world code, possibly with 20+ smaller and bigger functions in just one source code file.

That rule is not enforcable in classes:
struct Foo {    int length() const {        return size();    }    int size () const {        return foos.size();    }private:       std::vector<int> foos;};

This is a nice and a bad feature at the same time. Bad, because you can write wild-style code, good, because you can move all public entities to the top, where programmers look first for documentation.

##### Share on other sites
Quote:
 Original post by ScobboHello all,I have been away from this for a while and as a result I seem to have forgotten some simple coding. I started a game to see if I could get my knowledge of it back .......Scobbo

You might want to consider writing real variable names, just in case anyone besides you will ever look at the code..
Without looking at your switch statement, i was only able to guess agi, wil, inte and spd (speed/spellpower)
cha,luk,end and brn made no sense at all.. and you feel comfortable writing inte with 4 letters, but luck with 3..?

It's up to you how you write your code, just remember that someone else might read it at some point..

Also, if players/enemies has mostly the same attributes and skills, might as well just make a struct for them :)

##### Share on other sites
Hello agian.

I know my code is a little bit bad, I'm still learning, and all of your advice is greatly appreciated.

@Zahlman:
Yeah I found the while loop and changed it to be correct then found that it would be easier with an if statement, because a while really didn't suit the situation. And I realised the /n \n thing too, I felt silly.

@Phersnel:
Thanks I will reorder them striaght away, but I'll still prototype them just as a precaution.

@Marius:
Yeah I haven't yet but I plan to write the name of the attribute next to it in the header file. I was using the 3/4 letter variable names for ease, inte was just because int is already taken but the language. And that is a good idea, I'll brush up on classes and make one for the attributes. Thanks heaps everyone!

##### Share on other sites
Quote:
 Original post by Scobbo@Phersnel:Thanks I will reorder them striaght away, but I'll still prototype them just as a precaution.

s/Phersnel/Phresnel/

You should really not introduce redundancy if not necessary, it will only introduce more useless work. Also, there will be a time sooner or later (probably sooner, not later) where you fail or forget or fail and forget to keep track of all your prototypes. Further, once you fail to keep track, you are spamming your compiler with useless and unused symbols.

You ain't gonna need it.
Keep it small and simple.
Don't repeat yourself.
Problems associated with duplicate code.

##### Share on other sites
Oops, Sorry Phresnel, and ok I'll do that, thanks for the advise.

One last thing I have just been reviewing classes, and I was wondering if I needed to have a constructor for the class that was suggested for the player attributes, so when I set up the external file i/o for saving progress I can pass the information easier to the struct. I'm not sure if that would be necessary. Thanks!

##### Share on other sites
Quote:
 Original post by Scobbo@Marius:Yeah I haven't yet but I plan to write the name of the attribute next to it in the header file. I was using the 3/4 letter variable names for ease, inte was just because int is already taken but the language. And that is a good idea, I'll brush up on classes and make one for the attributes. Thanks heaps everyone!

Don't use comments to clarify your variable names: fix the names themselves. Most of a programmers time is spent thinking, not typing, so using cryptic names may save you some typing, but it'll make the thinking harder - especially when projects grow.

And yes, a constructor would be good - even if it's just there to ensure that those variables have a sane value to start with. Classes are useful to encapsulate things: rather than manually mocking with a class' internal variables, that class should provide functions that allow you to work with it on a higher level.

For example, you could do the following:
class Character{public:   int health;   int armour;   bool alive;};Character bob;bob.health = 100;bob.armour = 10;bob.alive = true;// Damage bob:int damage = 40;if(damage - bob.armour > 0){   bob.health - (damage - bob.armour);   if(bob.health < 0)      bob.alive = false;}

But this code makes it much easier and reliable, because you can call a single function to apply damage to a character and that function will take care of the details for you:
class Character{public:   Character(int health, int armour)      : health(health)      , armour(armour)      , alive(true)   {   }   void applyDamage(int damage)   {      if(damage > armour)      {         health - (damage - armour);         if(health < 0)            alive = false;      }   }   int health;   int armour;   bool alive;};Character bob(100, 10)// Damage bob:bob.applyDamage(40);

A next step would be to make those variables private, and to handle everything through functions. This allows you to make more robust code. For example, right now it's possible to say: bob.health = 100; bob.alive = false;. Or to set bobs armour to a negative value. When you work through functions (we call that an 'interface'), you can make sure those things never happen.

##### Share on other sites
Thanks Captain P,
So basicly I should put all the functions into the class, so that when the character is in combat I can just call the deal damage function of the correct object with an amount of damage. The problem I have now is can I call a function with in a class with a parameter that is a variable from the same class but different object say I do this?

class Character{public:   Character(int health, int armour, int attack)      : health(health)      , armour(armour)      , attack(attack)      , alive(true)   {   }   void applyDamage(int damage)   {      if(damage > armour)      {         health - (damage - armour);         if(health < 0)            alive = false;      }   }   int health;   int armour;   bool alive;};Character bob(100, 10, 15), jim(100, 10, 15)// Damage bob:bob.applyDamage(jim.attack);

To me that looks right but I'm a newbie so I could be wrong, I'd give it a go and check it out but I'm writing this before I head of to work, I'll be doing as much as I can in the moments I have to do it.

Thanks!

EDIT: Spelling

[Edited by - Scobbo on August 18, 2009 5:31:57 PM]