Lasha Zakariashvili
Lasha Zakariashvili

Reputation: 43

Bank System not working

For some reason my bank script isn't working. More specifically, the search() does not work. I kind of understand why it doesn't, probably because of if(obj.returnId() == n), but I have no clue how to fix it. When I search an account, it will only allow me to find the last account made, not any of the previous ones. Here is my code:

#include "stdafx.h"
#include <iostream>
#include <string>
#include <string.h>
#include <fstream>
#include <Windows.h>
#include <conio.h>
using namespace std;
bool loop = true;

class account
{
          int id;
         char name[40];
         char password[40];
public:
          void getData()
          {
                     cout << "\nEnter your name: ";
                     cin >> name;
                     cout << "\nEnter ID: ";
                     cin >> id;
                     cout << "\Enter pass: ";
                     cin >> password;
          }
          void showData()
          {
                     cout << "\nName: ";
                     puts(name);
                     cout << "\nID: " << id;
                     cout << "\n";
          }
          int returnId()
          {
              return id;
          }
};

void createAccount()
{
    account obj;
    ofstream fileCreate;
    fileCreate.open("accounts.dat", ios::binary|ios::app);
    obj.getData();
    fileCreate.write((char*)&obj,sizeof(obj));
    fileCreate.close();
}

void display()
{
    account obj;
    ifstream fileRead;
    fileRead.open("accounts.dat", ios::binary);
    while(fileRead.read((char*)&obj, sizeof(obj)))
    {
        obj.showData();
    }
    fileRead.close();
}

void search(int n)
{
    account obj;
    ifstream fileRead;
    fileRead.open("accounts.dat", ios::binary);
    while(fileRead.read((char *) &obj, sizeof(obj)) );
    {
        fileRead.seekg(0,ios::beg);
        if(obj.returnId() == n)
        {
            obj.showData();
        }
        else {
            cout << "\nUser not foud!\n";
        }
    }
    fileRead.close();
}








void main()
{
    cout << "Welcome to the Bank.\n\n";

    while (loop==true)
    {
        char choice[10];
        cout << "Please select an option:\n";
        cout << "------------------------------------------------\n";
        cout << "(a)Log into an account\n(b)Create an account\n(s)Search an account\n(e)Exit\n";
        cout << "------------------------------------------------\n";
        cout << "Choice: ";
        cin >> choice;
        choice[0] = tolower(choice[0]);
        cout << "\n------------------------------------------------\n\n";

        switch (choice[0])
        {
        case 'a':
            display();
            break;
        case 's':
            int n;
            cout << "Enter the ID of the account: ";
            cin >> n;
            search(n);
            break;
        case 'b':
            createAccount();
            break;
        case 'e':
            loop = false;
            break;
        default:
            system("CLS");
            cout << "The option \"" << choice[0] <<  "\" is invalid.\n\n\n\n";
            break;
        }

    };
    cout << "\n\n\n";
    cout << "Click anything to exit.";
    getch();
}

Upvotes: 0

Views: 195

Answers (4)

john
john

Reputation: 88007

The other error is that you should only say 'User not found' when you've tested all the accounts and they all failed. Your loop (when you've removed the semi-colon) is saying 'User not found' after every failed test.

Upvotes: 1

Keith Randall
Keith Randall

Reputation: 23265

Your problem is the semicolon at the end of this line:

while(fileRead.read((char *) &obj, sizeof(obj)) );

That makes this loop have an empty body. So you basically read the whole file and throw away the results, except for the last entry.

get rid of this also:

fileRead.seekg(0,ios::beg);

I don't know why you need that, it would only make you read the first entry over and over.

Upvotes: 2

Rudolf M&#252;hlbauer
Rudolf M&#252;hlbauer

Reputation: 2531

the seek might be the problem:

while(fileRead.read((char *) &obj, sizeof(obj)) ) //;
{
    // seek to start?
    //fileRead.seekg(0,ios::beg);
    ...
}

have a look at http://www.boost.org/doc/libs/1_51_0/libs/serialization/doc/index.html

aside, use

cout << "text" << endl;

for platform-agnostic newlines.

Upvotes: 0

Some programmer dude
Some programmer dude

Reputation: 409422

You probably don't find the entry you are looking for because each time you have read an entry from the file, you reset the position to the beginning. This means that your loop will run forever, reading the same entry over and over again and never finding the entry you search for.

Upvotes: 0

Related Questions