Searching a file

Hello, I am working on a practice customer database program. Everything is ok just Im dumbfounded on how to do the search function I have no clue where to start. I want the user to beable to type in the order Number and the customers info would come up.

code:
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
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;

class customerData
{
    public:
    char fullName[100];
    char orderNumber[50];
    char email[75];

};
void addCustomer()
{
    customerData customer;
    string yesNo;
    do {
    system("cls");
    cout << "Customer Name: ";
    cin >> customer.fullName;               //Input from User.
    cout << "Order Number: ";
    cin >> customer.orderNumber;
    cout << "Email: ";
    cin >> customer.email;

                                //Printing out info to screen.
    system("cls");
    cout << "Is this information correct? yes/no.\n" << endl;
    cout << "Customer Name: " << customer.fullName << endl;
    cout << "Order Number: " << customer.orderNumber << endl;
    cout << "Email Address: " << customer.email << endl;
    cin >> yesNo;
    }while(yesNo == "no");
    if(yesNo == "yes"){
        system("cls");
        cout << "Saved customer info in the database..." << endl;
        ofstream database("database.txt", ios::out | ios::in | ios::app);
        database << "Customer Name: " << customer.fullName << endl;
        database << "Order Number: " << customer.orderNumber << endl;
        database << "Email Address: " << customer.email << endl << endl;
    }


}

bool search()
{
    ifstream database("database.txt");
    if(database.is_open){
        while(true){
            cout << ""
        
        
        }
    }
}
int main()
{
    int selection, choice;
    do{
    system("cls");
    cout << "Customer Database\n" << endl;
    cout << "1.Add Customer Info " << endl;
    cout << "2.Search for Customer Info" << endl;
    cin >> selection;
    switch (selection)
    {
        case 1: addCustomer(); break;
        case 2: search(); break;
    }
    cout << "1. Main Menu " << endl;
    cout << "2. Exit " << endl;
    cin >> choice;
    }while(choice == 1);
    if(choice == 2){
        std::exit(EXIT_FAILURE);
    }
    return EXIT_SUCCESS;
}
Well...

You don't exactly have a database here my friend. Rather you have a textfile full of values right?

The standard template library provides std::map<T, T>, you could use std::map<int, customerData*> to store key / value pairs.

Assuming your text file makes sense, you could have each line like so:
0001 Aragorn aragorn@gondor.net
0002 Legolas legolas@mirkwood.net
0003 Gandalf gandalf@shire.net


Using an std::ifstream you could imput these values into the map and provide lookup functions.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// defined at the top of your file
map<int, customerData*> _customers;

bool search(int order)  {
   map<int, customerData*>::iterator c;
   c = _customers.find(order);
   return (c != _customers.end()) ? true : false;
}

// elsewhere

customerData *c;
if (search(0001))
   c = _customers[0001];

cout << c->orderNumber;  // => 0001
cout << c->fullName;     // => "Aragorn"
cout << c->email;        // => "aragorn@gondor.net"


This is a contrived example of a simple mapping application. A real database like SQL does a lot more than accessing names by id. I'm sure this is just a practice project like you said, but you wouldn't want to do the above as a real life answer to this issue.
Last edited on
How about SQL? http://w3schools.com/sql/default.asp :D
If you don't want to manage a database its still quite useful for SQL injections :D
Topic archived. No new replies allowed.