Al slam A'lykom ..
its kinda freaky to me .. cuz its my first year in c++ ( in fact its few months not even a year ) and i have a project , and its to create an ATM program that asks the user if he is new or existing user , so i need a something to save the information in .. and recall it again to use it when the user enters the account number and the password .
so i used the fstream and sstream to create text files multiple text files but there is a few problems :D
1- i want every time the program runs , i want it to create a new file contains the user information not to create the same file and rewrite the information every time
2- i need to recall that file to check the account number and the password .
so is there a kinda way to create database and use it for this program and how ?

sorry for prolong .. and hope you help me :)

Your question is quite vague to me. Also the assignment isn't fun enough for me to go try it and you don't provide any code whatsoever to fix. You could simply use a textfile as a database which stores the account number and password per line. Then at startup you could read this file into memory, or lookup the account number in the file everytime it's requested.

thanks for your reply ^^ ..

this is my code here

#include <iostream> 
#include <string>
#include <fstream>
#include <sstream>
using namespace std;

void starline ()
{
    for (int i=0 ; i<=15 ; i++)
    {
        cout<<"-";
    }
    cout<<endl;
}
void main ()
{
    char quest;
    string name  , mail , address, pass;
    int mobile , phone  ;
    cout<<"Are you a new client ? (y/n)"<<endl;
    cin>>quest;
    starline();



    if (quest=='y'|| quest=='Y') 
    {
        for ( int i =0 ; i<1 ; i++)
    {
        string mem("mem_");
        stringstream ss;
        ss<<mem<<i<<".txt";
        cout<<ss.str()<<endl;
        ofstream outfile(ss.str());

        //new user information
        cout<<"Please Enter your data : "<<endl; 
        starline();

        cout<<"Name : ";
        getline(cin,name);
        getline(cin,name);

        outfile<<name<<endl;

        cout<<"Address : ";
        getline(cin,address);

        outfile<<address<<endl;

        cout<<"Telephone number : ";
        cin>>phone;

        outfile<<phone<<endl;

        cout<<"Mobile number : ";
        cin>>mobile;

        outfile<<mobile<<endl;

        cout<<"E-mail address : ";
        getline(cin,mail);
        getline(cin,mail);
        outfile<<mail<<endl;

        int accnum = (mobile+phone)/50;

        outfile<<accnum<<endl;

        cout<<"Thanks for registering in our bank ! "<<endl;
        cout<<"Your account number is : "<<accnum<<endl; 
        cout<<"now please enter your password :";
        cin>>pass;
        outfile<<pass<<endl;
        starline();
        cout<<"Information succesfuly saved"<<endl;
        }
    }

    else if (quest=='n'||quest=='N')
    {
        cout<<"TOZ FEEK !! :P :P :P"<<endl;?? still didn't touched
    }

}

so i need to know how to every time start the prog create a new text file ( create it in differnet name ) n store the data in it ..
and how to check the data when the user enters the account number and the password

Are you sure you want a different file every time the program is run? It seems to me you actually want the same textfile everytime, otherwise you wouldn't need textfiles in the first place. (You could just store things in memory as you don't have to "remember" it in between executions anyway)

There's also some oddities in the code. For example:

for ( int i =0 ; i<1 ; i++)

I won't be going over these I guess, but it's probably good to look over it closely at some point. I assume you did the above loop to somehow determine the number of the file you want to create at some point.

First thing I'd do is to create a structure (or class, but I assume you don't want classes for this) for information that is stored on a per client basis. I'll be using the datatypes you used, but this is something that could be improved upon.

struct ClientInfo
{
    string name;
    string mail;
    string address;
    string password;
    int mobile;
    int phone;
};

Something you could do next is dealing with the file I/O. To keep things simply it's probably best to start with reading the contents of the file at startup, and then writing the updated contents back to the file when closing. Depending on your demands exactly you might not ever end in a proper fashion though. So instead I guess something that's sort of easy would be to read the file at the start, and after that update both the representation in memory and the file so you don't have to deal with simultaneous in- and output to/from the same file.

Given the above you'll need a construct to store client information. This could be a vector.

Making a quick general approach results in something like this:

#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;

const string DATABASE_FILENAME = "database.dat";

struct ClientInfo
{
    int accountNumber;
    string name;
    string mail;
    string address;
    string password;
    int mobile;
    int phone;
};

void CloseFile (ofstream& outputFile);
void HandleExistingClient(vector<ClientInfo>& database);
void SaveDatabase(const string filename, const vector<ClientInfo> database);
bool OpenDatabaseForWriting (const string filename, vector<ClientInfo>& database, ofstream& outputStream);
bool LoadDatabase(const string filename, vector<ClientInfo>& database);
ostream& operator<< (ostream& stream, const ClientInfo clientInfo);
void ProcessClients(vector<ClientInfo>& database, ofstream& databaseStream);
void RegisterNewClient (vector<ClientInfo>& database, ofstream& databaseStream);
ClientInfo ObtainClientInfo(const vector<ClientInfo>& database);
int GenerateAccountNumber(const int mobile, const int phone);
void starline();

