Help in File Stream and searching a book in my program

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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
void displayAdmin()
{
	
	//this will display if admin is chosen

	int choice;
		do{

			cout<<endl;
			cout<<"\t\t================LIBRARY==================\n";
			cout<<"\t\tSearch by:\n\n";
			cout<<"\t\t[1] Title\n\n";
			cout<<"\t\t[2] Author\n\n";
			cout<<"\t\t[3] Add Book\n\n";
			cout<<"\t\t[4] Quit\n\n";
			cout<<"\t\t=========================================\n\n";
			cout<<"\t\tEnter your Choice: ";
			cin>>choice;
			}while(choice<1&&choice>4);
			system("cls");
			switch(choice)
			{
			case 1:
				{
					string acctr;
					int ctr = 0;

					ifstream mainfile("library.dat");//open file
					
					if (!mainfile.good())//if not existing file
					{
						//if not existing
						cerr<<"Can not load data files."<<endl;
						exit(1);
					}

					while (!mainfile.eof())
					{
						//execute while it is not the end of file
						
						mainfile>>rary[ctr].title>>rary[ctr].author>>rary[ctr].category;//read file
						
						ctr++;//every line 
					}
						
					mainfile.close();
					//close file
						
					cout<<"Enter Title: ";

					cin>>acctr;
					system("cls");
					for (int ind=0; ind<200; ind++)
					{
						if (acctr == rary[ind].title)//find match on the file from title input
						{
						
							cout<<"Title: "<<rary[ind].title<<endl;
							cout<<"Author: "<<rary[ind].author<<endl;
							cout<<"Category: "<<rary[ind].category<<endl;
							cout<<endl;
							found=true;
						}
						

					}
				if(!found)//if not found
				cout<<"BOOK NOT FOUND!!";

				}
				break;
				case 2:
				{
					string acctr;
					int ctr = 0;
					string pass;
					int i = 0;

					ifstream mainfile("library.dat");//open file
					
					if (!mainfile.good())
					{
						//if not existing 
						cerr<<"Can not load data files."<<endl;
						exit(1);
					}

					while (!mainfile.eof())
					{
						mainfile>>rary[ctr].title>>rary[ctr].author>>rary[ctr].category;
						ctr++;
					}
						
					mainfile.close();
						
					cout<<"Enter Author: ";

					cin>>acctr;
					system("cls");
					
					for (int ind=0; ind<200; ind++)
					{
						if (acctr == rary[ind].author)//check author input if match on file
						{
							
							cout<<"Title: "<<rary[ind].title<<endl;
							cout<<"Author: "<<rary[ind].author<<endl;
							cout<<"Category: "<<rary[ind].category<<endl;
							cout<<endl;
							found=true;
						}

					}
				if(!found)//if no match
				cout<<"BOOK NOT FOUND!!";
				}
				break;
				
				case 3:
					{
						int ctr=0;
						ifstream mainfile("library.dat");
					
						if (!mainfile.good())
						{
							cerr<<"Can not load data files."<<endl;
							exit(1);
						}

						while (!mainfile.eof())
						{
							mainfile>>rary[ctr].title>>rary[ctr].author>>rary[ctr].category;
							ctr++;
						}
											
						mainfile.close();
						ofstream outfile("library.dat", ios::app);//open file for writing 
						//insert to text file 
						
							
							cout<<"Enter Book Title: ";
							cin.ignore();
							getline(cin,rary[ctr].title);
							cout<<"Enter Book Author: ";
							cin.ignore();
							getline(cin,rary[ctr].author);
							cout<<"Enter Book Category: ";
							cin.ignore();
							getline(cin,rary[ctr].category);
							outfile<<rary[ctr].title<<" ";
							outfile<<rary[ctr].author<<" ";
							outfile<<rary[ctr].category<<endl;
							cout<<"BOOK HAS BEEN ADDED!!"<<endl;
							system("pause");
							system("cls");
							displayAdmin();
						
					}
				case 4:exit(0);
			default:cout<<"Invalid Input";
			}
		
}


program can't search properly if the title or author has a space
Formatted input (operator>>) reads space delimited values. So if you enter line "John Smith", line std::cin >> str; will read only "John", leaving " Smith" in input buffer.
To read whole line, you should use getline() function:
http://www.cplusplus.com/reference/string/basic_string/getline/
http://en.cppreference.com/w/cpp/string/basic_string/getline

You would probably run into another problem, so there is an answer for that: http://stackoverflow.com/a/21567292/3410396
Last edited on
Topic archived. No new replies allowed.