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

All the files are in the same directory?

If so, all you need to do is add the two .cpp files to some kind of project (or makefile), and then build.

> cannot find -lfunctions.h".
-l is for libraries, not header files.
Like I said, in a simple case with everything in the same place, you don't need any additional compiler flags.

Oh, and remove the namespace from the header file. In future, if someone wants to use your header files, they may not want the namespace. There is no "undo" once a namespace has been added.
Instead, write

#ifndef FUNCTIONS_H
#define FUNCTIONS_H

#include <string>

// don't do this using namespace std;

int do_action(std::string a, std::string filename);

#endif

using namespace std is a pretty lazy habit anyway. You might try something like

using std::cin;
using std::cout;

to cut down on the really repetitive stuff, and use the full std:: approach for everything else.

When you start using other namespaces, the transition will be less harsh.

Thanks Salem. Another questions when you said project do you mean like .cbp (code::blocks project)? An I searched google on how to create a makefile but I couldn't found a good and detailed guide.

Thanks

EDIT: Fixed it. Thanks Salem

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.