---So far i have this code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream file;
file.open("example.txt");
int a;
string title;
string author;
string des;
int b;
while (file >> a >> title >> author >> des>>b){
}
getline(file, des);
cout << a << endl;
cout << title << endl;
cout << author << endl;
cout << des << endl;
getline(file, des);
cout << b << endl;
cin.get();
return 0;
}
----------------------------------Here is the text file that has to be read:
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 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
---Cant really figure out how to show everything from the text file given. It is reading line by line.
There is a pattern in the input file if you can notice...
------- contents of file ------
0 ----------------------> Marks the beginning of book info (index?..i don't know..maybe)
First_Book
Michael_Costantino
My first Book
10034
10
3 ----------------------> Marks the end of the book info (i don't know what's this value)
1 ----------------------> Marks again the beginning of next book info (index?..i don't know..maybe)
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 ----------------------> Marks again the end of the book info (i don't know what's this value)
2 ----------------------> Marks again the beginning of next book info (index?..i don't know..maybe)
BEGINNING_C++_THROUGH_GAME_PROGRAMMING
Michael_Dawson
approaches learning C++ from the unique and fun perspective of games.
2834970
100
22 ----------------------> Marks again the end of the book info (i don't know what's this value)
See the description of the contents of the input text file?
As you can notice of the pattern, you can somehow do this things below:
1. Declare 7 variables representing 7 book info (see lines 1-7, the book info). Use variable names that correspond to each property of a book e.g. index, title, author, description, ...)
2. Open the input file
3. If file is not opened, exit from main() otherwise, continue execution
4. Use getline() function to read a line from the file. You can have 7 getline() calls to get each 7 book properties.
5. After the 7 getline() calls, try to display the whole book info to verify if what you have read is correct.
6. After displaying the whole book info (7 book properties), check if file pointer has reached the end-of-file.
7. If the result of 6 is false which means the file pointer has not yet reached the end-of-file, repeat steps 4-7.
8. Return or exit from main()
paste your code and we will debug. Put code inside code blocks to be more readable. You can insert the formatting code blocks by choosing "<>" in the Format table in the right of the text box when replying. Your work for it, I will help.
When you put your codes inside the code block...it will be like this
I don't understand what you are doing at lines 23-25.
You better represent all book properties with 7 strings. So you should have declared 7 strings in main e.g.
int main() {
string index, title, author, desc, num1, num2, num3;
ifstream input;
//open file
input.open("c:\\input.txt", ios::in);
//if file is not opended, print and error then exit from main
if (!input.is_open()) {
cout << "Input file is not opened successfully!" << endl;
return -1;
}
// Read 7 book properties from file
getline(input, index);
getline(input, title);
getline(input, author);
...please continue with the rest of the properties
// Try to display what you have read from file using getline
// Display the 7 properties read from file
cout << "Displaying book information..." << endl
cout << "Book index: " << index << endl;
cout << "Book title: " << title << endl;
cout << "Book author: " << author << endl;
....please continue printing the rest of the book properties read form file
After that, when you have read all the properties of the first book successfully, you can somehow expand your code to use loop by repeating lines 14 up to 26. The loop should stop executing when the file has reached the end of file. The function that you can use to detect if the pointer of the input file has reached the end of file is the function eof().
input.eof() will return true if all the data from file is read but you still read more data.
example:
say for example you have a text file which contains 1 line only...
1 2 3 4 5 6 7 8 9 10
ifstream in;
string line;
in.open("c:\\onelinefile.txt", ios::in);
in.eof(); --> this call to eof() will returnfalse since the 1 line content has not yet read
getline(in, line); --> read the 1 line content;
in.eof(); --> this call to eof() will returnfalse, though the line is read. eof() will only be trueif there is a call to read data from a stream and no more data has to be read.
getline(in, line); --> attempting again to read data but no more data to be read. This call will cause the eof() call to returntrue
in.eof(); --> this will now returntrue since you tried to read data but no more data is found.
Use that concept of eof() function for you to continue reading all the books with 7 properties from the file. You can use the value returned by eof() function in a condition of your loop so you will know when to terminate the loop that reads 7 book properties from file.
int main () {
string index = ""; // Holds the value that looks like index value? I don't know
string title = ""; // Holds the value for book title
string author = ""; // Holds the value for book author
string desc = ""; // Holds the value for book description
string num1 = ""; // Holds the value of - i don't know - but it is a number. hehe
string num2 = ""; // Holds the value of - i don't know - but it is a number. hehe
string num3 = ""; // Holds the value of - i don't know - but it is a number. hehe
/*
** 0 --> index ?
** First_Book --> title
** Michael_Costantino --> author
** My first Book --> desc
** 10034 --> num1
** 10 --> num2
** 3 --> num3
** ----------------------------------------------
** 1 --> index?
** C++_for_Dummies --> title
** Stephen_R._Davis --> author
** Enter the world of computer ... --> desc
** 392048 --> num1
** 3 --> num2
** 1 --> num3
** ----------------------------------------------
** ...input file content continuation but same format as each book info above
**/
//declare an stream to open input file
ifstream input;
//open file
input.open("c:\\input.txt", ios::in);
//exit from main if file is not opened
if (!input.is_open()) {
return -1; //-1 for error...anyways, don't care
}
//Read all entries of book information from the input file
You can expand that code based on my advise at my previous comment.
do i repeat this function like multiple times so it keeps reading till the end?
Because thats the reason why i put while loop function to read everything off the notepad and would put a (break;) once it has read everything
You don't have to copy-paste the same code multiple times until all is read from the file since you cannot know how many books info found in the file. Use loop. You use loop but it is wrong.
Did you read my comments above to use loop and use eof() function to determine the condition if there are more data from the file?
This is the while loop should somehow look like and not your while loop above. Now, it is up to you on how to generate the condition. This condition will control the loop on when to stop reading book info from file. I have hints above and in my previous comments. Understand it so you can use it.