Question

/* BEGIN - DO NOT EDIT CODE */ // File: main.cpp #include "LoginAccount.h" #include "RegisteredLoginAccount.h" #include...

/* BEGIN - DO NOT EDIT CODE */

// File: main.cpp

#include "LoginAccount.h"
#include "RegisteredLoginAccount.h"
#include "GuestLoginAccount.h"
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>

using namespace std;

//prototypes and constants
const int ARRAY_SIZE = 20;

using number_of_records_t = int;

//Function Declarations.
void readAccountsFromFile(ifstream& fin, RegisteredLoginAccount registeredAccounts[], number_of_records_t& registeredLength,
GuestLoginAccount guestAccounts[], number_of_records_t& guestLength);
bool openFileForInput(ifstream& ifs, const string& filename);
bool openFileForOutput(ofstream& ofs, const string& filename);
void writeAccountsToFile(ofstream& ofs, RegisteredLoginAccount registeredAccounts[], const number_of_records_t registeredLength,
GuestLoginAccount guestAccounts[], const number_of_records_t guestLength);
void writeAccountToFile(ofstream& ofs, const LoginAccount& account);

int main() {
RegisteredLoginAccount registeredAccounts[ARRAY_SIZE];
GuestLoginAccount guestAccounts[ARRAY_SIZE];
number_of_records_t registeredLength;
number_of_records_t guestLength;
ifstream ifs;
ofstream ofs;

  
/* Open the input file for reading.
* Give an error message and exit if connection fails.
*/
if ( !openFileForInput(ifs, "loginAccounts.txt") ) {
cerr << "Error reading file." << endl;
exit(1);
}
  
/* Declare the integer variable, numberOfRecords to hold the actual number
* of records read in.
* Assign to it the value returned from the loadArrayFromFile() function.
*/
readAccountsFromFile(ifs, registeredAccounts, registeredLength, guestAccounts, guestLength);
  
  
/* Open the output file for writing.
* Give an error message and exit if connection fails.
*/
if ( !openFileForOutput(ofs, "accountsOutput.txt") ) {
cerr << "Error writing file." << endl;
exit(1);
}
  
writeAccountsToFile(ofs, registeredAccounts, registeredLength, guestAccounts, guestLength);
cout << "File created successfully." << endl;

return 0;
}// end main()

/* END - DO NOT EDIT CODE */

bool openFileForInput(ifstream& ifs, const string& filename) {
/*
* TODO (1):
*
* Attempt to open the stream and connect it to the filename given.
* Return the result of the open attempt.
*/
ifs.open(filename, ios::in);

if( !ifs.is_open() ) {
cerr << "Error opening file." << endl;
return false;
}
return true;
  
  
  
}// end openFileForInput()


bool openFileForOutput(ofstream& ofs, const string& filename) {
/*
* TODO (2):
*
* Attempt to open the stream and connect it to the filename given.
* Return the result of the open attempt.
*/
ofs.open(filename, ios::out);

if( !ofs.is_open() ){
cerr << "Error opening file." << endl;
return false;
}
return true;
  
  
  
}// end openFileForOutput()


void readAccountsFromFile(ifstream& ifs, RegisteredLoginAccount registeredAccounts[], number_of_records_t& registeredLength,
GuestLoginAccount guestAccounts[], number_of_records_t& guestLength) {
/*
* TODO (3):
*
* Read in the account records from the given stream.
*
* Records in the file are formatted as:
* <accountType>,<first>,<last>,<username>,<password | expirationInSeconds><newline>
* ...
* <accountType> is either 'r' or 'g'.
*
* Use these values to create 'Registered' or 'Guest' objects, which you are to store
* in their respective arrays. Remember to use the build in function
* int stoi(string s)
* to convert a string to an integer.
*
* Make sure you close the file stream.
*/
  
  
  
  
}// end readAccountsFromFile()


void writeAccountsToFile(ofstream& ofs, RegisteredLoginAccount registeredAccounts[], const number_of_records_t registeredLength,
GuestLoginAccount guestAccounts[], const number_of_records_t guestLength) {

/* TODO (4):
*
* Write each type of account record to the provided output stream. First
* write all the 'registered' users, then all the 'guest' users.
*
* Call the function writeAccountToFile() to do so.
*
* Make sure you close the file stream.
*/
  
  
  
  
  
}// end writeAccountsToFile()


