Dzielenie kodu na pliki .h i .cpp

0

Witajcie, mam pewien problem, napisalem prosty program do obslugi biblioteki, ale chciałbym podzielić go na pliki .h i .cpp, ale wysypuje mi się to przy kompilowaniu, a więc kod programu to:


```cpp
#include "stdafx.h"
#include <stdlib.h>
#include <iostream>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>//convert string to int
#include <iomanip> //text align
#include <conio.h>
#include <vector>
#include <algorithm>//find in vector
#include <io.h>
#include <Windows.h> // to Sleep() function

using namespace std;
HANDLE hOut;
class book{
public:
    int year;
    string kind;
    string tittle;
    int available;
    //constructor
    book(int year, string kind,string tittle,int available):
        year(year), kind(kind), tittle(tittle), available(available){}
    ~book(){}
};
class user{
public:
    string name;
    string pass;
    user(string name, string pass):
            name(name), pass(pass){}
};
class database{
private:
    bool admin;
    void login_menu();
    void login();
    void create_user();
    void add_book();
    void rent_book();
    void rented_book();
    void return_book(string username = "");
    void read_tab_book();
    void read_tab_user();
    void display_books();
    void remove_book();
    void edit_book();
    void save_tab_book();
    string user_name;
public:
    void dispaly_users();
    void user_menu();

    vector<user> tab_user;
    vector<book> tab_book;

    database(){
        read_tab_user();
        read_tab_book();
        login_menu();

    }
};
void database::user_menu(){
    char c = '1'; // default value
    while(c != '3'){
        system("cls");
        cout << "1. View books\n";
        if(user_name == "admin"){
            cout << "2. Add a book\n";  
            cout << "3. Edit a book\n";
            cout << "4. Remove a book\n";
            cout << "5. View rented books\n";
        }else{
            cout << "2. Rent a book\n";
            cout << "3. Return a book\n";
        }   
        cout << "9. Exit\n";
        cin>>c;
        switch (c){
        case '1':{display_books(); cin>>c; break;}
        case '2':{
            if(user_name == "admin"){
                add_book();
            }else{
                rent_book();
            }break;
                 }
        case '3':{if(user_name == "admin"){
            edit_book();
            user_menu();
                 }else{
                     return_book();
                     user_menu();
                 }break;}
        case '4':{if(user_name == "admin"){
            remove_book();
            user_menu();
                 } break;}
        case '5':{if(user_name == "admin"){
            rented_book();
            user_menu();
                 } break;}
        case '9':{exit(1);}
        default:{cout << "Enter correct digit!\n";}
        }
    }
}
void database::rent_book(){
    int nr;
    string snr;
    display_books();
    cout << "Type Id of book which You wannt to rent.";
    cin >> snr;
    nr = atoi(snr.c_str());//convert string to int   c_str - Returns a pointer to an array that contains a null-terminated sequence of characters( 
    if(nr > tab_book.size() || nr < 1){
        cout << "Enter correct id!";
        Sleep(999);
    }else if(tab_book[nr-1].available == 0){
        cout << "You can not rent this book, because its unavailable!";
        Sleep(999);
    }else{
        tab_book[nr-1].available = 0;
        string f_name = "data/";
        f_name += user_name;
        f_name += ".udat";
        fstream user_f(f_name.c_str(), ios::app); 
        user_f << nr << endl;
        save_tab_book();
        cout << "\nAction succesfull";
    Sleep(999);
    }
}
void database::rented_book(){
    string uName;
    _finddata_t fileData;
    long data = _findfirst("data/*.udat", & fileData ); //
    string fName = fileData.name;
    cout << "\nList of users who rented a book\n\n";
    cout << fName.substr(0, fName.length() - 5)<<endl;      //print filename without extension
    int nextData = _findnext(data, & fileData );        // go to next file
    while (nextData != -1){
        fName = fileData.name;
        cout << fName.substr(0, fName.length() - 5)<<endl;  //print filename w/o extension
        nextData = _findnext(data, & fileData );        // go to next file
    }
    cout << "\n\nType username to see what book he rent\n";
    cin.ignore();
    getline(cin, uName);
    return_book(uName);
}
void database::return_book(string username){           
    bool rent = 0;
    bool continuing = 1;
    int nr;
    string data;
    vector<int> tab_rented;
    string f_name = "data/";
    if (username != ""){
        f_name += username;
    }else{
        f_name += user_name;
    }
    f_name += ".udat";
    fstream user_f; 
    user_f.open(f_name.c_str());
    if(user_f.good()){
        while(!user_f.eof()){ //feof Checks whether the end-of-File indicator associated with stream is set, returning a value different from zero if it is.
            getline(user_f, data);
            nr=atoi(data.c_str());
            if (nr >0){
                tab_rented.push_back(nr);
                rent = 1;
            }
        }
    } else {cout << "You did not rent a book!\n";Sleep(1999);}
    user_f.seekg(0);
    user_f.close();
    if (rent){
        if (username == ""){
            cout << "books which You rented\n";
        }else{
            cout << "books which "<<username<<" rented\n";
        }
        for(int i=0; i<tab_rented.size(); i++){
            cout << tab_rented[i] << " " << tab_book[tab_rented[i]-1].kind << " " << tab_book[tab_rented[i]-1].tittle << endl;
        }
        if(username != ""){
            char z;
            cout << "Do you want to return " << username << " book? (y/n)\n";
            cin >> z;
            if (!(z == 'y' || z == 'Y')){
                continuing = 0;
            }
        }   
        if(continuing){
            cout << "\nWhich book you want to return?\n";
            cin>>nr;

            sort(tab_rented.begin(), tab_rented.end()); //vector has to be sorted to search.
            if(binary_search(tab_rented.begin(), tab_rented.end(), nr)){//check if is in array
                tab_book[nr-1].available = 1;

                //Remove element form tab_rented
                for(int i=0; i<tab_rented.size(); i++){
                    if(tab_rented[i] == nr){
                        tab_rented.erase(tab_rented.begin() + i);
                        break;
                    }
                }
                //Remove element form file
                if (tab_rented.size() > 0){
                    ofstream user_f(f_name.c_str());
                    for(int i=0; i<tab_rented.size(); i++){
                        user_f << tab_rented[i]<<endl;
                    }
                    user_f.close();
                } else {  //remove empty file
                    remove(f_name.c_str());
                }
                save_tab_book();
                cout << "\nAction succesfull";
            }else{
                cout << "Type correct number!";
                Sleep(999);
            }
        }

    }
}
void database::login_menu(){
    char z;
    HANDLE uchwyt;
    uchwyt = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(uchwyt,12);
    cout << "Welcome to Library management system. \n\n1. Login\n2. Crate new account\n\n\n\nDesigned by Pawel Opolski\n" << flush << endl;
    cin >> z;
    while(z != '1' && z !='2'){
        cin >> z;
    }
    if(z == '1'){
        login();
    }else if(z == '2'){
        create_user();
    }
}
void database::login(){
    system("cls");
    cout << "List of users\n";
    dispaly_users();
    cout << "\nLOGIN TO ACCOUNT\n\n";

    string name,pass;
    bool login = 0;

    while(login == 0){
        cout << "Enter name\n";
        cin >> name;
        cout << "Enter pass\n";
        cin >> pass;
        for(int i=0; i<tab_user.size(); i++){
            if(name == tab_user[i].name && pass == tab_user[i].pass){
                login=1; 
                break;
            }
        }
        if(login == 1){
            cout << "Login correctly\n";
            user_name = name;
            Sleep(1999);

        }
        else{
            cout << "Cant login!\n";
            Sleep(1999);

            system("cls");
        }
    }
}
void database::add_book(){
    int year;
    string kind,tittle;
    int available;
    string date;
    cout << "Type kind\n";
    cin.ignore( 80, '\n' );
    getline(cin, kind);
    cout << "Type tittle and author\n";
    getline(cin, tittle);
    cout << "Year of the book?\n";
    cin >> date;
    year = atoi(date.c_str());
    available = 1;
    book temp(year, kind, tittle, available);
    tab_book.push_back(temp);
    save_tab_book();
}
void database::remove_book(){
    unsigned int nr;
    cout << "List of books\n";
    display_books();
    cout << "\nType id of book which you want to delete:";
    cin >> nr;
    if(nr > tab_book.size() || nr <1){
        cout << "Enter correct id!";
        Sleep(999);
    }else if(tab_book[nr-1].available == 0){
        cout << "You can not remove this book, because its rented!";
    Sleep(999);
    }else{
        tab_book.erase(tab_book.begin() + nr - 1);
        save_tab_book();
    }
}
void database::edit_book(){
    unsigned int nr;
    int year;
    string kind, tittle, date;

    cout << "List of books\n";
    display_books();
    cout << "\nType id of book which you want to edit:";
    cin >> nr;
    if(nr > tab_book.size() || nr < 1){
        cout << "Enter correct id!";
        Sleep(999);
    }else{
        cout << "\n\nType kind\n";
        cin.ignore( 80, '\n' ); // If you write ignore(80,'\n'), up to 80 characters will be thrown away until a newline character is found
        getline(cin, kind);
        cout << "Type tittle and author\n";
        getline(cin, tittle);
        cout << "year of the book?\n";
        cin>>date;
        year = atoi(date.c_str());
        tab_book[nr-1].kind = kind;
        tab_book[nr-1].tittle = tittle;
        tab_book[nr-1].year =year;
        save_tab_book();
    }
}
void database::save_tab_book(){
    ofstream file;
    try { file.open("data/books.dat") ;
            for (int i=0; i<tab_book.size(); i++){
            file << tab_book[i].year << "|" << tab_book[i].kind << "|" << tab_book[i].tittle<<"|" << tab_book[i].available;
            if(i != tab_book.size() - 1) {file<<endl;}
        }
        file.close();
        }
catch (int param) {
        cout << "Cant save database!\n";
}
}
void database::create_user(){
    bool error = 0;
    system("cls");
    cout << "CREATE ACCOUNT\n\n";
    fstream file;
    file.open( "data/users.dat", ios::app); // iso::app All output operations are performed at the end of the file
    string name,pass;
    do{
        error = 0;//reset value
        cout << "Enter name\n";
        cin >> name;
        for(int i=0; i<tab_user.size(); i++){
            if(tab_user[i].name == name){
                cout << "\nPlease select diffrent name. This login exist\n";
                error = 1;
            }
        }
    }while(error);
    cout << "Enter pass\n";
    cin >> pass;
    if(file.good())
    {
        file << endl << name << "|" << pass;
        user temp(name, pass);
        tab_user.push_back(temp); //dodajemy na koncu wektora sobie elemencik
        file.close();
        cout << "\n\nUSER CREATED!";
        Sleep(999);
        user_name = name;
    } else {
        cout << "Cant create user!\n";
    }

}
void database::read_tab_book(){
    int year;
    string data, kind, tittle;
    int available;
    int found;
    int i = 0;
    fstream file;
    try {
    file.open("data/books.dat", ios::in); // Zezwolenie na odczytywanie danych z pliku.

    {
        while(!file.eof())
        {
            getline( file, data );
            found     = data.find_first_of("|");
            kind      = data.substr(0, found);//use unused string to change it to int
            year      = atoi(kind.c_str());
            data      = data.substr(found + 1); // substr returns a newly constructed string object with its value initialized to a copy of a substring of this object.
            found     = data.find_first_of("|");
            kind      = data.substr(0, found);
            data      = data.substr(found + 1);
            found     = data.find_first_of("|");
            tittle    = data.substr(0, found);
            data      = data.substr(found + 1);
            available = atoi(data.c_str());

            book temp(year, kind, tittle, available);
            tab_book.push_back(temp);
        }
        file.close();
    }
        } 
    catch(int param){
        cout << "books data not found!\n";
    }
}
void database::read_tab_user(){ 
    string data, name, pass;
    int found;
    int i = 0;
    fstream file;
    file.open("data/users.dat", ios::in);
    if(file.good())
    {
        while(!file.eof())
        {
            getline( file, data );
            found = data.find_first_of("|"); //Searches the string for the first character that matches any of the characters specified in its arguments.
            name  = data.substr(0, found);
            pass  = data.substr(found + 1);
            user temp(name, pass);
            tab_user.push_back(temp);

        }
        file.close();
    }else{
        file.close();
        cout << "User data not found!\nPlease type admin password.\n";
        string pwd;
        cin >> pwd;
        file << "admin|" << pwd;
        cout << "Admin account created, please login\nLogin: admin\tPassword:" << pwd << endl;
        Sleep(999);
        user temp("admin",pwd);
        tab_user.push_back(temp);
        ofstream file("data/users.dat");
        file << "admin|"<<pwd;
        file.close();
    }
}

void database::dispaly_users(){
    for(int i=0; i<tab_user.size(); i++){
        cout << tab_user[i].name << endl;
    }
}
void database::display_books(){
    cout << "NR  |   KIND   |          |  Author and tittle   |           |   Printed    |     |   Availible  |\n";
    cout << "_____________________________________________________________________________________________________________\n";
    for(int i=0; i<tab_book.size(); i++){
        cout << i + 1 << left << setw(4) << "." << setw(30) << tab_book[i].kind << setw(40) << tab_book[i].tittle << setw(23) << tab_book[i].year;
        if(tab_book[i].available == 1){
            cout << "yes\n";
        }else{
            cout << "no\n";
        }
    }
}
int main(){
    database newdb;
    newdb.user_menu();

    return 0;
}

a podzieliłem go w ten sposób:
plik database.h

#include "stdafx.h"
#include <stdlib.h>
#include <iostream>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>//convert string to int
#include <iomanip> //text align
#include <conio.h>
#include <vector>
#include <algorithm>//find in vector
#include <io.h>
#include <Windows.h> // to Sleep() function
#include "class_book.h"

using namespace std;

#ifndef _DATABASE_H_
#define _DATABASE_H_

class database{
private:
    bool admin;
    void login_menu();
    void login();
    void create_user();
    void add_book();
    void rent_book();
    void rented_book();
    void return_book(string username = "");
    void read_tab_book();
    void read_tab_user();
    void display_books();
    void remove_book();
    void edit_book();
    void save_tab_book();
    string user_name;
public:
    void dispaly_users();
    void user_menu();

    vector<user> tab_user;
    vector<book> tab_book;

    database(){
        read_tab_user();
        read_tab_book();
        login_menu();

    }
};

class book{
public:
    int year;
    string kind;
    string tittle;
    int available;
    //constructor
    book(int year, string kind,string tittle,int available):
        year(year), kind(kind), tittle(tittle), available(available){}
    ~book(){}
};

class user{
public:
    string name;
    string pass;
    user(string name, string pass):
            name(name), pass(pass){}
};

#endif _DATABASE_H_ 

plik database.cpp

#include "stdafx.h"
#include <stdlib.h>
#include <iostream>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>//convert string to int
#include <iomanip> //text align
#include <conio.h>
#include <vector>
#include <algorithm>//find in vector
#include <io.h>
#include <Windows.h> // to Sleep() function
#include "Database.h"
using namespace std;

void database::user_menu(){
    char c = '1'; // default value
    while(c != '3'){
        system("cls");
        cout << "1. View books\n";
        if(user_name == "admin"){
            cout << "2. Add a book\n";  
            cout << "3. Edit a book\n";
            cout << "4. Remove a book\n";
            cout << "5. View rented books\n";
        }else{
            cout << "2. Rent a book\n";
            cout << "3. Return a book\n";
        }   
        cout << "9. Exit\n";
        cin>>c;
        switch (c){
        case '1':{display_books(); cin>>c; break;}
        case '2':{
            if(user_name == "admin"){
                add_book();
            }else{
                rent_book();
            }break;
                 }
        case '3':{if(user_name == "admin"){
            edit_book();
            user_menu();
                 }else{
                     return_book();
                     user_menu();
                 }break;}
        case '4':{if(user_name == "admin"){
            remove_book();
            user_menu();
                 } break;}
        case '5':{if(user_name == "admin"){
            rented_book();
            user_menu();
                 } break;}
        case '9':{exit(1);}
        default:{cout << "Enter correct digit!\n";}
        }
    }
}
void database::rent_book(){
    int nr;
    string snr;
    display_books();
    cout << "Type Id of book which You wannt to rent.";
    cin >> snr;
    nr = atoi(snr.c_str());//convert string to int   c_str - Returns a pointer to an array that contains a null-terminated sequence of characters( 
    if(nr > tab_book.size() || nr < 1){
        cout << "Enter correct id!";
        Sleep(999);
    }else if(tab_book[nr-1].available == 0){
        cout << "You can not rent this book, because its unavailable!";
        Sleep(999);
    }else{
        tab_book[nr-1].available = 0;
        string f_name = "data/";
        f_name += user_name;
        f_name += ".udat";
        fstream user_f(f_name.c_str(), ios::app); 
        user_f << nr << endl;
        save_tab_book();
        cout << "\nAction succesfull";
    Sleep(999);
    }
}
void database::rented_book(){
    string uName;
    _finddata_t fileData;
    long data = _findfirst("data/*.udat", & fileData ); //
    string fName = fileData.name;
    cout << "\nList of users who rented a book\n\n";
    cout << fName.substr(0, fName.length() - 5)<<endl;      //print filename without extension
    int nextData = _findnext(data, & fileData );        // go to next file
    while (nextData != -1){
        fName = fileData.name;
        cout << fName.substr(0, fName.length() - 5)<<endl;  //print filename w/o extension
        nextData = _findnext(data, & fileData );        // go to next file
    }
    cout << "\n\nType username to see what book he rent\n";
    cin.ignore();
    getline(cin, uName);
    return_book(uName);
}
void database::return_book(string username){           
    bool rent = 0;
    bool continuing = 1;
    int nr;
    string data;
    vector<int> tab_rented;
    string f_name = "data/";
    if (username != ""){
        f_name += username;
    }else{
        f_name += user_name;
    }
    f_name += ".udat";
    fstream user_f; 
    user_f.open(f_name.c_str());
    if(user_f.good()){
        while(!user_f.eof()){ //feof Checks whether the end-of-File indicator associated with stream is set, returning a value different from zero if it is.
            getline(user_f, data);
            nr=atoi(data.c_str());
            if (nr >0){
                tab_rented.push_back(nr);
                rent = 1;
            }
        }
    } else {cout << "You did not rent a book!\n";Sleep(1999);}
    user_f.seekg(0);
    user_f.close();
    if (rent){
        if (username == ""){
            cout << "books which You rented\n";
        }else{
            cout << "books which "<<username<<" rented\n";
        }
        for(int i=0; i<tab_rented.size(); i++){
            cout << tab_rented[i] << " " << tab_book[tab_rented[i]-1].kind << " " << tab_book[tab_rented[i]-1].tittle << endl;
        }
        if(username != ""){
            char z;
            cout << "Do you want to return " << username << " book? (y/n)\n";
            cin >> z;
            if (!(z == 'y' || z == 'Y')){
                continuing = 0;
            }
        }   
        if(continuing){
            cout << "\nWhich book you want to return?\n";
            cin>>nr;

            sort(tab_rented.begin(), tab_rented.end()); //vector has to be sorted to search.
            if(binary_search(tab_rented.begin(), tab_rented.end(), nr)){//check if is in array
                tab_book[nr-1].available = 1;

                //Remove element form tab_rented
                for(int i=0; i<tab_rented.size(); i++){
                    if(tab_rented[i] == nr){
                        tab_rented.erase(tab_rented.begin() + i);
                        break;
                    }
                }
                //Remove element form file
                if (tab_rented.size() > 0){
                    ofstream user_f(f_name.c_str());
                    for(int i=0; i<tab_rented.size(); i++){
                        user_f << tab_rented[i]<<endl;
                    }
                    user_f.close();
                } else {  //remove empty file
                    remove(f_name.c_str());
                }
                save_tab_book();
                cout << "\nAction succesfull";
            }else{
                cout << "Type correct number!";
                Sleep(999);
            }
        }

    }
}
void database::login_menu(){
    char z;
    HANDLE uchwyt;
    uchwyt = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(uchwyt,12);
    cout << "Welcome to Library management system. \n\n1. Login\n2. Crate new account\n\n\n\nDesigned by Pawel Opolski\n" << flush << endl;
    cin >> z;
    while(z != '1' && z !='2'){
        cin >> z;
    }
    if(z == '1'){
        login();
    }else if(z == '2'){
        create_user();
    }
}
void database::login(){
    system("cls");
    cout << "List of users\n";
    dispaly_users();
    cout << "\nLOGIN TO ACCOUNT\n\n";

    string name,pass;
    bool login = 0;

    while(login == 0){
        cout << "Enter name\n";
        cin >> name;
        cout << "Enter pass\n";
        cin >> pass;
        for(int i=0; i<tab_user.size(); i++){
            if(name == tab_user[i].name && pass == tab_user[i].pass){
                login=1; 
                break;
            }
        }
        if(login == 1){
            cout << "Login correctly\n";
            user_name = name;
            Sleep(1999);

        }
        else{
            cout << "Cant login!\n";
            Sleep(1999);

            system("cls");
        }
    }
}
void database::add_book(){
    int year;
    string kind,tittle;
    int available;
    string date;
    cout << "Type kind\n";
    cin.ignore( 80, '\n' );
    getline(cin, kind);
    cout << "Type tittle and author\n";
    getline(cin, tittle);
    cout << "Year of the book?\n";
    cin >> date;
    year = atoi(date.c_str());
    available = 1;
    book temp(year, kind, tittle, available);
    tab_book.push_back(temp);
    save_tab_book();
}
void database::remove_book(){
    unsigned int nr;
    cout << "List of books\n";
    display_books();
    cout << "\nType id of book which you want to delete:";
    cin >> nr;
    if(nr > tab_book.size() || nr <1){
        cout << "Enter correct id!";
        Sleep(999);
    }else if(tab_book[nr-1].available == 0){
        cout << "You can not remove this book, because its rented!";
    Sleep(999);
    }else{
        tab_book.erase(tab_book.begin() + nr - 1);
        save_tab_book();
    }
}
void database::edit_book(){
    unsigned int nr;
    int year;
    string kind, tittle, date;

    cout << "List of books\n";
    display_books();
    cout << "\nType id of book which you want to edit:";
    cin >> nr;
    if(nr > tab_book.size() || nr < 1){
        cout << "Enter correct id!";
        Sleep(999);
    }else{
        cout << "\n\nType kind\n";
        cin.ignore( 80, '\n' ); // If you write ignore(80,'\n'), up to 80 characters will be thrown away until a newline character is found
        getline(cin, kind);
        cout << "Type tittle and author\n";
        getline(cin, tittle);
        cout << "year of the book?\n";
        cin>>date;
        year = atoi(date.c_str());
        tab_book[nr-1].kind = kind;
        tab_book[nr-1].tittle = tittle;
        tab_book[nr-1].year =year;
        save_tab_book();
    }
}
void database::save_tab_book(){
    ofstream file;
    try { file.open("data/books.dat") ;
            for (int i=0; i<tab_book.size(); i++){
            file << tab_book[i].year << "|" << tab_book[i].kind << "|" << tab_book[i].tittle<<"|" << tab_book[i].available;
            if(i != tab_book.size() - 1) {file<<endl;}
        }
        file.close();
        }
catch (int param) {
        cout << "Cant save database!\n";
}
}
void database::create_user(){
    bool error = 0;
    system("cls");
    cout << "CREATE ACCOUNT\n\n";
    fstream file;
    file.open( "data/users.dat", ios::app); // iso::app All output operations are performed at the end of the file
    string name,pass;
    do{
        error = 0;//reset value
        cout << "Enter name\n";
        cin >> name;
        for(int i=0; i<tab_user.size(); i++){
            if(tab_user[i].name == name){
                cout << "\nPlease select diffrent name. This login exist\n";
                error = 1;
            }
        }
    }while(error);
    cout << "Enter pass\n";
    cin >> pass;
    if(file.good())
    {
        file << endl << name << "|" << pass;
        user temp(name, pass);
        tab_user.push_back(temp); //dodajemy na koncu wektora sobie elemencik
        file.close();
        cout << "\n\nUSER CREATED!";
        Sleep(999);
        user_name = name;
    } else {
        cout << "Cant create user!\n";
    }

}
void database::read_tab_book(){
    int year;
    string data, kind, tittle;
    int available;
    int found;
    int i = 0;
    fstream file;
    try {
    file.open("data/books.dat", ios::in); // Zezwolenie na odczytywanie danych z pliku.

    {
        while(!file.eof())
        {
            getline( file, data );
            found     = data.find_first_of("|");
            kind      = data.substr(0, found);//use unused string to change it to int
            year      = atoi(kind.c_str());
            data      = data.substr(found + 1); // substr returns a newly constructed string object with its value initialized to a copy of a substring of this object.
            found     = data.find_first_of("|");
            kind      = data.substr(0, found);
            data      = data.substr(found + 1);
            found     = data.find_first_of("|");
            tittle    = data.substr(0, found);
            data      = data.substr(found + 1);
            available = atoi(data.c_str());

            book temp(year, kind, tittle, available);
            tab_book.push_back(temp);
        }
        file.close();
    }
        } 
    catch(int param){
        cout << "books data not found!\n";
    }
}
void database::read_tab_user(){ 
    string data, name, pass;
    int found;
    int i = 0;
    fstream file;
    file.open("data/users.dat", ios::in);
    if(file.good())
    {
        while(!file.eof())
        {
            getline( file, data );
            found = data.find_first_of("|"); //Searches the string for the first character that matches any of the characters specified in its arguments.
            name  = data.substr(0, found);
            pass  = data.substr(found + 1);
            user temp(name, pass);
            tab_user.push_back(temp);

        }
        file.close();
    }else{
        file.close();
        cout << "User data not found!\nPlease type admin password.\n";
        string pwd;
        cin >> pwd;
        file << "admin|" << pwd;
        cout << "Admin account created, please login\nLogin: admin\tPassword:" << pwd << endl;
        Sleep(999);
        user temp("admin",pwd);
        tab_user.push_back(temp);
        ofstream file("data/users.dat");
        file << "admin|"<<pwd;
        file.close();
    }
}

void database::dispaly_users(){
    for(int i=0; i<tab_user.size(); i++){
        cout << tab_user[i].name << endl;
    }
}
void database::display_books(){
    cout << "NR  |   KIND   |          |  Author and tittle   |           |   Printed    |     |   Availible  |\n";
    cout << "_____________________________________________________________________________________________________________\n";
    for(int i=0; i<tab_book.size(); i++){
        cout << i + 1 << left << setw(4) << "." << setw(30) << tab_book[i].kind << setw(40) << tab_book[i].tittle << setw(23) << tab_book[i].year;
        if(tab_book[i].available == 1){
            cout << "yes\n";
        }else{
            cout << "no\n";
        }
    }
} 

oraz main:

#include "stdafx.h"
#include <stdlib.h>
#include <iostream>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>//convert string to int
#include <iomanip> //text align
#include <conio.h>
#include <vector>
#include <algorithm>//find in vector
#include <io.h>
#include <Windows.h> // to Sleep() function
#include "Database.cpp"
#include "Database.h"

using namespace std;
HANDLE hOut;

int main(){
    database newdb;
    newdb.user_menu();

    return 0;
} 

Ogółem w planie mam rozdzielic ten kod na kilka plikow .cpp, aby w kazdym byla osobna funkcja, na razie chcialem spróowac w taki sposób i nie śmiga.
Ewentualnie,jeśli nie mozecie mi pomcpoc to podeslijcie jakis szablon jak to powinno mniej wiecej wygladac to dzielenie na pliki

z gory ogromne dzieki, Panie i Panowie :)

1

Co to za pomysł, żeby includować plik cpp?! Do #include dajesz tylko pliki nagłówkowe. Skoro Twój "Database.h" dołącza już masę potrzebnych bibliotek to nie powtarzaj tego przed main.

Upewnij się, że kompilujesz ten plik cpp! Widać, że używasz VS więc upewnij się, że ten plik jest dodany do projektu.

0

jak dawałem #include "database.h" to nie działało, więc spróbowałem dodać "database.cpp" i przez przypadek tutaj wrzuciłem, ale ten błąd kojarzę. co do tego czy są w projekcie to są gdyż dodawałem je przez Add new element (ctrl shift a) i na drzewie z lewej strony sa zawarte w header files oraz source files. zastanawiam sie czy dobrze to zrobilem ze wrzucilem klasy do pliku .h a wszystkie funkcje do pliku cpp. a co do tej masy bibliotek to po prostu nie usuwalem ich dla pewnosci, ale wiem, że są niepotrzebne w pliku main. - optymalizacje zostawilem na koniec, na razie chcialem zeby sie skompilowało.

kompiluje plik main, a bledy sie wyswietlaja w pliku database.cpp i .h:

bledy typu:
1>c:\users\paweu\documents\visual studio 2012\projects\project_library_final\project_library_final\database.h(45): error C2923: 'std::vector' : 'book' is not a valid template type argument for parameter '_Ty'
1> Database.cpp
1>c:\users\paweu\documents\visual studio 2012\projects\project_library_final\project_library_final\database.h(44): error C2065: 'user' : undeclared identifier
1>c:\users\paweu\documents\visual studio 2012\projects\project_library_final\project_library_final\database.h(44): error C2923: 'std::vector' : 'user' is not a valid template type argument for parameter '_Ty'
1>c:\users\paweu\documents\visual studio 2012\projects\project_library_final\project_library_final\database.h(45): error C2065: 'book' : undeclared identifier
1>c:\users\paweu\documents\visual studio 2012\projects\project_library_final\project_library_final\database.h(45): error C2923: 'std::vector' : 'book' is not a valid template type argument for parameter '_Ty'
1>c:\users\paweu\documents\visual studio 2012\projects\project_library_final\project_library_final\database.cpp(70): warning C4018: '>' : signed/unsigned mismatch
1>c:\users\paweu\documents\visual studio 2012\projects\project_library_final\project_library_final\database.cpp(73): error C2228: left of '.available' must have class/struct/union

EDIT:

Znalazłem swój błąd, w pliku .h mialem najpierw zadeklarowana klase DATABASe, ktora korzystala z klas book i user. gdy zmienilem kolejnosc - najpierw klasa user, potem book, a potem database zaczęło smigac. przepraszam za problem. prosze o niezamykanie tematu, gdyz gdyby mi się jeszcze cos sypalo to napiszę tutaj :)

1 użytkowników online, w tym zalogowanych: 0, gości: 1, botów: 0