How to add data to the end of a line in text file in C++?

I am a beginner in C++ programming. I need to create a program for a movie reservation system. I have done it until halfway through, but I am stuck at selecting the seats. I need to program it like the user must select a seat. The user can select seats for about 10 times in a loop. After selecting it, all the data should be stored in the text file, and in the line which I have stored the information about the particular movie. I am really hoping for a good suggestion to this problem. This is how I arrange the text file:

Avengers|1600|40|16
LoST World|3200|40|16
Titanic|6897|87|16
Avengers|1600|40|16

[code]
#include<iostream>
#include<cmath>
#include<fstream>

using namespace std;

display_all()
{
char name [25];
char hours[25];
char seats[25];

fstream movie;
movie.open("Movies.txt",ios::in);

int count = 0;
string line;

ifstream file("Movies.txt");
while (getline(file, line))
count++;

int i = 0;
while(!movie.eof(), i++ , i<= count)
{
movie.getline(name,25,'|');
movie.getline(seats,25,'|');
movie.getline(hours,25);
cout<< "(" << i << ") " << name << "\n ";
}
return 0;
}

main()
{
char name [25];
char hours[25];
char seats[25];
char price[25];
char seats1[25];
char seats2[25];
char seats3[25];
char seats4[25];
char seats5[25];

char response = 'Y';
string customer, phone_number;
fstream movie;
movie.open("Movies.txt",ios::in);
char selection_role ;
int selection_movie ;

cout << " *************************************************************** " << endl;
cout << " MOVIE TICKET RESERVATION SYSTEM (MTRS) " << endl;
cout << " *************************************************************** " << endl;
cout << endl << endl ;
cout << " Please specify the selection : (1) CUSTOMER" << endl;
cout << " (2) MANAGEMENT" << endl;
cout << endl;
cout << " INPUT : ";
cin >> selection_role;

if (selection_role == 1);

cout << endl;
cout << "MOVIES AVAILABLE : ";
display_all();

cout << endl;
cout << "Please select a movie: ";
cin >> selection_movie ;

int i = 0;
while(i++ , i <= selection_movie)
{
movie.getline(name,25,'|');
movie.getline(seats,25,'|');
movie.getline(hours,25,'|');
movie.getline(price,25,'|');
}

cout << endl;
cout << "======You have selected " << name <<".=========" <<endl;

cout << endl;
cout << "Movie hours: " << seats << endl;
cout << "Number of seats available: " << hours << endl;
cout << " SCREEN "<<endl << endl;
cout << "1 2 3 4 5 6 7 8" <<endl << endl;
cout << "9 10 11 12 13 14 15 16"<<endl << endl;
cout << "17 18 19 20 21 22 23 24"<<endl << endl;;
cout << "25 26 27 28 29 30 31 32"<<endl << endl;

cout << "Enter the number of seat you would like to book for: ";
cin >> seats1 ;

cout << endl;
cout << "Price for the ticket: RM " << price << endl << endl;
cout << "Enter you name: " ;
cin >> customer ;
cout << "Enter your phone number: ";
cin >> phone_number;

cout << endl;
cout << " MOVIE TICKET " << endl;
cout << "============================" << endl;
cout << "Name :" << customer << endl;
cout << "Handphone number :" << phone_number <<endl;
cout << "Movie selected :" << name << endl;
cout << "Seat selected :" << seats1 <<"," << seats2 << endl;
cout << "Price for the movie(RM):" << price << endl;
}
Obviously this code hasn't been compiled and tried. There are numerous issues. As provided, I've re-factored the code so that it actually compiles, reads the file, displays the contents and asks for the input. Obviously more work is needed - but I haven't time at the moment. This will give you a working template. Note that this C++20 code.

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
#include <iostream>
#include <fstream>
#include <vector>
#include <string>

using namespace std;

struct Movie {
	string name;
	int seats {};
	double hours {};
	double price {};
};

istream& operator>>(istream& is, Movie& mv) {
	char delim {};

	getline(is, mv.name, '|') && is >> mv.seats && is >> delim && is >> mv.hours >> delim && is >> mv.price >> ws;
	return is;
}

ostream& operator<<(ostream& os, const Movie& mv) {
	return os << mv.name << "  " << mv.seats << "  " << mv.hours << "  " << mv.price;
}

void display_all(const vector<Movie>& mv)
{
	for (size_t i {}; const auto m : mv)
		cout << '(' << ++i << ") " << m << '\n';
}

