Problem with getline, page system

Hello
I have to make a text file database. I have a text file with 16 car names and this code.
First question is, why in void Addnew it skips first getline input and goes right to the second one?
And second question is, how to make a page system, 5 names/lines at a time. So first page shows 1-5 carnames, second page 5-6 etc.
Thank you

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
#include <iostream>
#include <string>
#include <fstream>
#include <windows.h>
#include <cstdlib>

void ViewDatabase();
void Addnew();

int main()
{
	std::cout << "Choose" << std::endl;
	std::cout << "1. See Database" << std::endl;
	std::cout << "2. Add new car" << std::endl;
	int choice;
	std::cin >> choice;

	switch (choice)
	{
	case 1:
		ViewDatabase();
		break;
	case 2:
		Addnew();
		break;
	}
}

void ViewDatabase()
{
	system("cls");
	std::string line, page;
	std::fstream database("database.txt");
	if (database.is_open()) {
		for (int i = 0; i < 5; i++) {
			std::getline(database, line);
			std::cout << i + 1 << ". " << line << std::endl;
		}
		database.close();
		std::cout << std::endl << "\"n\" next page | \"p\" previous page | \"e\" exit" << std::endl;
		std::cin >> page;
	}
	system("pause");
}

void Addnew()
{
	system("cls");
	std::string carname, prodyear, power, fuel, engine, eng_pos;
		
	std::cout << "Enter the cars name: " << std::endl;
	std::getline(std::cin, carname); //skips this
	std::cout << "Enter the production year(s): " << std::endl;
	std::getline(std::cin, prodyear);
	std::cout << "Enter the engine power output: " << std::endl;
	std::getline(std::cin, power);
	std::cout << "Enter the fuel type: " << std::endl;
	std::getline(std::cin, fuel);
	std::cout << "Enter the engine details: " << std::endl;
	std::getline(std::cin, engine);
	std::cout << "Enter the engine position: " << std::endl;
	std::getline(std::cin, eng_pos);
	
		
	std::cout << "Do you want to save? (y/n)" << std::endl;
	std::string save;
	std::cin >> save;
	if (save == "y") {
		std::ofstream database("database.txt", std::ios::app);
		database << carname << "; " << prodyear << "; " << power << "kW; " << fuel << "; " << engine << "; " << eng_pos << ' ' << std::endl;
		database.close();
		std::cout << "Save successful" << std::endl;
		system("pause");
	}
	else {
		std::cout << "Save not successful" << std::endl;
		system("pause");
	}
}
Last edited on
First question is, why in void Addnew it skips first getline input and goes right to the second one?

The problem is caused by the input of choice in main(). That input leaves the end of line character in the input buffer which getline() grabs as it's only input. You need to manually remove this offending character.

And second question is, how to make a page system, 5 names/lines at a time.

What do you mean by "page system" and where are you trying to implement this feature.
By page system I mean to display certain amount of lines. My text file has 16 objects or lines. I want to display lines 1-5 then asks "n" next page, "p" previous page or "e" exit". By pressing "n" you go to lines 6-10, then asks again your input and so on until you exit by pressing "e". Also it has to be versatile, because you can always add objects to the text database.
Well trying to randomly read a text file is slow and error prone so perhaps you should consider using some kind of class/structure to hold the information for each object, then use a vector of that object. It is fairly easy to jump around a vector and to only display a few objects at a time.





Topic archived. No new replies allowed.