simple code errors

Started by
14 comments, last by Scobbo 14 years, 8 months ago
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()
{
	// display main menu
	std::cout << "Welcome to the arena, \n";
	std::cout << "Please Sign in. \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':
		{
			// add one to brawn
			brn++;
			break;
		}
			case 'a':
		{
			// add one to agility
			agi++;
			break;
		}
			case 'e':
		{
			// add one to endurance
			end++;
			break;
		}
			case 'w':
		{
			// add one to willpower
			wil++;
			break;
		}
			case 'i':
		{
			// add one to inteligence
			inte++;
			break;
		}
			case 'l':
		{
			// add one to luck
			luk++;
			break;
		}
			case 'c':
		{
			// add one to charisma
			cha++;
			break;
		}
			case 's':
		{
			// add one to speed
			spd++;
			break;
		}
	}
	}
}

void main()
{
	Login();
}


and I am getting these error messages: 1>c:\documents and settings\administrator\my documents\visual studio 2008\projects\adv\adv\main.cpp(13) : error C3861: 'New': identifier not found 1>c:\documents and settings\administrator\my documents\visual studio 2008\projects\adv\adv\main.cpp(25) : error C3861: 'Playeratp': identifier not found 1>c:\documents and settings\administrator\my documents\visual studio 2008\projects\adv\adv\main.cpp(30) : error C2061: syntax error : identifier 'atp' But the thing is I have checked every where and that is the correct way to change the value of a variable and also calling functions doesn't work in other functions but it does in the main function even though (as far as I can see) they are formatted the same. Any help would be greatly aprreciated. Cheers, Scobbo EDIT: Changed tags from code to source [Edited by - Scobbo on August 17, 2009 6:24:48 PM]
Advertisement
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.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

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.
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,
Quote:Original post by Scobbo
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.
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).

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

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.
Quote:Original post by Shadowwoelf
won'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.
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;
Quote:Original post by Satharis
Make 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.
Quote:Original post by Zahlman
Quote:Original post by Satharis
Make 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.

This topic is closed to new replies.

Advertisement