inFile

I am stuck on this program. Just requesting some guidance...a point in the right direction or some useful knowledge. I am trying to create a program that reads from a file...it contains 5 bank customer records with each line starting with the line number (starting with 1000) and followed by a first name, a last name, and a balance. The program is supposed to read from the file and list the info in four separte arrays. The starting address of any element in the account array can then be calculated as the address of the first record in the array + (account number - 1000) * sizeof(int). Usin this informatio, the program should request a user entered account number and display the corresponding name and account balance.

I think i'm close to getting the first part, but the second part...I don't know where to start.


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

int main()
{
string filename = "Bank Record.txt";
const int MAXRECORDS = 5;
string acctNum;
string name;
string lastName;
string balance;
ifstream inFile;
int i;



if(inFile.fail())
{
cout << "\nFailed to pen the input file." << endl;
cout << "Make sure the file currently exists" << endl;
exit(1);
}

inFile >> setiosflags (ios::fixed)
>> setiosflags (ios::showpoint)
>> setprecision(2);

inFile >> acctNum >> name >> lastName >> balance;
while(inFile.good())
{

cout << acctNum << " " << name << " " << lastName << " " << balance<< " " << endl;
}

return 0;
}
my comments to start off with:
1. Where is the array, and what will it be an array of? (strings possibly?).
2. You need to open the file at least for reading.
3. You wont need to do any calculation like you have described to get the data out of the array. That calculation looks wrong anyway.
I feel as if I am getting close....can anyone see why I'm getting this error:

error C2784: 'bool std::operator <(const std::vector<_Ty,_Alloc> &,const std::vector<_Ty,_Alloc> &)' : could not deduce template argument for 'const std::vector<_Ty,_Ax> &' from 'std::string'
c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\vector(915) : see declaration of 'std::operator`<''

Here is my Program:

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

int main()
{
string filename = "Bank Record.txt";
const int MAXRECORDS = 5;
int acctNum[MAXRECORDS];
int name[MAXRECORDS];
int lastName[MAXRECORDS];
int balance[MAXRECORDS];
string account[1];
ifstream inFile;
int i;

inFile.open(filename.c_str());

if(inFile.fail())
{
cout << "\nFailed to pen the input file." << endl;
cout << "Make sure the file currently exists" << endl;
exit(1);
}

inFile >> setiosflags (ios::fixed)
>> setiosflags (ios::showpoint)
>> setprecision(2);

for(i=0; i < MAXRECORDS; i++)
{
cin >> acctNum[i];
cin >> name[i];
cin >> lastName[i];
cin >> balance[i];
}

inFile.close();

cout << "Please enter the account number you are searching for: ";
cin >> account[i];

if(account[i] < 1000)
{
cout << "The account number you entered is invalid." << endl;
exit(2);
}
else
{
account -= 1000;

cout << "\nThe name on the account is: " << name[account] << lastName[account] << "." << endl;

cout << "\nThe current balance is $ ";
cout << setiosflags (ios::fixed) << setiosflags (ios::showpoint) << setprecision(2);
cout << balance[account] << endl;
}

return 0;
}

It looks to me like the error is related to declaring a vector, but I do not see that anywhere in your code; do you have any other code?
Topic archived. No new replies allowed.