struct help

Pages: 12
Okay, I decided I'll still do it with vectors and containers. I'm trying to do that when "n" is pressed, it displays the 5 next books, when "p" is pressed it displays the 5 previous books and when "q" is pressed it quits the 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
for( size_t i = 0; i < container.size(); ++i )
	{
		 BOOK &book = container[i];
		 cout << "Book Name : " << book.NAME<< "\nPublished : " <<  book.YEAR << "\nPages : " <<  book.PAGES << "\nAuthor : " <<  book.FIRSTNAME << " " <<  book.LASTNAME << "\nLanguage : " <<  book.LANGUAGE << "\nRank : " <<  book.SCORE << "/10";
		cout << "\nOn Shelf : ";
		if ( book.READ == true)
			cout << "Read\n\n";
		else
			cout << "Not read\n\n";
		X:
		if((i+1)%5==0)
		{
			cout << "Press ""n"" to see the 5 next books, ""p"" to see the 5 previous books and ""q"" to exit the program.\n";
			string g;
			cin >> g;
			if(g == "n")
			{
				i++;
				if(i>5)
				{
					goto X;
				}
			}
			else if (g == "p")
			{
				i--;
				if(i>5)
				{
					goto X;
				}
				
			}
			else if (g == "q")
			{
				goto Y;
			}
			cin.ignore();
		}	
	}
	Y:
return 0;
}

The problem here is, when "n" is pressed it displays only 4 books and when "p" is pressed it displays only one book.
@starter
Please do not use GOTO in c++. Very poor programming style and easy to make mistakes. Creates what is known as, "spaghetti code". Anyway, here is that section and it works. To me, previous, means the 5 books shown BEFORE the ones currently on screen, so you can't just go back 5, otherwise, you're only viewing the same ones over and over. I have it where it doesn't look for the "p" key until 10 books have been shown. If you do press "p" after only 5 books are shown, it just shows the next 5, the same as if "n" were pressed.
Hope this is what you wanted it to happen..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
for( size_t i = 0; i < container.size(); ++i )
	{
		 BOOK &book = container[i];
		 cout << "Book Name : \t" << book.NAME<< "\nPublished : \t" <<  book.YEAR << "\nPages : \t" <<  book.PAGES << "\nAuthor : \t" <<  book.LASTNAME << ", " <<  book.FIRSTNAME << "\nLanguage : \t" <<  book.LANGUAGE << "\nRank : \t\t" <<  book.SCORE << "/10";
		cout << "\nOn Shelf : \t";
		cout << Read_Book[book.READ] << endl << endl;
		
		if((i+1)%5==0)
		{
			cout << "Press \"n\" to see the 5 next books,";
			if(i>=9)
				cout << " \"p\" to see the 5 previous books " << endl;
			cout << " or \"q\" to exit the program.\n";
			string g;
			cin >> g;
			if(g=="P" || g =="p" && i>=9)
				i-=10;
			if(g=="N" || g == "n")
				continue;
			if(g=="Q" || g == "q")
				break;

		}	
	}
Last edited on
Big thanks for your help! Now, the second part is this - the user has to enter his/her book details. for example:
Enter the books name: (user enters here)
Enter the authors name: (user enters here)
... and so on.
When the user has entered all the details, his book must be shown like the output in the previous exercise. Then the program asks if he/she wants to save the file. If "n" is pressed it saves and exits the program and when "p" is pressed it doesn't save and leaves the program. I hope you understand me.
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
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
struct BOOK
{
	string NAME;
	int YEAR;
	int PAGES;
	string FIRSTNAME;
	string LASTNAME;
	string LANGUAGE;
	bool READ;
	int SCORE;

	// constructor
	BOOK( const string name, int year, int pages, string firstname, string lastname, string language, bool read, int score )
		: NAME(name), YEAR( year ), PAGES( pages ), FIRSTNAME( firstname ), LASTNAME( lastname ), LANGUAGE( language ), READ( read ), SCORE( score ) {}
};

int main()
{
	fstream library;
	library.open ("library.txt");

	return 0;
}
	

I know this isn't much, but I really don't know how to deal with file i/o. I hope you can help me.
Last edited on
@starter

Sorry, I haven't got much experience in i/o either. Hopefully, others here can be of help.
It's surprisingly like console I/O. For input, one must first declare an ofstream object (after #include'ing fstream)

1
2
3
ofstream non_initialized; //Doesn't set its file location to anything
non_initalized.open("C://path/to/your/file/goes/here"); //This opens a file
ofstream initialized ("C://path/to/your/file/goes/here"); //This declares a new object and opens a file 


Then everything's pretty much like console I/O

initialized << "C++ was here." << endl;

While in some cases it'll do this automatically, I suggest you use initialized.close() at the end (at which point you can .open() other files, too.)

Input's pretty much the same, but you use ifstream instead of ofstream (and, of course, you use >>, getline(), and anything else applicable to cin)
Topic archived. No new replies allowed.
Pages: 12