So I am trying to use multiple source files for my text based game.
In Code::Blocks in doesn't even compile it says " cannot find -lfunctions.h".
In Dev-C++ it compiles but the function doesn't work properly
Here is the code
Main.cpp
#include <iostream>
#include <string>
#include <fstream>
#include "functions.h"
using namespace std;
int main()
{
int selection; //Selection of two of the beggining options
string newuser; // Name of the new user
int newpass; // Password of the new user
string exuser; // Existing user
int expass; //Existing password
string verifyuser; //Contains the username of an existing account in his .txt
int verifypass; //Contains the password of an existing account in his .txt
string action; //Action that the characther will do
cout << "Hello and welcome to The Warrior. Do you want to: \n";
cout<< "1. Play with an existing account.\n";
cout<< "2. Create a new account.\n";
cout<< "3. Help\n";
cout<< "Select: ";
cin>> selection;
cin.ignore();
if (selection == 1)
{
cout<< "\nWrite your username: ";
cin>> exuser;
cin.ignore();
cout<<"Now write your password: ";
cin>>expass;
cin.ignore();
string filename = exuser + ".txt";
if (ifstream(filename.c_str())) //Check if file exists
{
ifstream file(filename.c_str());
file >> verifyuser >> verifypass;
if ((verifyuser == exuser) && (verifypass == expass)) // Comproves if the user wrote his password and username correctly
{
cout<<"\nWelcome " << exuser << ". If this is your first time, please write HELP. Would you like to:";
getline(cin, action);
do_action(action, filename);
}
else //If he didn't write it correctly
{
cout<<"Wrong username or password. Please try again";
}
}
else //If file doesn;t exists
{
cout<< "Sorry, your accout doesn't exists. Please create one";
cin.get();
return 0;
}
}
if (selection == 2) //Creating a new account
{
cout<<"\nPlease write a username: ";
cin>>newuser;
cout<<"Now write the password: ";
cin>>newpass;
string filename = newuser + ".txt";
ofstream file (filename.c_str());
file << newuser <<" " << newpass << "\n";
file << "1\n";//Strengh
file << "1\n";//Defense
file << "1\n";//Hitpoints
file<< "0\n";//Experience
file<< "1\n";//Level
file.close();
cout<<"Now close the game and then try to login to your new account";
cin.get();
}
return 0;
}
functions.cpp
#include <iostream>
#include <string>
#include <fstream>
#include "functions.h"
using namespace std;
int do_action (string a, string filename)
{
for (int i=0; i < a.size(); i++) //Make string to lowercase
{
a[i]=tolower(a[i]);
}
if ("help" == a) //f user writes help
{
cout<<"This is a role-playing textbased game. You can wite the following commands: \n";
cout<<"ATTACK: You will attack an enemy to level up and gain items.\n";
cout<<"HELP: To bring these instructions again\n";
cin.get();
}
else if ("attack" == a) //If user writes attack
{
cout<<"You hava attacked an enemy and you.....won";
cin.get();
}
else if ("stats" == a)
{
ifstream file(filename.c_str());
string find_pos;
getline(file, find_pos);//Skips first line
getline(file,find_pos);
cout<< "\nStrengh:" << find_pos << "\n";
cout<< "Defense:" << find_pos << "\n";
cout<< "Hitpoints:" << find_pos << "\n";
cout<< "Experience" << find_pos << "\n";
cout<< "Level:" << find_pos << "\n";
file.close();
}
return 0;
}
functions.h
#ifndef FUNCTIONS_H
#define FUNCTIONS_H
#include <string>
using namespace std;
int do_action(string a, string filename);
#endif
Thanks for your help,
AssaultM16