int main() {
	ifstream movie("Movies.txt");

	if (!movie)
		return (std::cout << "Cannot open file\n"), 1;

	std::vector<Movie> movies;

	for (Movie mv; movie >> mv; movies.push_back(mv));

	int selection_role {};
	int selection_movie {};

	cout << " *************************************************************** \n";
	cout << " MOVIE TICKET RESERVATION SYSTEM (MTRS) \n";
	cout << " *************************************************************** \n";
	cout << "\n\n Please specify the selection : \n (1) CUSTOMER\n";
	cout << " (2) MANAGEMENT\n\n";
	cout << " INPUT : ";
	cin >> selection_role;

	if (selection_role == 1) {
		cout << "\nMOVIES AVAILABLE : \n";
		display_all(movies);

		cout << "\nPlease select a movie: ";
		cin >> selection_movie;

		const auto& selmv {movies[selection_movie - 1]};

		cout << "\n======You have selected " << selmv.name << ".=========\n";
		cout << "\nMovie hours: " << selmv.hours << '\n';
		cout << "Number of seats available: " << selmv.seats << '\n';
		cout << " SCREEN \n\n";
		cout << "1 2 3 4 5 6 7 8\n\n";
		cout << "9 10 11 12 13 14 15 16\n\n";
		cout << "17 18 19 20 21 22 23 24\n\n";
		cout << "25 26 27 28 29 30 31 32\n\n";

		int seats1 {};
		string customer, phone_number;

		cout << "Enter the number of seat you would like to book for: ";
		cin >> seats1;

		cout << "\nPrice for the ticket: RM " << selmv.price << "\n\n";
		cout << "Enter you name: ";
		cin >> customer;

		cout << "Enter your phone number: ";
		cin >> phone_number;

		cout << "\n MOVIE TICKET \n";
		cout << "============================\n";
		cout << "Name :" << customer << '\n';
		cout << "Handphone number :" << phone_number << '\n';
		cout << "Movie selected :" << selmv.name << '\n';
		//cout << "Seat selected :" << seats1 << "," << seats2 << endl;
		cout << "Price for the movie(RM):" << selmv.price << endl;
	}
}


If you can't get this to compile as C++20 and the error is line 28, put the i variable outside the loop.
Last edited on
> I am a beginner in C++ programming.
You need to slow down!

Specifically, you need to learn how to break a problem down into manageable steps which you can complete separately. It's a skill you'll need, so best get some practice in now while the problems are relatively small.

The big bang approach of write all the code and then try to compile it doesn't work.

A good place to start is making sure you can actually read the file correctly. Until you can, the rest of the code is meaningless, it will only end up processing garbage data and confusing 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
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
  ifstream movies("foo.txt");
  char name [25];
  char hours[25];
  char seats[25];
  char price[25];
  while ( movies.getline(name,25,'|') &&
          movies.getline(seats,25,'|') &&
          movies.getline(hours,25,'|') &&
          movies.getline(price,25) ) {
    cout << "Name=" << name
         << ", seats=" << seats
         << ", hours=" << hours
         << ", price=" << price
         << endl;
  }

  return 0;
}

$ ./a.out 
Name=Avengers, seats=1600, hours=40, price=16
Name=LoST World, seats=3200, hours=40, price=16
Name=Titanic, seats=6897, hours=87, price=16
Name=Avengers, seats=1600, hours=40, price=16


Your next step?
Maybe a class and a vector as seeplus shows.
Your test to to make sure the vector is populated correctly.

Then you can start adding some of the user interface. If your menu has 5 options, then it's 5 times around the write / compile / debug / test loop.


Hello Mathavan,

You either missed the closing "code" tag or did something wrong. Have a look at:
http://www.cplusplus.com/articles/z13hAqkS/

I think that knowing what is wrong with you posted coed is useful in knowing what needs fixed. The comments in the code should help.
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
#include <iostream>
#include <iomanip>  // <--- Added.
#include <string>   // <--- Needed for the "std::string" and for some of the "cout" statements.
//#include <cmath>  // <--- I see no use for this at this time.

#include <fstream>

using namespace std;

constexpr int MAXSIZE{ 25 };  // <--- You might want this as 26 for the arrays and another variable for for other places where you only want 25 characters in an array. The 26 gives you 1 more for the (\0) to mark the end of the string.

