Ok I am trying to write a part of a program (that will later become a class) that will read data from a file accounts.txt line by line each line in the file look simular to this
100000 6459 Matt Smith 500.00
what I currently want to do is read each line into a string array. convert that first bit into an integer then compare it to what the current highest accountNumber if the new number is larger it then set accountNumber. This is ultimately so I can find the largest account number increment it by 1 and assign that to the next new user. Later I plan on using a modified version of the code to allow for existing users to search for their account by number or first and last name. then look at the second bit of the string "6459" and use that as their pin to verify their idenity. Code follows
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
usingnamespace std;
int main()
{
int selection;
int accountNumber = 100000;
int pinNumber;
int temp;
string firstName;
string lastName;
string accountInfo[5];
float balance = 500.00;
cout <<"Are you a \n";
cout <<"1. New user or\n";
cout <<"2. Returning user\n";
cin >> selection;
switch(selection)
{
case 1:
fstream account ("accounts.txt", ios::in | ios::out);
if(account.is_open())
{
while(getline (account, accountInfo))
{
temp = atoi(accountInfo[0].c_str());
if (temp > accountNumber)
{
accountNumber = temp;
}
}
accountNumber++;
account.close();
}
else
{
cout <<"Accounts file not found game will not continue \n";
return 1;
}
account.open ("accounts.txt", ios::in | ios::out | ios::ate);
if(account.is_open())
{
cout << "Well then welcome to our Black Jack game Let's get you an acocunt set up ok? \n";
cout << "Please enter your first name \n";
cin >> firstName;
cout << "Please enter your last name \n";
cin >> lastName;
cout << "Your account number is " << accountNumber << " Please enter what you would like your pin number to be \n"; //remember to print the account number instead of #######
cin >> pinNumber;
account << accountNumber << " " << pinNumber << " " << firstName << " " << lastName << " " << balance << endl;
}
else
{
cout <<"Accounts file not found game will not continue \n";
return 1;
}
account.close();
}
}
When i compile I get
error: no matching function for call to 'getline(std::fstream&, std::string [5])'|
which calls the line of code
while(getline (account, accountInfo))
I'm assuming this error is happening because the accountInfo is a string with 5 parts in it and getline only allows for 1 but if I dont use the string array how can I separate each of those sections out for various uses?
First off thank you thats gotten me farther than I had been but im a little lost in your code (templates are not really my friend though I am trying to better understand them). I wanted to understand your code a bit better so I added a cout << number; which returns every account number as one long run on integer is there a way to perhaps chunk it in to individual groupings I know that each account is 6 digits long if that helps, what would be the best way to modify this to read in the strings that make up the users names? I was thinking about approaching this slightly differently and just put the whole file into a multidimensional vector and just work from there any suggestions?
no there are 5 pieces of data in each line that I wanted seperated account number, pin, first name, last name, and balance but since I only needed to hold one account at a time each line would replace the old one until I found what I was looking for
//add this before the getline statement
string input;
int i=0;
while(getline(account,input))
{
accountInfo[i] = input;
temp =... //whatever you want to do
//...whatever you want to do
i++; //incremene to store the next account
}
lwtan90 I've encountered a small problem each line is a separate account and I'm trying to get accountInfo to hold just that line of account (all 5 pieces of it) but the input captures the whole line as one piece of data and Im trying to use the space in between as a way of separating each bit of info. any ideas?