银行系统不工作

时间:2012-10-27 14:42:58

标签: c++

由于某些原因,我的银行脚本不起作用。更具体地说,search()不起作用。我有点理解为什么它没有,可能是因为if(obj.returnId() == n),但我不知道如何修复它。当我搜索一个帐户时,它只允许我找到最后一个帐户,而不是之前的帐户。这是我的代码:

#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();
}

4 个答案:

答案 0 :(得分:2)

你的问题是这一行末尾的分号:

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

这使得这个循环有一个空体。所以你基本上读取整个文件并丢弃结果,除了最后一个条目。

也摆脱了这个:

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

我不知道为什么你需要它,它只会让你一遍又一遍地阅读第一个条目。

答案 1 :(得分:1)

另一个错误是您只应该说'找不到用户&#39;当您测试了所有帐户时,它们都失败了。你的循环(当你删除了分号时)说'用户未找到&#39;在每次测试失败后。

答案 2 :(得分:0)

您可能找不到要查找的条目,因为每次从文件中读取条目时,都会将位置重置为开头。这意味着您的循环将永远运行,一遍又一遍地读取相同的条目,并且永远不会找到您搜索的条目。

答案 3 :(得分:0)

寻求可能是问题所在:

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

查看http://www.boost.org/doc/libs/1_51_0/libs/serialization/doc/index.html

除此之外,请使用

cout << "text" << endl;

用于与平台无关的换行符。

相关问题