display_all()  // <--- Needs a return type.
{
    char name[MAXSIZE]{};
    char hours[MAXSIZE]{};
    char seats[MAXSIZE]{};

    //fstream movie;
    //movie.open("Movies.txt", ios::in);

    // <--- But how do you know that it is open and usable?

    int count = 0;
    string line;

    ifstream movie("Movies.txt");  // <--- The more preferred way to define and open a file stream.
    // <--- But how do you know that it is open and usable?

    while (getline(movie, line))
        count++;

    movie.clear();
    movie.seekg(0);

    int i = 0;

    //while (movie.getline(name,MAXSIZE, '|') && movie.getline(seats, MAXSIZE, '|') && movie.getline(hours, MAXSIZE))
    //while (movie.getline(name,MAXSIZE, '|'))
    //while (!movie.eof() && i++ && i <= count)
    while (!movie.eof(), i++, i <= count)  // <--- Does not work the way that you are thinking. Esepically the ".eof()".
    {
        movie.getline(name, MAXSIZE, '|');  // <--- Delete line(s) used in the while loop.
        movie.getline(seats, MAXSIZE, '|');
        movie.getline(hours, MAXSIZE);

        cout << "(" << i << ") " << name << "\n ";

        //i++;  // <--- Do here. Not in while condition.
    }

    return 0;
}

main()  // <--- Needs a return type. Normally an "int".
{
    char name[MAXSIZE]{};  // <--- All arrays would work better as a std::string.
    char hours[MAXSIZE]{};
    char seats[MAXSIZE]{};
    char price[MAXSIZE]{};
    char seats1[MAXSIZE]{};
    char seats2[MAXSIZE]{};
    char seats3[MAXSIZE]{};
    char seats4[MAXSIZE]{};
    char seats5[MAXSIZE]{};

    char response = 'Y';

    string customer, phone_number;

    fstream movie;
    movie.open("Movies.txt", ios::in);

    char selection_role;
    int selection_movie;

    cout << 
        " ***************************************************************\n"
        "             MOVIE TICKET RESERVATION SYSTEM (MTRS)\n"
        " ***************************************************************\n\n\n";
    //cout << endl << endl;

    cout << 
        " Please specify the selection:\n"
        " (1) CUSTOMER\n"
        " (2) MANAGEMENT\n\n";

    //cout << endl;

    cout << " INPUT: ";
    cin >> selection_role;

    if (selection_role == 1);  // <--- The "if" statement ends with the (;) and does nothing.

    cout << endl;
    cout << "MOVIES AVAILABLE:\n ";  // <--- Changed.

    display_all();

    //cout << '\n';
    cout << "\nPlease select a movie: ";
    cin >> selection_movie;  // <--- I believe this value is 1 more than it should be.

    int i = 0;

    while (i++, i <= selection_movie)  // <--- May not work the way that you are thinking.
    {
        movie.getline(name, MAXSIZE, '|');
        movie.getline(seats, MAXSIZE, '|');
        movie.getline(hours, MAXSIZE, '|');
        movie.getline(price, MAXSIZE, '|');  // <--- Do not need the 3rd parameter here. Gives the variable the wrong number.
    }

    //cout << endl;
    cout << "\n======You have selected " << name << ".=========\n";  // <--- Changed.

    cout << endl;
    cout << "Movie hours: " << seats << endl;
    cout << "Number of seats available: " << hours << endl;
    cout << " SCREEN " << endl << endl;
    cout << " 1  2  3  4  5  6  7  8" << endl << endl;  // <--- Changed. Added a leading space on the single digits for a better display.
    cout << " 9 10 11 12 13 14 15 16" << endl << endl;
    cout << "17 18 19 20 21 22 23 24" << endl << endl;;
    cout << "25 26 27 28 29 30 31 32" << endl << endl;

    cout << "Enter the number of seat you would like to book for: ";
    cin >> seats1;

    cout << endl;
    cout << "Price for the ticket: RM " << price << endl << endl;
    cout << "Enter you name: ";
    cin >> customer;
    cout << "Enter your phone number: ";
    cin >> phone_number;

    cout << endl;
    cout << " MOVIE TICKET " << endl;
    cout << "============================" << endl;
    cout << "Name :" << customer << endl;
    cout << "Handphone number :" << phone_number << endl;
    cout << "Movie selected :" << name << endl;
    cout << "Seat selected :" << seats1 << "," << seats2 << endl;  // <--- No value for "seat2".
    cout << "Price for the movie(RM):" << price << endl;

    return 0;  // <--- Not required, but makes a good break point.
}

With the changes I made I did get this output:

 ***************************************************************
             MOVIE TICKET RESERVATION SYSTEM (MTRS)
 ***************************************************************


 Please specify the selection:
 (1) CUSTOMER
 (2) MANAGEMENT

 INPUT: 2

MOVIES AVAILABLE:
 (1) Avengers
 (2) LoST World
 (3) Titanic
 (4) Avengers

Please select a movie: 2

