通过函数传递数组并提取某些对象

时间:2018-11-27 04:58:06

标签: c++ arrays object for-loop

我正在处理需要协助的任务。我需要验证自己是否先完成了阵列操作(以确保它们都正确拉出),然后将该阵列传递给另一个函数,该函数将循环遍历该阵列并查找要输入到控制台中的某个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或说无效。

我希望一切都有意义,谢谢您的帮助!

1 个答案:

答案 0 :(得分:0)

弄清楚,其余代码是正确的,这是我必须添加才能使其正常工作的原因。

在Singleton内部:

Customer MyDataStore::getCustomer(int custID)
{

for (Customer c : customers) {
    if (custID == c.getCustID()) {
        return c;
    }   
    }

}

在源代码内部:

int x = 0;
cout << "Please enter the customer ID!" << endl;
cin >> x;
Customer c = MyDataStore::getCustomer(x);
cout << c.print();