#include <iostream>
#include <string>
#include <fstream>
using namespace std;
//code to register, display and delete a constituency for election using file handling
fstream constituencies("constituencies.txt", ios::out | ios::in);
fstream temp("temp.txt", ios::out | ios::in);
/* declaring files for constituencies registration and when to delete, a temp file to copy the previous data into it and later rename it as constituencies*/
void registerConstituency() // function to register constituency
{
// Proccess for constituency
string consti;
cout << "\n\nEnter unique constituency\n";
getline(cin, consti);
string get_file;
static bool flag = false;
/* flag to detect whether the constituency to be registered is already registered or not */
// searching file if entry by user already exists or not through while loop
while (getline(constituencies, get_file)) {
if (get_file == consti) {
flag = true;
break;
}
}
if (flag == true) {
cout << "\nConstituency already exists, try another one\n";
}
else {
ofstream constituencies("constituencies.txt", ios::app);
constituencies << consti << endl;
cout << "Your Constituency has been registered\n";
constituencies.close();
}
}
void displayConstituency()
{
string get_file;
while (getline(constituencies, get_file))
cout << get_file << endl;
}
void updateOrDelete()
{
string deleteline;
string line;
ofstream temp("temp.txt", ios::app);
cout << "Which constituency do you want to remove? \n";
cin >> deleteline;
while (getline(constituencies, line)) {
if (line != deleteline) {
temp << line << endl;
}
}
remove("constituencies.txt");
rename("temp.txt", "constituencies.txt");
}
void menu()
{
cout << "Select an option:" << endl
<< endl;
cout << "1) Register a national assembly constituency (e.g. NA-1)\n";
cout << "2) List all constituencies\n";
cout << "3) Update/Delete Constituencies \n";
int option;
cout << "option: ";
cin >> option;
switch (option) {
case 1:
registerConstituency();
break;
case 2:
displayConstituency();
break;
case 3:
updateOrDelete();
break;
default: // Invalid option
cout << "INVALID OPTION!!" << endl;
break;
}
}
int main()
{
for (;;) {
menu();
int exit;
cout << "\nENTER ANY NUMBER TO EXIT\n";
cin >> exit;
system("CLS");
}
}
Asked
Active
Viewed 127 times
0

drescherjm
- 10,365
- 5
- 44
- 64

Danyal Ali
- 5
- 3
-
Please expand the issue. What have you tried? What is the expected behavior? What is the result? – Uberhumus Jun 02 '20 at 06:41
-
You should not have the global files and you should not have the same file open more than once. – drescherjm Jun 02 '20 at 17:23
1 Answers
0
First of all, you need to add fflush(stdin)
before getline(cin, consti)
in the first defined function, since it's skipping to ask for an input.
And now, let's solve the main problem.
The problem was, the file content in second time execution didn't show up anything.
The reason is: Even after not reading, it'll go to end of file of that file after while(getline())
statement.
To solve that, jump to the second function void displayConstituency()
:
void displayConstituency()
{
string file = "";
while (getline(constituencies, file))
cout << file << endl;
// goes end of file and never jumps to beginning for next execution
}
Rather than that, do this:
void displayConstituency()
{
string file = "";
constituencies.clear(); // this
constituencies.seekg(0, ios::beg); // this
while (getline(constituencies, file))
cout << file << endl;
}
It clears the EOF and the second lines seeks to beginning of the file again for future use.

Rohan Bari
- 7,482
- 3
- 14
- 34
-
Thank you, the content of the file are being shown but now the updateOrDelete() function does not delete the constituency / (line in the file) – Danyal Ali Jun 02 '20 at 11:56
-
See [Using fflush(stdin)](https://stackoverflow.com/a/2979217/10871073). – Adrian Mole Jun 02 '20 at 15:15