======You have selected 3200.=========

Movie hours: 40
Number of seats available: 16
Titanic
 SCREEN

 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

Enter the number of seat you would like to book for: 19

Price for the ticket: RM 6897

Enter you name: Andy
Enter your phone number: 6145551234

 MOVIE TICKET
============================
Name :Andy
Handphone number :6145551234
Movie selected :3200
Seat selected :19,╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠19
Price for the movie(RM):6897


As you can see you are part way there, but not quite right.

You should initialize all your variables when defined just to make sure they do not contain a garbage value.

I do agree with salem c that you should work in small steps.

I would start with a new source file, but to not delete the original. There is still code there worth using.

Since you have demonstrated that you can use functions, even if they are not complete, I would suggest a function that only reads the file and I would make use of a struct like seeplus has shown. Followed by saving this struct in an array or a vector would be better if you can use a vector.

All your opening and reading the file is unnecessary. You only need to do this once unless the assingnent calls for something different. Since you did not post what you were given there is still a lot of guess work.

Once you have read the file and saved the information into the program the red becomes much easier to write. Until you have something to work with you are just wasting your time.

Andy
A further re-factoring of the code to give a proper menu, available seats changed, tests for bad input, split into functions etc etc gives:

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
#include <iostream>
#include <fstream>
#include <vector>
#include <string>

using namespace std;

struct Movie {
	string name;
	int seats {};
	double hours {};
	double price {};
};

istream& operator>>(istream& is, Movie& mv) {
	char delim {};

	getline(is, mv.name, '|') && is >> mv.seats && is >> delim && is >> mv.hours >> delim && is >> mv.price >> ws;
	return is;
}

ostream& operator<<(ostream& os, const Movie& mv) {
	return os << mv.name << "  " << mv.seats << "  " << mv.hours << "  " << mv.price;
}

void display_all(const vector<Movie>& mv)
{
	for (size_t i {}; const auto m : mv)
		cout << '(' << ++i << ") " << m << '\n';
}

void Ticket(vector<Movie>& movies)
{
	int selection_movie {};

	cout << "\nMOVIES AVAILABLE : \n";
	display_all(movies);

	cout << "\nPlease select a movie: ";
	cin >> selection_movie;

	if (selection_movie >= 1 && selection_movie <= movies.size()) {
		auto& selmv {movies[selection_movie - 1]};

		cout << "\n======You have selected " << selmv.name << ".=========\n";
		cout << "\nMovie hours: " << selmv.hours << '\n';
		cout << "Number of seats available: " << selmv.seats << '\n';

		if (selmv.seats > 0) {
			int seats {};
			string customer, phone_number;

			cout << "Enter the number of seats you would like to book for: ";
			cin >> seats;

			if ((seats > 0) && seats <= selmv.seats) {
				cout << "\nPrice per seat: " << selmv.price << '\n';

				cout << "Enter you name: ";
				cin >> customer;

				cout << "Enter your phone number: ";
				cin >> phone_number;

				cout << "\n\n MOVIE TICKET \n";
				cout << "============================\n";
				cout << "Name :" << customer << '\n';
				cout << "Phone number: " << phone_number << '\n';
				cout << "Movie selected: " << selmv.name << '\n';
				cout << "Seat(s) required: " << seats << '\n';
				cout << "Price for the movie: " << selmv.price * seats << '\n';

				selmv.seats -= seats;
			} else
				cout << "Only " << selmv.seats << " available\n";
		} else
			cout << "No seats available\n";
	} else
		cout << "Invalid movie!\n";
}

vector<Movie> read()
{
	vector<Movie> movies;
	ifstream movie("Movies.txt");

	for (Movie mv; movie >> mv; movies.push_back(mv));

	return movies;
}

int main()
{
	auto movies {read()};

	if (movies.empty())
		return (cout << "Problem obtaining movie data\n"), 1;

	do {
		cout << "\n *************************************************************** \n";
		cout << " MOVIE TICKET RESERVATION SYSTEM (MTRS) \n";
		cout << " *************************************************************** \n\n";
		cout << " (1) CUSTOMER\n";
		cout << " (2) MANAGEMENT\n";
		cout << " (0) Exit\n\n";
		cout << " Please specify the selection: ";

		int selection_role {};
		cin >> selection_role;

		switch (selection_role) {
			case 1:
				Ticket(movies);
				break;

			case 2:
				cout << "(To do)\n";
				break;

			case 0:
				return 0;

			default:
				cout << "Invalid option\n";
				break;
		}
	} while (true);
}

Last edited on
Topic archived. No new replies allowed.