-3

I got a task that is: Write a simple program that can be used to delete data on one one line specified in a file with the following steps:  Manually create a file containing:

i.  Fill from line 1
ii. Fill from line 2
ii. Fill from line 3
iv. Fill in line 4

 Display the entire contents of the file.  Appears the choice of how many rows to delete.  Delete data in the selected row.  Display the entire contents of the file.

I have created the program as below:

#include <iostream>
#include <string>
#include <fstream>
using namespace std;
// Function Prototipe
void garis();
void show_databefore();
void show_data();
void del_line(const char *file_name, int n);
// Variable Declaration
int n;
ifstream myFile;
string output,buffer;
// Main Function
int main(){
    cout << "Show data and delete specific line Program " << endl;
    garis ();
    cout << "The Data " << endl;
    show_databefore();
    garis();
    
    cout << "Select the Rows of data you want to delete: ";
    cin >> n;
    cout << "\nBefore " << endl;
    // If Case after input n
    if((0 < n) && (n < 100)){
        del_line("data1.txt", n);  // this process will delete the row that has been selected.
    } else {
        cout << "Error" << endl;} 
    show_data(); // when calling this function. Display on the console, data is displayed 2 times.
    return 0;
}


//Function to delete data in the row that has been selected
void del_line(const char *file_name, int n){    
  ifstream fin(file_name);    
  ofstream fout;                
  fout.open("temp.txt", ios::out); 
  
  char ch; 
  int line = 1;            
  while(fin.get(ch)) 
  {      
    if(ch == '\n') 
      line++; 
     
    if(line != n)      
      fout<<ch; 
  } 

  fout.close();  
  fin.close();   

  remove(file_name);        
  rename("temp.txt", file_name);  
} 

// Function to display data1.txt to the console
void show_databefore(){
    myFile.open("data1.txt");
    while (!myFile.eof()){
        getline(myFile,buffer);
        output.append("\n" + buffer); 
    }
    cout << output << endl;
    myFile.close(); 
}
// Function to display data1.txt to the console  T
// This fuction actually same as show_databefore. 
void show_data(){
    myFile.open("data1.txt");
    while (!myFile.eof()){
        getline(myFile,buffer);
        output.append("\n" + buffer); 
    }
    cout << output;
    myFile.close(); 
}
//Function to provide a boundary line.
void garis(){
    cout << "======================================================= " << endl << endl;
}

When I run my program it works, but when I bring up data to the console, my data appears 2 times and I've tried the method without using the function. However, the result remains the same.

Here is the result

Can anyone help me? Or maybe there's an easier way to build my program? Thank you

SKAR
  • 3
  • 2
  • `while (!myFile.eof())` Where does this error come from? Why do so many beginners write code like this? – john Dec 03 '22 at 15:42
  • Besides that this is a duplicate [Please do not upload images of code/data/errors.](//meta.stackoverflow.com/q/285551) – cafce25 Dec 03 '22 at 15:45
  • while (!myFile.eof()) is not an error. The error is in the image I attached – SKAR Dec 03 '22 at 15:53
  • @SKAR *while (!myFile.eof()) is not an error* -- Ok, so where in *the code* is the error? And note -- we have no way to duplicate your program without a file to read. – PaulMcKenzie Dec 03 '22 at 15:57
  • @PaulMcKenzie Oke, Im sorry i havent add note in my code. I will add the note. – SKAR Dec 03 '22 at 16:00
  • 1
    @SKAR That code is an error as the duplicate explains, and as reading how eof actually works would also explain. It's the persistence of this error that baffles me. – john Dec 03 '22 at 16:03
  • @SKAR BTW the correct code (in case it's not clear) is `while (getline(myFile,buffer))`. Maybe you should try it? – john Dec 03 '22 at 16:06
  • @SKAR -- The point I am making is that if you are so sure that isn't an error, then you would have some indication of where the error is, but nowhere in your post do you give any indication of where *you* think the error is. Did you actually try the code that others mentioned? – PaulMcKenzie Dec 03 '22 at 16:07
  • @SKAR OK I can actually see the error you are complaining about now. The problem is that `output` is a global variable, and when you call `show_data` the variable **is not empty**. Simple solution declare the variable `output` in the functions where you use it, instead of as a global variable. – john Dec 03 '22 at 16:11
  • Reopened because the problem the OP is asking about has nothing to do with end of file processing. Instead the bug is caused by global variable use. – john Dec 03 '22 at 16:12
  • @SKAR But of course that's not to say that you don't have a bug in your end of file handling, – john Dec 03 '22 at 16:14
  • I have tried @john suggestion but the result in the consol like this Fill from line 1 ii. Fill from line 2 ii. Fill from line 3 iv. Fill in line 4 ii. Fill from line 3 iv. Fill in line 4 – SKAR Dec 03 '22 at 16:14
  • @SKAR Yes I've found the actual bug you are complaining about, see below. Sorry I was initially confused since the eof bug also causes duplicate output. – john Dec 03 '22 at 16:22
  • @john its okay. Thank you for your help. I also apologize because the explanation about my error is not clear. I'm a beginner and sometimes it's hard to explain my problem. – SKAR Dec 03 '22 at 16:28

1 Answers1

0

This code is bugged in two different ways

void show_data(){
    myFile.open("data1.txt");
    while (!myFile.eof()){
        getline(myFile,buffer);
        output.append("\n" + buffer); 
    }
    cout << output;
    myFile.close(); 
}

The first bug is the incorrect use of eof. The second bug is the use of the global variable output. The code assumes that the variable is an empty string when the function is entered, but because it's a global variable there is no way to ensure this. Here's a fixed version, the global variable is now a local variable and the eof problem has been fixed.

void show_data(){
    string output; // local variable
    myFile.open("data1.txt");
    while (getline(myFile,buffer)){
        output.append("\n" + buffer); 
    }
    cout << output;
    myFile.close(); 
}

Don't needlessly use global variables, they are a huge source of bugs, as well as many other problems. There are several others in your code. They should all be removed.

john
  • 85,011
  • 4
  • 57
  • 81
  • Oke thank you @john. I think your suggestion in the comment before is right. The problem is that output is a global variable. So i follow your suggestion and it works. – SKAR Dec 03 '22 at 16:22