void writeAccountToFile(ofstream& ofs, const LoginAccount& account) {
/* Each account received is of a subclass. Since the parameter is a
* LoginAccount& type, and the toString() method is virtual, then
* the actual object's (RegisteredLoginAccount or GuestLoginAccount)
* toString() method is used.
*/
ofs << account << endl;
}// end writeAccountToFile()

This is all of main. TODO #3 is where I am currently struggling. Registered and Guest accounts inherit fields from LoginAccount

string first;
string last;
string username;

string accountType;

RegisteredAccount gets the field string password and GuestLoginAccount gets the field int expirationInSeconds. Id add the header files but I'm out of space. If this is sufficient information for any help with TODO #3 I'd appreciate it. Thanks.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Dear Student,

Since the program was not complete, I have created by own structures for RegisteredLoginAccount and GuestLoginAccount to inherit the structure LoginAccount. You need to replace loginAccount in these structures and in function readAccountsFromFile with your existing variable. The structures defined as below:

struct RegisteredLoginAccount{
   LoginAccount loginAccount;
   string password;
};

struct GuestLoginAccount{
   LoginAccount loginAccount;
   int expirationInSeconds;
};

The LoginAccount remains same as you indicated like:

struct LoginAccount{
   string first;
   string last;
   string username;
   string accountType;
};

As you requested for the solution for only TODO #3, below is the code for function readAccountsFromFile to read details from input file.

void readAccountsFromFile(ifstream& ifs, RegisteredLoginAccount registeredAccounts[], number_of_records_t& registeredLength, GuestLoginAccount guestAccounts[], number_of_records_t& guestLength)
{
   string accountType,           // Holds the Account Type
       expirationInSeconds;   // Holds the expiration seconds

   // Reset count of registered and guest users
   registeredLength = guestLength = 0;

   /*
       Loop over till end of file
       Use getline function along with delimiter which is ","
   */
   while (!ifs.eof())
   {
       // Get the account type
       getline(ifs, accountType, ',');

      
       if (accountType == "r")
       {
           // Store details in registeredAccounts for accountType = r
           registeredAccounts[registeredLength].loginAccount.accountType = accountType;
           getline(ifs, registeredAccounts[registeredLength].loginAccount.first, ',');
           getline(ifs, registeredAccounts[registeredLength].loginAccount.last, ',');
           getline(ifs, registeredAccounts[registeredLength].loginAccount.username, ',');
           getline(ifs, registeredAccounts[registeredLength].password);
           registeredLength++;       // Increment the count of registered users
       }
       else if (accountType == "g")
       {
           // Store details in guestAccounts for accountType = g
           guestAccounts[guestLength].loginAccount.accountType = accountType;
           getline(ifs, guestAccounts[guestLength].loginAccount.first, ',');
           getline(ifs, guestAccounts[guestLength].loginAccount.last, ',');
           getline(ifs, guestAccounts[guestLength].loginAccount.username, ',');
           getline(ifs, expirationInSeconds);
           guestAccounts[guestLength].expirationInSeconds = stoi(expirationInSeconds);
           guestLength++;       // Increment the count of guest users
       }
   }

   // Close the file
   ifs.close();
}

For your reference, the output in console looks like below after reading from input file:

