How to link vectors + sorts + binary searches?

Hi all, I am starting to create the bones of the program below. I am looking to...
a) read info from a file
b) store that info in a vector
c) sort that info
d) print that now sorted info

I have no idea wtf I am doing at this point. I've been reading this C++ book but I feel that it confuses me more. Could someone provide the light I need in order to understand how I can link these functions together read the data in the file, throw it in a vector, have it sorted and than print it? Many thanks, cheers

I would like to add that I am just focusing on the code which shows the Last Names in the file. After I can complete this, I will move on...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85

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

using namespace std;

class CustomerInfo

{

public:
	CustomerInfo();
	~CustomerInfo();

	void inputCustomerData(string _lastName, string _zipCode, string _state, double _rebateAmt);
	void vectorCustomerData(); 
	void sortCustomerData();
	void searchCustomerData();
	void print();
	int getlastName();
	int getzipCode();
	int getstate();
	int getrebateAmt();


private:
	string lastName;
	string zipCode;
	string state;
	string rebateAmt;
};


int main()


{


    system("pause");
    return 0;   
}

void inputCustomerData() // Input stream function
{
    ifstream input;
    input.open("notebook.txt"); // Info will be read from this file
    
    int x=0, i=0;
    
    for(x; x < 0; x++)
	{
        input >> lastName[x];

       
    }
}

void vectorCustomerData() // Intializes a Vector Object
{
vector<string> intList; 

intList.push_back(); 


}


void sortCustomerData() // Sorts the list before Binary Search 
{

}

void searchCustomerData() // Binary Search function 
{

}
	

void printCustomerData() // Prints a list containing all info 
{

}
Last edited on
Simplify, simplify, simplify.
_._._ Means fill in the blanks

Step 1:
Cut back your CustomerData structure to just the data.
Make it a struct and make everything public i.e. remove all public/private declarations.

Step 2:
Use typedef(s) all the time.
Don't ask why, just trust an old hand at this USE TYPEDEFS ALL THE TIME.
e.g.
typedef std::vector<CustomerInfo> Customers;
typedef Customers::iterator CustomersIt;
typedef Customers::const_iterator CustomersItC;

Step 3:
Write your main() as pseudo-code, e.g.
1
2
3
4
5
6
7
8
int main(int argc, char** argv)
{
    // parse command line arguments (this is optional in exercise programs)
    // read (customer) data from file
    // sort records
    // search for a customer
    // print customer records
}


Write your reader operation and pass it the name of the file and the collection it must add those records to, e.g.
void readCustomerData(Customers &rCustomers, char const* filePath) { _._._ }
CustomersItC searchCustomerData(Customers &rCustomers, string const& rSurname) { _._._ }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

ostream& operator<<(ostream& rOs, Customer const& rCustomer)
{
    _._._
    return rOs;
}

void printCustomerData(ostream& rOut, Customers &rCustomers)
{
    // loop through all customer records and print them
    for (CustomersItC it = rCustomers.begin(), end = rCustomers.end();
        it != end; ++it)
    {
         rOs << *it << std::endl;
    }
}
Topic archived. No new replies allowed.