I compiled the following code and got one error:
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <cmath>
using namespace std;
void player_house(); //Room: the main characters house
void town_square(); //Room: the town square
int main(int argc, char *argv[])
{
//Declare local variables
float player_h; //Player health
float player_g; //The amount of gold the player has
int player_l; //The amount of lives
char yn; //choice of yes or no for questions
char y = 'y'; //Choice of yes
char n = 'n'; //Choice of no
char d; //Direction input
//Declare arrays
char name[35]; //Player name
char town[4][15] = { //Array for town
{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,29,40,41,42,43,44,45,46,47,48,49},
{50,51,52,53,54,55,56,57,58,59,60,61},
};
//Main code
cout << "Shadow gate - A massive rpg adventure." << endl;
cout << endl;
cout << " * * " << endl;
cout << " * * " << endl;
cout << " * * " << endl;
cout << " * * " << endl;
cout << " * * " << endl;
cout << " * * " << endl;
cout << "***** ***** " << endl;
cout << " * * " << endl;
cout << " * * " << endl;
cout << endl;
cout << "What be thy name, warrior?" << endl;
gets(name); //Read the whole array. player types in name
cout << "I see great potential for you, " << name << " the warrior." << endl;
cout << "Welcome to Grant Hill village " << name << "." << endl;
cout << "Do you have any questions?" << endl;
cin >> yn;
if (yn == y)
{
cout << "I am sure you have many. However, I am not the one to answer them. You should ask people around town. They will know more then I do." << endl;
}
if (yn == n)
{
cout << "Good. We do not have time for that. If you DO have questions though, you should ask around town." << endl;
}
bool done = false;
do
{
cout << "G: " << player_h << endl;
cout << "Health: " << player_h << endl;
cout << "Lives: " << player_l << endl;
if (town[x][y] == 1)
{
town_square;
}
if (town[x][y] == 2)
{
player_house;
}
} while (!done);
system("pause");
return 0;
}
void Player_house()
{
cout << "You are in my house!" << endl;
}
void town_square()
{
cout << "You are in town square!" << endl;
}
I get the following error:
'x' undeclared
I dont know why im getting this error. if you look at my code, youl notice on my if statments, I have town[x][y]. I assumed x and y were a and y as in x,y cordinates, but I think the compiler thinks there variables. I am basing my code off of an example some one gave me, so that code might help:
#include <iostream>
using namespace std;
void room_type_1 (int &x, int &y)
{
char d;
cout << "you can move in any direction" << endl;
cout << "which direction do you want to go? (N, S, W, E): ";
cin >> d;
if (d == 'n') y --;
if (d == 's') y ++;
if (d == 'w') x --;
if (d == 'e') x ++;
}
void room_type_2 (int &x, int &y)
{
char d;
cout << "you can move north and south" << endl;
cout << "which direction do you want to go? (N, S): ";
cin >> d;
if (d == 'n') y --;
if (d == 's') y ++;
}
int main ()
{
int grid[5][5] = {
{ 1, 1, 1, 1, 1 },
{ 1, 1, 2, 1, 2 },
{ 1, 1, 1, 1, 1 },
{ 1, 1, 2, 1, 2 },
{ 1, 1, 2, 1, 2 }
};
int x = 2;
int y = 3;
bool done = false;
do
{
cout << "x: " << x << endl;
cout << "y: " << y << endl;
if (grid[x][y] == 1)
room_type_1 (x, y);
else if (grid[x][y] == 2)
room_type_2 (x, y);
} while (!done);
return 0;
}
I also have a question. how do I create multiple source files? mainly, I want to have three files. main.cpp, battle.cpp, and resource.cpp main is all the main code. however, in battle and resource, I basically just want to use the code I make in them as a function in the other. so what I mean is that, I write the battle code in battle.cpp, but then just type something and get that code executed in main.cpp I dont know if that makes sense or not. I hope someone can help with my two problems. thanks!
[Edited by - jakpandora on September 16, 2004 2:51:24 PM]