Я работаю над заданием, по которому мне нужна помощь.Мне нужно убедиться, что я сначала сделал свой массив (чтобы убедиться, что все они правильно вытягивают), а затем передал этот массив другой функции, которая будет проходить по массиву и искать определенный custID
, который будет введен в консольпользователь.Вот мой текущий код.
Это мой одноэлементный файл
#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
}
Это исходный файл, к которому он вызывается:
#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");
}
Надеюсь, все это имеет смысл.Поэтому мне нужно проверить, действительно ли в синглтоне мой
void getCustomerList(Customer customers[], int size)
действительно выполняет массив правильно (есть 5 элементов, все из текстового файла. Затем мне также нужно поместить их в:
Customer MyDataStore::getCustomer(int custID)
Но мне нужно спросить у консоли, что такое custID
, и проверить его, если это то, что указано в тексте, затем распечатать его, если нет, то по умолчанию установить значение 0 илискажем недействительным.
Надеюсь, все это имеет смысл, спасибо за любую помощь!