void starline()
{
    for (int i=0 ; i <= 15 ; i++)
    {
        cout<<"-";
    }
    cout << endl;
}

int GenerateAccountNumber(const int mobile, const int phone)
{
    return ((mobile + phone) / 50);
}

ClientInfo ObtainClientInfo(const vector<ClientInfo>& database)
{
    ClientInfo newClient;

    cout << "Please Enter your data : " << endl;
    starline();

    cout << "Name : ";
    getline(cin, newClient.name);

    cout<<"Address : ";
    getline(cin, newClient.address);

    cout << "Telephone number : ";
    cin >> newClient.phone;

    cout << "Mobile number : ";
    cin >> newClient.mobile;

    cout<<"E-mail address : ";
    getline(cin, newClient.mail);

    // Note: will probably add a check for duplicates here. Potentially for other data as well.
    //       It's why I added the database as a parameter. But i'll leave this up to you..
    newClient.accountNumber = GenerateAccountNumber(newClient.mobile, newClient.phone);

    cout << "Thanks for registering in our bank ! " <<endl;
    cout << "Your account number is : " << newClient.accountNumber <<endl;
    cout << "now please enter your password :";
    cin >> newClient.password;
    starline();

    return newClient;
}

void RegisterNewClient (vector<ClientInfo>& database, ofstream& databaseStream)
{
    ClientInfo newClient = ObtainClientInfo(database);

    // Add the client to the database in memory.
    database.push_back(newClient);

    // Add the client information to the database file.
    databaseStream << newClient;

    cout<<"Information succesfuly saved"<<endl;
}

void ProcessClients(vector<ClientInfo>& database, ofstream& databaseStream)
{
    string quest;

    do
    {
        cout << "Are you a new client ? (y/n)" << endl;
        getline(cin, quest);
        starline();

        if (quest[0] == 'y'|| quest[0] =='Y')
        {
            RegisterNewClient(database, databaseStream);
        }
        else
        {
            HandleExistingClient(database);
        }
    }
    // For now loop endlessly, but you'll probably want a stop condition at some point.
    while (true);
}

ostream& operator<< (ostream& stream, const ClientInfo clientInfo)
{
    // Implement this.

    // For example, if you want to put "hello world" into the file you can do:
    // stream << "hello world";

    return stream;
}

bool LoadDatabase(const string filename, vector<ClientInfo>& database)
{
    // Implement this.

    // Open the file for reading.
    // Load every item into "database".
    // Close the file.

    // Return true if everything went fine, false otherwise.
}

bool OpenDatabaseForWriting (const string filename, vector<ClientInfo>& database, ofstream& outputStream)
{
    // Implement this.

    // Open the file for writing with outputStream. Set the write pointer to the end of the file. Do not truncate the file.

    // Return true if no error occurred, false otherwise.
}

void SaveDatabase(const string filename, const vector<ClientInfo> database)
{
    // Implement this.

    // Open the file
}

void HandleExistingClient(vector<ClientInfo>& database)
{
    // Implement this.
}

void CloseFile (ofstream& outputFile)
{
    // Implement this.

    // Close the stream. (could make it work with both in- and output streams by using fstreams)
}

int main()
{
    // Our databases containing the information of clients.
    vector<ClientInfo> database;
    ofstream outputStream;

    // Attempt to load the database from file.
    cout << "Loading database file \"" << DATABASE_FILENAME << "\".. ";

    if (LoadDatabase(DATABASE_FILENAME, database))
    {
        cout << "success!\n";
        cout << "Opening databases file \"" << DATABASE_FILENAME << "\" for writing..";

        if (OpenDatabaseForWriting(DATABASE_FILENAME, database, outputStream))
        {
            cout << "success!\n";

            // Will happen "endlessly".
            ProcessClients(database, outputStream);

            cout << "Done processing clients. Closing database file \"" << DATABASE_FILENAME << "\".\n";
            CloseFile(outputStream);
        }
        else
        {
            cout << "failed!\n";
        }
    }
    else
    {
        cout << "failed!\n";
    }
}

It can be improved ofcourse, but as a general outline I think this is a good thing to start with. More importantly, I tried to keep it simple as I don't really know your programming experience. (as you mentioned "a few months" I assumed it's very little. The operator overloading might confuse you, but I hope it's detailed enough in the body on how to use it)

well thanks for your reply ^^ .. and im really sorry for wasting your time ..
but to be honest im really confused .. there is alot in the code that i can't understand maybe because of my c++ little experience .. so that i have alot of questions
is there is a simple way to do that code .. and if not can you give me a reference to understand this code and to help me more in that project ..

thanks again , i really appreciate it ^^

Edited 3 Years Ago by omarking05

This article has been dead for over six months. Start a new discussion instead.