Код Endless записывает первую строку текстового файла - PullRequest
1 голос
/ 09 октября 2019

Ниже приведен код, с которым я работаю, предполагается, что он считывает информацию из данных о зарплате, которая содержит имена и текущую зарплату, а также процентное увеличение, а затем записывает новую информацию в файл NewData. Хорошо, он читает первую строку и снова и снова переписывает в новый файл. Он должен уметь читать каждую строку и записывать каждую строку в файл зарплаты.

/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
This Program does two things, first it reads data from a file, and second
it formats number output...

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */


#include<iostream>
#include<fstream>
#include<string>
#include <iostream>     // std::cout, std::fixed
#include <iomanip>      // std::setprecision
using namespace std;

int main()
{
// need an input file variable reference
ifstream fin;
ofstream myfile;

string lastName, firstName, str;
double salary, newSalary, percentIncrease;


// Output File

    // Place the directory to the file you want to write to here.
myfile.open("C:/Users/Adam/Desktop/NewData.txt");

// What is being wrote to the output file.
myfile << "Writing this to a file.\n";


// Open the file named numbers.txt
fin.open("C:/Users/Adam/Desktop/SalaryData.txt");    // catch, file has 
to be in the same folder as the .cpp

// OR

// use a EOF While loop to loop through an input file
// to be safe, always PRIME your EOF While loops



fin >> lastName >> firstName >> salary >> newSalary >> percentIncrease;           
// reads first eligible item (in this case, an integer) from the file
while (! fin.eof()) {

    newSalary = salary * (percentIncrease / 100) + salary;

    cout << lastName << firstName << newSalary;
    myfile << lastName << firstName << newSalary;
    fin >> lastName >> firstName >> salary >> newSalary;

    }

cout << endl;


    cin.get();

    return 0;
}

1 Ответ

1 голос
/ 09 октября 2019

Вместо

fin >> lastName >> firstName >> salary >> newSalary >> percentIncrease;           
while (! fin.eof()) {
    // do your stuff
    fin >> lastName >> firstName >> salary >> newSalary;
}

попробуйте поместить fin >> в цикл while

while( fin >> lastName >> firstName >> salary >> newSalary >> percentIncrease ) {
   // do your stuff
}

Потому что fin >> может вызвать eofbit в одном >> call и badbit в следующем>> звоните.

...