0

I am creating a bank terminal for an assignment. It has the ability to add clients with each client containing 5 different variables for name, address, social#, employer, and income. Those variables are then written to a file once they have been filled and the terminal is exited.

The problem I am having is when starting the terminal I need to read these values from the file, each on their separate lines and store them in their respective variables to use in the addClient() function. This is the code snippet to make things easier than submitted my entire project:

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main()
{
  using namespace std;

  std::ifstream infile2("client-info.txt");

  //Strings used for respective items from file
  string clientName, clientAddress, clientSocial, clientEmployer, clientIncome;

  //Here is where I am having the problem of reading the info from the file
  //line by line and storing it in respective variables.
  while (infile2)
  {
     getline(infile2,clientName);
     getline(infile2,clientAddress);
     getline(infile2,clientSocial);
     getline(infile2,clientEmployer);
     getline(infile2,clientIncome);

     client.addClient(clientName, clientAddress, clientSocial, clientEmployer, clientIncome);
  }
  infile2.close();
}

The file, for example is stored as such.

John Doe
123 Easy Lane
123-45-6789
USSRC
36000

The problem I am having is that I cannot figure out a solid way to get each line and store them in their respective strings. For the assignment, I will not have to be dealing with blank spaces and such. So lines 0-4 will be for one client, 5-9 for another, etc.

A push in the right direction would be greatly appreciated, thanks!

user1710357
  • 3
  • 1
  • 7

2 Answers2

3

If the addClient function accepts 5 parameters as you have did, then your current main function has already solved your questions.

If you would like to put those 5 strings into a single string, then work this single string inside addClient function.

You can create a class:

class ClientInfo
{
 private:
   string clientName;
   string clientAddress; 
   string clientSocial;
   string clientEmployer,;
   string clientIncome;
public:
  ClientInfo(string name, string addr, string ssn, 
                 string employer, string income):
                  clientName(name), clientAddress(addr), clientSocial(ssn),
                  clientEmployer(employer), clientIncome(income)
  {
  }
};

Then inside your main, you can do the following:

ClientInfo currentClient(clientName, clientAddress, 
                clientSocial, clientEmployer, clientIncome);
client.addClient(currentClient);
taocp
  • 23,276
  • 10
  • 49
  • 62
0

I think the only problem you're having is when you call getline, you're not passing in a parameter. In this case I think you need to use the newline delimeter.

  while (infile2)

      {
         getline(infile2,clientName, '\n');
         getline(infile2,clientAddress, '\n');
         getline(infile2,clientSocial, '\n');
         getline(infile2,clientEmployer, '\n');
         getline(infile2,clientIncome, '\n');

         client.addClient(clientName, clientAddress, clientSocial, clientEmployer, clientIncome);
      }

I'm not sure on the '\n' syntax but this will read the file until it hits a newline and then move on to the next line.

Jesse Moreland
  • 441
  • 6
  • 17
  • by default, it is the \n character, see: http://www.cplusplus.com/reference/string/string/getline/ – taocp Apr 23 '13 at 01:45