Reading in TEXT files

Ok guys, so I have this program. Menu driven, input a traffic ticket with 8 variables is step 1. Step 2 is to be able to search by ticket number then print the ticket. Then be able to search by month, sort those by location, and count how many there are. So far I have figured out how to write them to a file, and read in the file. I'm stuck on how to read in the file, set each string to variable IE. first string is the ticket number, second string is office number so on and so forth so can search by ticket number. Can anyone help me please? I am VERY new to programming and still trying to figure out the basics.

BTW, my tutor suggested most easy way is to do this as a text file with strings, I know others are using classes and vectors. I don't understand those too well yet so trying to start off with baby steps...

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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Jeff Kirn
// 10/20/2014
 

#include <iostream>
#include <iomanip>
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>

using namespace std;


main()
	{
		
	string ticketNum = "",
		   officeNum = "",
		   plateNum = "",
		   state = "",
		   location = "",
		   codeNum = "",
		   date = "",
		   time = "";
		   
	double input = 0;
	
	cout<<"Welcome to the PIGS database.\nPlease tell me what you would like to do."<<endl;
	cout<<"Press 1 to input a ticket.\nPress 2 to find a ticket.\nPress 3 to sort tickets by month.\n"<<endl;
	cin>>input;
	
	while ( input < 1 || input > 3)
	{
		 cout<<"\nPlease input a 1,2, or 3.\nPress 1 to input a ticket.\nPress 2 to find a ticket.\nPress 3 to sort tickets by month.\n"<<endl;
		 cin>>input;
	}
	 cin.ignore();
	while (input == 1)
	{ 
	cout<<"What is the ticket number? : ";
	getline(cin,ticketNum);
	cout<<"What is the office number? : ";
	getline(cin,officeNum);
	cout<<"What is the liscense plate? : ";
	getline(cin,plateNum);
	cout<<"What state is the car from? : ";
	getline(cin,state);
	cout<<"Where did the ticket take place? Austin, Cedar Park, Leander or Round Rock? : ";
	getline(cin,location);
	cout<<"What is the violation code?\n01- Speeding $150."<<endl;
	cout<<"02-Running a stop light $200.\n03-Driving without a seat belt $125."<<endl;
	cout<<"04-Driving while Intoxicated (DWI) $1000.\n05-Driving with an invalid vehicle registration $100."<<endl;
	cout<<"06-Having expired or missing license plates $200. : ";
	getline(cin,codeNum);
	cout<<"What time did the ticket happen? : ";
	getline(cin,time);
	cout<<"What was the date?  (mm/dd/yyyy): ";
	getline(cin,date);
	cout<<""<<endl;
	cout<<"Ticket number is "<<ticketNum<<endl;
	cout<<"Office number is "<<officeNum<<endl;
	cout<<"Plate number is "<<plateNum<<endl;
	cout<<"Ticket happend in the state of "<<state<<endl;
	cout<<"Ticket happend in the city of "<<location<<endl;
	cout<<"Violation code is "<<codeNum<<endl;
	cout<<"The time the ticket happened was "<<time<<endl;
	cout<<"The date the ticket took place was "<<date<<endl;
	
	ofstream outputfile;
	outputfile.open("Tickets.txt",std::ios_base::app);
	outputfile<<ticketNum<<'\t'<<officeNum<<'\t'<<plateNum<<'\t'<<state<<'\t'<<location<<'\t'<<codeNum<<'\t'<<time<<'\t'<<date<<endl;

	
	cout<<"\nWelcome to the PIGS database.\nPlease tell me what you would like to do."<<endl;
	cout<<"Press 1 to input a ticket.\nPress 2 to find a ticket.\nPress 3 to sort tickets by month.\n"<<endl;
	cin>>input;
	 cin.ignore();
	

	}
	
	while(input==2)
	{
	
		string line;
		ifstream myfile ("Tickets.txt");
		if (myfile.is_open())
		{
			while ( getline (myfile,line) )
			{
				cout << line<< '\n';
			}
			myfile.close();
			system("pause");
		}
  
		else cout << "Unable to open file";
	}
		return 0;
		
	
	
	}
	
Last edited on
Topic archived. No new replies allowed.