Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8747005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:17:10+00:00 2026-06-13T12:17:10+00:00

For some reason my bank script isn’t working. More specifically, the search() does not

  • 0

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();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T12:17:10+00:00Added an answer on June 13, 2026 at 12:17 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason this isn't working, am I missing something obvious? RewriteRule ^(.*)infopopup.html$ /acatalog/infopopup.html
For some reason this isn't working for me. It gives me the vector iterator
For some reason, my script isn't writing out the text after I remove the
For some reason my simple jquery bind event isn't working. The funny thing is
for some reason my event data isn't working correctly with jquery fullcalendar when I
For some reason, Section 1 works but Section 2 does not. When run in
For some reason any php snippet does not render in my browser. I know
For some reason this is not working. Instead it thinks it should check the
For some reason, my spec isn't passing. It appears that @categories isn't getting to
For some reason beyond me my html page is not finding my stylesheet. I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.