Question

I have a question, i want to read every title books being read on the notepad and it reads everything. How can i read only the title of the book in the notepad.

-------------------------------Here is my code so far:
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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{

	



	ifstream file;
	file.open("example.txt");
	file.eof();

	string a;
	string title;
	string author;
	string des;
	string b;
	string c;
	string d;
	int x;

	cout << "Type 1,2,3,4,5 to perform a search." << endl;
	cout << "1. Title" << endl;
	cout << endl;
	cin >> x;

	switch (x)
	{
	case 1:
		while (file){
			getline(file, title);
			cout << title << endl;
			
		}
		break;
	}
	cin.get();




	cin.get();
	return 0;
}


And my output came into this
Type 1,2,3,4,5 to perform a search.
1. Title

1
0
First_Book
Michael_Costantino
My first Book
10034
10
3
1
C++_for_Dummies
Stephen_R._Davis
Enter the world of computer programming with this step-by-step guide to the C++
language! C++ is a great introduction to object-oriented programming,
392048
3
1
2
BEGINNING_C++_THROUGH_GAME_PROGRAMMING
Michael_Dawson
approaches learning C++ from the unique and fun perspective of games.
2834970
100
22
3
Structured_Programming_Approach_Using_C
Behrouz_A._Forouzan
A Structured Programming Approach Using C continues to present both computer sci
ence theory.
534491324
50
21
4
Structure_and_Interpretation_of_Computer_Programs
Harold_Abelson
Different approaches to dealing with time in computational models.
34343455
34
6
5
Introduction_to_Algorithms
Thomas_H._Cormen
Broad range of algorithms in depth.
4343445
23
9
6
Design_Patterns
Erich_Gamma
Elements of Reusable Object-Oriented Software
342322
44
23
7
Introduction_to_the_Theory_of_Computation
Michael_Sipser
Automata and Languages, Computability Theory and Complexity Theory.
4232676
23
21
8
Advanced_Programming_in_the_Unix_(R)_Environment
W._Richard_Stevens
summary and guide to advanced programming in the Unix environment.
3434236
32
24
9
Database_System_Concepts
Abraham_Silberschatz
Database System Concepts
2327879
23
12
10
Structured_Computer_Organization
Andrew_S._Tanenbaum
How computer designers can follow the structured model to develop efficient hard
ware and software systems.
2324346
67
55
11
The_Psychology_of_Computer_Programming
Gerald_M._Weinberg
computer programming as human performance, a social activity, and an individual
activity.
2323234
45
12
12
The_Basics_of_Digital_Forensics
John_Sammons
Details on digital forensics for computers, networks, cell phones, GPS, the clou
d, and Internet.
232323
44
22
13
Advanced_3D_Game_Programming_With_Directx_10.0
Peter_Walsh
Guide to developing cutting-edge games using DirectX 10.0.
143454
12
11
14
Network_and_System_Security
John_R._Vacca
Focused coverage of network and system security technologies.
2323456
12
6




-------------------------------Text file being read:
0
First_Book----------Title of the book
Michael_Costantino
My first Book
10034
10
3
1
C++_for_Dummies------------Title of the book
Stephen_R._Davis
Enter the world of computer programming with this step-by-step guide to the C++ language! C++ is a great introduction to object-oriented programming,
392048
3
1
2
BEGINNING_C++_THROUGH_GAME_PROGRAMMING-------------Title of the book
Michael_Dawson
approaches learning C++ from the unique and fun perspective of games.
2834970
100
22
3
Structured_Programming_Approach_Using_C
Behrouz_A._Forouzan
A Structured Programming Approach Using C continues to present both computer science theory.
534491324
50
21
4
Structure_and_Interpretation_of_Computer_Programs
Harold_Abelson
Different approaches to dealing with time in computational models.
34343455
34
6
5
Introduction_to_Algorithms
Thomas_H._Cormen
Broad range of algorithms in depth.
4343445
23
9
6
Design_Patterns
Erich_Gamma
Elements of Reusable Object-Oriented Software
342322
44
23
7
Introduction_to_the_Theory_of_Computation
Michael_Sipser
Automata and Languages, Computability Theory and Complexity Theory.
4232676
23
21
8
Advanced_Programming_in_the_Unix_(R)_Environment
W._Richard_Stevens
summary and guide to advanced programming in the Unix environment.
3434236
32
24
9
Database_System_Concepts
Abraham_Silberschatz
Database System Concepts
2327879
23
12
10
Structured_Computer_Organization
Andrew_S._Tanenbaum
How computer designers can follow the structured model to develop efficient hardware and software systems.
2324346
67
55
11
The_Psychology_of_Computer_Programming
Gerald_M._Weinberg
computer programming as human performance, a social activity, and an individual activity.
2323234
45
12
12
The_Basics_of_Digital_Forensics
John_Sammons
Details on digital forensics for computers, networks, cell phones, GPS, the cloud, and Internet.
232323
44
22
13
Advanced_3D_Game_Programming_With_Directx_10.0
Peter_Walsh
Guide to developing cutting-edge games using DirectX 10.0.
143454
12
11
14
Network_and_System_Security
John_R._Vacca
Focused coverage of network and system security technologies.
2323456
12
6

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
case 1: {
                // There are 7 properties of a book, 2nd is the title.
                // so we mark every 2nd of every book property since that is the title
                int ctr = 0;
		while (file){ //not sure with this condition. If this works for you then fine. 
                        ctr = ctr + 1;
			getline(file, title);
                        // 2nd line is the title so print/process it
                        if (ctr == 2) {
			     cout << title << endl;
                        }
                        // If count of lines read has reached 7 (7 properties of a book), reset to 0
                        // in preparation for counting properties of the next book.
			ctr = ctr % 7;
		}
		break;
 }
Thanks alot also i have one more question, once the user enter the title of the book, how would i grab all the details of that book such as(author,description of the book,etc.)
Last edited on
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
string title_tosearch;

cout << "Enter a title of the book to search from file: ";
cin >> title_tosearch;

string index, title, author, desc, num1, num2, num3;

while (file) {
     //at every cycle, read all the 7 properties of a book.
     getline(file, index);
     getline(file, title);
     getline(file, author);
     getline(file, desc);
     getline(file, num1);
     getline(file, num2);
     getline(file, num3);

     //check if this particular book has title same with the title_tosearch value
     if (title_tosearch == title) {
           //Book found
           cout << "\nBook with title \"" << title_tosearch << "\" found from the file!" << endl;
           cout <<  "Below are the details of the book:" << endl;
           cout << "Book index: " << index << endl;
           cout << "Book title: " << title << endl;
           cout << "Book Author: " << author << endl;
           ...

    }

}

Topic archived. No new replies allowed.