# code not working

## Recommended Posts

lordcorm    100
Can some one help me this code i wrote isnt working
#include <cstdlib>
#include <iostream>

using namespace std;

int character;

int main(int argc, char *argv[])
{

cout << "\t\t Primoris Games " << endl;
cout << "\t\t Presents " << endl;
cout << "\t\t Divine the first strike Demo\n " << endl;
cout << " Whats your hero's name: " << endl;
cin >> character;

void battle();

system("PAUSE");
return EXIT_SUCCESS;
}

void battle()
{
cout << " i guess it works " << character << endl;
}

##### Share on other sites
Prototype    2014
'void battle()' inside the main function needs to be just 'battle()'. Right now the compiler thinks you're defining a new function, which isn't allowed there.

##### Share on other sites
lordcorm    100
if i do that i get an error that says its undecleared?

##### Share on other sites
Morpheus011    309
That's because you have to define a function before you call it. That means you either need to have a function prototype before it's first use, or it needs to actually be defined before you call it. Local function definitions are illegal which is why you got the error you were getting.

This is all very basic C++ syntax stuff that is covered in virtually any book about how to program in C++. I would recommend picking one up as they will have the answers for you in hard ink so you don't have to post here hoping someone will get back to you for an issue as basic as this.

##### Share on other sites
lordcorm    100
Thank you!!!! GUYES

##### Share on other sites
TFS_Scorpion    128
Have a look at a couple simple changes I made with the code... explanation is below...

#include <string>#include <iostream>using namespace std;//Function Prototypesvoid battle();		//simply the declaration of the function so that the compiler knows what to expect			//when it's called//Application Global Variablesstd::string character;//Functionsint main(int argc, char *argv[]){            cout << "\t\t Primoris Games " << endl;    cout << "\t\t Presents " << endl;    cout << "\t\t Divine the first strike Demo\n " << endl;    cout << " Whats your hero's name: " << endl;	cin >> character;	//assigns the user's input string to the variable "character"    battle();	//calls the actual "battle" function declared above in the prototypes    system("PAUSE");				    return EXIT_SUCCESS;}/***************************************/Function: "battle"/Purpose:	To print to the screen, and validate that the function is being called correctly,/				and show that the "character" variable does in fact contain the user's input/**************************************/void battle(){     cout << " i guess it works " << character << "\n" << endl;}

Every function that you write in the program must have a declaration, which you see in the Prototypes area. The declaration doesn't call the function, it defines it, so that the compiler knows what the function should expect and/or return when the function is actually called later. Notice that function prototypes must end with a ";". The actual function is called inside of main, and the actual function implementation is below the main function.

After moving the declaration above the main function, about the only way I could see a "not declared" error popping up is if you forgot to add the ";" onto the prototype.

Secondly, notice I changed the character variable to a string, and not an int (int is an integer, which has no way possible to hold a string of characters). If left as an int (integer), the input would not be read correctly and you would get unexpected results. You can use a string with the standard std::cin, std::cout just as easily as an integer.

Just as a side note, try to force yourself to comment your code when you can. It helps you understand things better when you look at it later, and it doesn't actually get compiled with the code, so it doesn't make your program any larger at all.

Hope this helps :)
Scorp

EDIT -- Can someone please tell me how to use the -source- tag correctly in the forums here? I would use it here, but when I do, it converts every
> and < into the & lt ; and & gt ; equivalents, which really hoses things up :/

##### Share on other sites
Nytegard    839
Quote:
 Original post by TFS_ScorpionEDIT -- Can someone please tell me how to use the -source- tag correctly in the forums here? I would use it here, but when I do, it converts every> and < into the & lt ; and & gt ; equivalents, which really hoses things up :/

Copy the code you want in the source tags before you go into edit mode. This is just another thing GameDev apparently put in.

##### Share on other sites
TFS_Scorpion    128
That's what I do normally, but in a case where I c/p code over from the IDE and into a post, it doesn't work for me. I'm sure there's a way around it, but can't figure it out. I'm going to use this post for a moment and experiment... then change it completely into a formal reply and what worked if I find it :)

Scorp

EDIT -> Well... either I was smoking something last night and can't figure out what I did different, or my luck is just better today, as it works fine today. Maybe there was a ghost in the machine last night... anyhoo, it works now, so disregard, and thanks :)