I am working on an assignment that I am needing assistance on. I need to verify I did my array first (to make sure they are all pulling correctly) and then pass that array to another function that will loop through the array and look for a certain custID
that will be entered into the console by the user. Here is my current code.
This is my singleton file
#include "MyDataStore.h"
#include <fstream>
MyDataStore * MyDataStore::iInstance;
//Declare private customer array..
Customer customers[5];
void getCustomerList(Customer[], int);
MyDataStore::MyDataStore()
{
getCustomerList(customers, 5);
}
MyDataStore * MyDataStore::GetInstance()
{
if (iInstance == NULL())
{
iInstance = new MyDataStore();
}
return iInstance;
}
void MyDataStore::getBooks(Book books[], int size)
{
ifstream input;
input.open("Books.txt");
string ISBN, title, author;
double price = 0.0;
int i = 0;
while (input >> title >> author >> ISBN >> price)
{
Book b = Book(ISBN, title, author, price);
if (i < size)
{
books[i] = b;
i++;
}
}
}
Customer MyDataStore::getCustomer(int custID) // need help here
{
//for (int i = -0; i < 5; i++) {
for (Customer c: customers)
{
if (custID = c.getCustID())
{
//custID = c.getCustID();
return c;
}
}
//}
//for range
//for each customer c in customers
//if custID = c.getCustID()
//return c
//
}
void getCustomerList(Customer customers[], int size)
{
//need help here
ifstream custfile;
custfile.open("Customer.txt");
int custID;
string firstName, lastName, city, street, state, zip;
Address address;
int i = 0;
while (custfile >> custID >> firstName >> lastName >> city >> street >> state >> zip)
{
Customer c = Customer(custID, firstName, lastName, address);
if (i < size)
{
customers[i] = c;
i++;
}
//Need to make the address part work
}
//load customer array
}
This is the source file it is being called to:
#include <iostream>
#include <string>
#include "Address.h"
#include "Book.h"
#include "Customer.h"
#include "MyDataStore.h"
#include <fstream>
using namespace std;
//Main function
void main()
{
MyDataStore * obj1 = MyDataStore::GetInstance();
MyDataStore * obj2 = MyDataStore::GetInstance();
//declare a book array --> this works great!
Book books[6];
//send array to get books
MyDataStore::getBooks(books, 6);
//loop through the filled in book array and just show the author
for (Book b: books)
{
cout << b.getTitle() << endl;
cout << b.getAuthor() << endl;
cout << b.getPrice() << endl;
}
//This will only print out the first line.
Customer c = MyDataStore::getCustomer(2345);
cout << c.print();
system("pause");
}
I hope this all makes sense. So I need to check to see if in the singleton if my
void getCustomerList(Customer customers[], int size)
is actually doing the array correctly (there are 5 items, all coming from the text file. I also need to then put then in the:
Customer MyDataStore::getCustomer(int custID)
But, I need to ask the console what the custID
is and validate it, if it is what is listed in the text, then print it, if not then default it to 0 or say invalid.
I hope this all makes sense, thank you for any help!