Searching text files, would this method work?

Hi, I'm new to C++ and am wondering if I am going about doing this right.
I am working on a simple library management system project. I have it working up to the point where the user can enter information on students and books and send that information to a text file. I've been having a lot of issues past that point.

Here is the format of how the student and book information is stored in the file: (Book and student information stored in separate files)

student ID number
Student Name
Date
0 if no book checked out. Replaced with book ID once student has book checked out.


Book ID number
Book Title
Book Author

My plan has been as follows:
When the student checks out a book (after entering some student and book information) the user will be prompted to enter a student ID. The program will then search for that ID within the student file. If the ID is found the line with the ID and the next three lines would be read into an array to work with and then displayed on the screen. Further access won't be granted if this student already has a book checked out as indicated by the last line. Next they would prompted to enter a book ID. If book ID is found in the book file the book title replaces the last line of of that student's information in the array.

Could this work? I've been looking around and have been having trouble finding anything that would help.

I'm wondering if I'm going about this the wrong way. I'm going to look into just sending the information straight into an array after it is entered, as I have been having problems with the above method using a text file. Thinking about it, maybe it would be better to have all the information on one line, separated by spaces.

currently working code consists of a series of switch statements that make up the menu, a student and book class, and functions to print input to screen and send the information to a specified text file.

Any advice would be appreciated.

Thanks
Last edited on
Here's my current progress. Not going quite how I want it to.

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
//.......................................CheckOutBook...................................//

//start this off by reading data in from the student.txt file

 void readStudent(){


	 //Here is where we open the file, and load it into the array
	 int ID; 
	 int find=0; 
	 const int STUDENT=31; 
								//Both of these arrays read in the same information from the same text file. One array is int, the other is string.
	 int studentarray[STUDENT]; //Array used for searching the student ID
	 string SstudentArray[STUDENT]; //Array used for displaying information and writing to file

	 //...........................read into int studentarray[STUDENT] and string SstudentArray[STUDENT];...........................//

	 ifstream student("student.txt");
	if(!student){
		cout<<"Error opening file. Shutting down...\n";
		exit(EXIT_FAILURE);
	}

	for(int counter=0; counter<STUDENT; counter++)
	{
		student>>studentarray[counter];
				student>>SstudentArray[counter];
	
	}


	//Next we search for a specific student via ID number
	//Line with ID number and the next three lines are displayed (going to have to use loops and counters)
	cout<<"Please enter student ID: ";
	cin>>ID; 

	for(int srch = 0; srch < STUDENT; srch++)
  {
    if(studentarray[srch] == ID)
    {
cout << "Displaying student information:"<<endl; 
cout<<endl; 
	cout<<"Student ID: "<<SstudentArray[srch] << "\n";
			cout<<"Student Name: "<<SstudentArray[srch+1] << "\n";
				cout<<"Date: "<<SstudentArray[srch+2] << "\n";
					cout<<"Book checked out: "<<SstudentArray[srch+3] << "\n"
     << endl;

      //find += 1;
		find++;
    }
  }

	if(find < 1)
  {
    cout << "Target not found." << endl;
  }


	//If the 3rd line after the ID number is a 0 the user can no longer continue pass this point.
	//The user enters the title of a book which then replaces the zero.
	//Prefer to read books.txt into its own array. search for a book ID. 
	// student element conaining the zero is set equal to the book element containing book title. Address is one greater than the ID element.

	//Finally the contents of the array over write the old contents of the file.



	student.close(); 



 }



 
//.......................................End CheckOutBook...................................// 
Topic archived. No new replies allowed.