Public Group

# simple code errors

This topic is 3225 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.

• 32
• 12
• 10
• 9
• 9
• ### Forum Statistics

• Total Topics
631351
• Total Posts
2999481
×