I'm working on a text based game and my code won't compile

I'm working on a text based game and my code won't compile. It tries but never finishes. It just goes back and forth between control.h and setup.cpp

there are 4 files, only the first three are really in use but i'll include all 4.

control.h
#include <string>
#include "control.h"
extern string focus, name;
extern int faith, essence, intellect, charm, strength, agility, endurance;

---------------------------------------------------------------------------

main.cpp
#include <cstdlib>
#include <iostream>
#include "control.h"

using namespace std;
string focus, name;
void setup(focus);
int main(int argc, char *argv[])
{ cout << "Before assuming control of your character, please choose a name." << endl;
cin >> name;
cout << "Welcome to Tarna " << name << "." << endl;
system ("PAUSE");
system ("cls");
cout << "Well, I guess we all knew this day was coming." << endl;
cout << "It is time for you to venture out into the world and make it your own." << endl;
cout << "The first thing to do is choose your focus;" << endl;
cout << " faith, essence, intellect, charm, endurance strength or agility." << endl;
cin >> focus;
setup(focus);

system("PAUSE");
return EXIT_SUCCESS;
}

---------------------------------------------------------------------------

setup.cpp
#include <cstdlib>
#include <iostream>
#include <string>
#include "control.h"

using namespace std;
void varify();
void setup(focus) {
if (focus == "Faith" || focus == "faith") {
cout << "Ah yes, faith, calling upon the power of one's god to perform mighty deeds." << endl;
varify();
}
else if (focus == "Essence" || focus == "essence") {
cout << "Essence, the harnessing of your internal enegry and unleashing it upon the world." << endl;
varify();
}
else if (focus == "Intellect" || focus == "intellect") {
cout << "Intellect, training the mind to gain greater mastery over yourself and the world around you." << endl;
varify();
}
else if (focus == "Charm" || focus == "charm") {
cout << "Charm, the art of influensing the people around you." << endl;
varify();
}
else if (focus == "Endurance" || focus == "endurance") {
cout << "Endurance, the ability to take what's throwen at you and then keep going." << endl;
varify();
}
else if (focus == "Agility" || focus == "agility") {
cout << "Agility; speed, acuracy and the ability to dodge." << endl;
varify();
}
else if (focus == "Strength" || focus == "strength") {
cout << "Strength; over all physical ability." << endl;
varify();
}
else {
cout << "I'm sorry, what is your focus? I don't know what " << focus << " is." << endl;
cin >> focus;
setup(focus);
}
}
void varify(){
string answer;
cout << "Are you sure this is the path you wish to take?" << endl;
cout << "yes/no" << endl;
cin >> answer;
if (answer[0] == 'n'){
cout << "Ok, than what do you want to focus in?" << endl;
cout << " faith, essence, intellect, charm, endurance, strength or agility." << endl;
cin >> focus;
setup(focus);
}
else if (answer[0] == 'y'){
cout << "Than we shall move on to assigning your skill points." << endl;
}
else {
cout << "You must answer either yes or no." << endl;
varify();
}
}

-----------------------------------------------------------------------
stats.cpp
#include <cstdlib>
#include <iostream>
#include "control.h"

using namespace std;
int faith, essence, intellect, charm, strength, agility, endurance;
void statcalc () {
}

void faith() {
int strength, speed, potency, total;
}

void essence () {
int strength, speed, potency, total;
}

void intellect () {
int knowledge, wisdom, pickup, speed;
}

void charm () {
int inspire, intimidate, influence, resist;
}

void physical () {
int health, fatigue, movespeed, attspeed, dodge, block, hit, counter, maxdmg, meandmg, maxblock, meanblock,
}










If anyone knows what i'm screwing up, i'd appreciate a hint.
This is the first code i've ever written involving multiple files.
Did you just include the header file in itself?

Details on how to write a multi-file like that below.

-Albatross
Sorry about the double post, but this is going to be VERY long.

Writing C++ files across multiple files allows for much greater organization of your code, and makes it look generally much neater. That said, I don't think there is one tutorial on how to do it...

A multi-file C++ program consists of multiple cpp file and at least one custom header file. In the header file, you include preprocessor commands and/or declarations. In the cpp files, you include the implementations (the actual code). In other words, in the header file, you list the names, arguments, and return types of the functions, variables, classes, structs, unions, typedefs and maybe templates? that you will be using in more than one cpp file. The keyword extern is never needed.