Add a comment
Know the answer?
Add Answer to:
/* BEGIN - DO NOT EDIT CODE */ // File: main.cpp #include "LoginAccount.h" #include "RegisteredLoginAccount.h" #include...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Can someone help me put the string removee the return to an outfile and fix?? prompt: CS 575 LabEx14: no e’s Let the user specify an input file and an output file (text files). Read the input file and...

    Can someone help me put the string removee the return to an outfile and fix?? prompt: CS 575 LabEx14: no e’s Let the user specify an input file and an output file (text files). Read the input file and write the output file; the output file should be the same as the input file, except that it has no e’s. For example, if the input file had the line. Streets meet in the deep. Then the output file would have...

  • In c++ please How do I get my printVector function to actually print the vector out?...

    In c++ please How do I get my printVector function to actually print the vector out? I was able to fill the vector with the text file of names, but I can't get it to print it out. Please help #include <iostream> #include <string> #include <fstream> #include <algorithm> #include <vector> using namespace std; void openifile(string filename, ifstream &ifile){    ifile.open(filename.c_str(), ios::in);    if (!ifile){ cerr<<"Error opening input file " << filename << "... Exiting Program."<<endl; exit(1); }    } void...

  • C++ problem. hi heys, i am trying to remove beginning and the end whitespace in one...

    C++ problem. hi heys, i am trying to remove beginning and the end whitespace in one file, and output the result to another file. But i am confused about how to remove whitespace. i need to make a function to do it. It should use string, anyone can help me ? here is my code. #include <iostream> #include <iomanip> #include <cstdlib> #include <fstream> using namespace std; void IsFileName(string filename); int main(int argc, char const *argv[]) { string inputfileName = argv[1];...

  • // File: main.cpp #include <iostream> #include <fstream> #include <iomanip> usi...

    // File: main.cpp #include <iostream> #include <fstream> #include <iomanip> using namespace std; int recursiveCount = 0; void inorder(const int list[], const int n, const int index); int leftIndex(const int index); int rightIndex( const int index); int main() { int list[] = {1,2,3,4,5,6,7,8,9,10}; inorder(list, 10, 0); cout << endl; return 0; }// end main() void inorder(const int list[], const int n, const int index) { /* START - NO EDIT */ recursiveCount += 1; /* END - NO EDIT */ /*...

  • #include <fstream> #include <iostream> #include <cstdlib> using namespace std; // Place charcnt prototype (declaration) here int...

    #include <fstream> #include <iostream> #include <cstdlib> using namespace std; // Place charcnt prototype (declaration) here int charcnt(string filename, char ch); int main() { string filename; char ch; int chant = 0; cout << "Enter the name of the input file: "; cin >> filename; cout << endl; cout << "Enter a character: "; cin.ignore(); // ignores newline left in stream after previous input statement cin.get(ch); cout << endl; chcnt = charcnt(filename, ch); cout << "# of " «< ch« "'S:...

  • The 4th deliverable is to create the program the makes the buy recommendation. It uses the...

    The 4th deliverable is to create the program the makes the buy recommendation. It uses the class Stock to store and retrieve the stock information. I need to make this program compatible with my stocks class. Program I need to change: #include <iostream> #include <cmath> #include <fstream> #include <iomanip> #include <string> #include <stdlib.h> using namespace std; // read the data file, store in arrays int openDatafile(ifstream&,string [], float[], float[], int[]); // opens the data file void readData(ifstream &, string[], float[],...

  • #include <iostream> #include <fstream> using namespace std; //constants const int CAP = 100; //function prototypes bool...

    #include <iostream> #include <fstream> using namespace std; //constants const int CAP = 100; //function prototypes bool openFile(ifstream &); void readData(ifstream &, int [], int &); void printData(const int [], int); void sum(const int[], int); void removeItem(int[], int &, int); int main() { ifstream inFile; int list[CAP], size = 0; if (!openFile(inFile)) { cout << "Program terminating!! File not found!" << endl; return -1; } //read the data from the file readData(inFile, list, size); inFile.close(); cout << "Data in file:" <<...

  • I need help debugging this C++ prgram. What Am i doing wrong? //******************************************************** // This program...

    I need help debugging this C++ prgram. What Am i doing wrong? //******************************************************** // This program reads two input files whose lines are //ordered by a key data field. This program should merge //these two files, writing an output file that contains //all lines from both files ordered by the same key field. // //********************************************************* #include <iostream> #include<string> #include<fstream> //prototype void mergeTwoFiles (ifstream&,ifstream&, ofstream&); using namespace std; int main() {string inFile1,inFile2,outFile; // input and output files ifstream in1; ifstream in2;...

  • employee.h ---------- #include <stdio.h> #include <iostream> #include <fstream> class Employee { private: int employeeNum; std::string name;...

    employee.h ---------- #include <stdio.h> #include <iostream> #include <fstream> class Employee { private: int employeeNum; std::string name; std::string address; std::string phoneNum; double hrWage, hrWorked; public: Employee(int en, std::string n, std::string a, std::string pn, double hw, double hwo); std::string getName(); void setName(std::string n); int getENum(); std::string getAdd(); void setAdd(std::string a); std::string getPhone(); void setPhone(std::string p); double getWage(); void setWage(double w); double getHours(); void setHours(double h); double calcPay(double a, double b); static Employee read(std::ifstream& in); void write(std::ofstream& out); }; employee.cpp ---------- //employee.cpp #include...

  • Hi, I want to creat a file by the name osa_list that i can type 3...

    Hi, I want to creat a file by the name osa_list that i can type 3 things and store there ( first name, last name, email). then i want to know how to recal them by modifying the code below. the names and contact information i want to store are 200. #include <iostream> #include <fstream> #include <cstdlib> #include <iomanip> using namespace std; class Student { private: string fname; string lname; string email;       public: Student(); Student(string fname, string lname,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT