need a lil help

so i wrote a program that reads from a disk file and i need the program to read the disk file and separate each line into three fields of text
what would be the best way to do this??


for example i type in a account number and it gives me all the information that comes with that number

7221379 abula 970.51
6853921 allen 343.54
4215534 austin 76.78
5551298 bailey 4563.90
6224649 balovich 34.09
6502286 bates 543.34
8448936 battencourt 1234.23
2883903 blackbrun 767.90
6752376 boucher 844.23
3564698 brown 23.45
6373150 duncan 8759.89
6375372 estrada 3024.45
8736368 fernandez 7563.78
7218536 gallagher 3004.09
6681985 gonzales 5673.45
3909086 ha 3453.54
5221192 hernandez 7483.90
5901163 howard 356.45
2427821 john 435.67
6331655 jose 9009.45
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
#include <fstream>
#include <string>
#include <map>
#include <iostream>

struct File{
unsigned int AccountNumber;
std::string Name;
float Balance;
};

//...
std::map<unsigned int, File*> database;
std::ifsream infile("yourfile.txt");
File* record = 0;

while(!infile.eof())
{
     record = new File;
     infile>>record->AccountNumber>>record->Name>>record->Balance;
     database[record->AccountNumber] = record;
}

//access the data
unsigned int accountnumber = 0;
std::cout<<"Please enter an account number:"<<std::endl;
std::cin>>accountnumber;

std::map<unsigned int, File*>::iterator itr = database.find(accountnumber);

std::cout<<"Details: \n"<<"Account Number: "<<itr->first<<"\nName: "<<itr->second->name<<"\nBalance: "<<itr->second->Balance<<std::endl;

//...
//delete the memory;
itr = database.begin()
while(itr != database.end())
{
   delete (*itr);
    itr++;
}

//... 


http://www.cplusplus.com/reference/map/map/

Hope this helps.

Last edited on
when ever i try to run it it comes up with a lot of errors
im sorry if its something easy but im just starting out
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
#include <iostream>
#include <vector>
#include <fstream>

struct Record {
	int accountNumber;
	std::string name;
	float balance;
	Record(const int aN, const std::string &n, const float b)
		: accountNumber(aN), name(n), balance(b) { } 
};

int main(void) {
	std::ifstream ReadFile("file.txt");
	if(!ReadFile.is_open()) {
		std::cerr << "Error: Could not open file.\n";
		return -1;
	}
	int accountNum;
	std::string name;
	float balance;
	std::vector<Record> records;
	while(ReadFile >> accountNum >> name >> balance) {
		records.emplace_back(Record(accountNum, name, balance));
	}
	ReadFile.close();
	std::cout << "Account number: ";
	unsigned int index;
	std::cin >> index;
	if(index >= 0 && index < records.size()) {
		std::cout << "Account number: " << records[index].accountNumber << "\nBalance: " 
			<< records[index].balance << "\nName: " << records[index].name << '\n';
	}
}


http://en.cppreference.com/w/cpp/container/vector

The code above accomplishes everything you asked for in your original post. The code above has minimal error checking. The records stored in the std::vector object begin at the index of zero - remember that if you need to find the first record. There are a number of ways the code above could be improved, but I will leave that to you or another member.
Last edited on
Topic archived. No new replies allowed.