Here's how one might write the Hello World DAMNIT WORLD, I HATE YOU, GTFO OF MY LIFE program across multiple files.
1
2
3
4
5
6
7
8
//main.cpp
#include "customheader.h"
int main ()
{
   return tellthetruth(true);
   //tellthetruth is a function that we will be declaring in the header file and implementing in another cpp file.
  //It will basically say a lot of nasty stuff unless its input is false... and then return 0.
}

1
2
3
4
5
6
7
8
//customheader.h
#include <iostream.h>
//If you include a header (named a, for example) in a header (named b), there will be no need to include header a
//In a file that includes header b.

int tellthetruth(bool shouldi);
//In header files you may only use preprocessor calls and declarations. You may not use implementations 
//(at least the last time I checked) outside of #define 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//IHATETHEWORLD.cpp
#include "customheader.h"
//As you see, there was no need to include iosteam.h

int tellthetruth(bool shouldi)
//Now, we deal with defining the actual code of our function.
{
   if (shouldi)
   {
      cout <<"DAMN THIS ****ING WORLD! I HATE IT! I HATE IT!\nIT'S SUCH A PIECE OF ****, AND I*censor*\n";
   }
   else
   {
      cout <<"This is such a pretty, such a wonderful world...\nThe bunnies eat rainbows and poop butterflies...\n";
   }
   cin.ignore();
   return 0;
}


Compiling files with multiple header files is compiler-specific. However, using gcc, one would use
gcc fileonelocation.cpp filetwolocation.cpp headerlocation.h

I'm not 100% sure if you need to include the header file, but just to be safe...

I'm not satisfied with this program. Let's write something a lil bit more complex...

1
2
3
4
5
6
7
8
9
10
//main.cpp
#include "customheader.h"
int main ()
{
   cout <<"What's your name?\n";
   cin>>naem;
   //naem is declared in the header.
   return greet(naem);
   //greet is defined in greet.cpp.
}

1
2
3
4
5
6
7
//customheader.h
#include <iostream.h>

char naem[10] = "";
//You can declare variables in headers as well!
//You can also initialize them to some value.
int greet(const char * naemi);

1
2
3
4
5
6
7
8
9
10
//greet.cpp
#include "customheader.h"

int greet(const char * naemi)
//Now, we deal with defining the actual code of our function.
{
	cout <<"WELL WHAT THE **** ARE YOU DOING IN MY LIFE, " << naemi <<"!?!\n GET OUT! GET THE **** OUT!\n";
        cin.ignore();
	return 0;
}


You'd compile this the exact same way.

And what about classes? How does one deal with them?

1
2
3
4
5
6
7
8
9
10
//main.cpp
#include "customheader.h"
int main ()
{
   cout <<"What do you want to do?\nPress 1 to give up on life.\n";
   classy.get();
   //classy is declared in the header and defined in classy.cpp
   cin.ignore();
   return 0;
}

1
2
3
4
5
6
7
8
9
10
11
12
//customheader.h
#include <iostream.h>

class classofnintyfive
{
   private:
      int WolfieMcWolfenstein;
      void reask();
      void givup();
   public:
      void get();
} classy;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
//classy.cpp
#include "customheader.h"

void classofnintyfive::get()
{
   while(true)
   {
      cin >> WolfieMcWolfenstein;
      if(WolfieMcWolfenstein==1)
      {
         classofnintyfive::givup();
         break;
      }
      else
      {
         classofnintyfive::reask();
      }
   }
}
void classofnintyfive::givup()
{
   cout << "Excellent choice.\nYou feed yourself to your dog. Enjoy the rest of your day.\n";
}
void classofnintyfive::reask()
{
   cout << "Because this program was written by an insane geek, you may not fail to give up on life. \nTry again.\n";
   classofnintyfive::get();
}


By now, you should have the general idea of what to do, and I used up over half the maximum number of characters explaining everything.

Enjoy,
-A very exhausted Albatross


ghym

I think your problem is with the header files,when ever you create a header file you should put a inclusion guard so that you compiler will not define the header mutli times and that will cause an error ok you should put in every header file you make like this:

controls.h

#ifndef CONTROLS_H
#define CONTORLS_H


your classes or ......etc


#endif

sorry for my english i hope you understand it.
Topic archived. No new replies allowed.