I need help solving this algorithm. Thanks Data File DB Create an inputfile name
ID: 3564451 • Letter: I
Question
I need help solving this algorithm.
Thanks
Data File DB
Create an inputfile named "inputFile.txt" (use "Note Pad") that contains the following data. Be sure that the last line of the file is empty (i.e., not even a space character). In other words, after entering the second line of data, press the <ENTER> key, save the file, then exit the editor.
5 10 6 9
23 7 13 40
Implement the following algorithm
Create & Open inputFile
Create & Open outputFile
Read first line of data into variables a, b, c, d
DOWHILE !EOF
Create a unique formula that uses the variables a, b, c, & d. For example,
value = (a*a) + b - c + d
Output value into outputFile
Read each line of data into variables a, b, c, d
ENDDO
Close inputFile
Close outputFile
Post the source code (embed the code in your response; DO NOT ATTACH THE C++ FILE).
Explanation / Answer
Here is Your program That you need
#include <iostream>
#include <fstream>
#include <assert.h>
using namespace std;
int main()
{
ifstream file("inputFile.txt");
int a,b,c,d,value;
assert (!file.fail( ));
do
{
file >> a;
file >> b;
file >> c;
file >> d;
value = (a*a) + b - c + d;
ofstream file1("outputFile.txt",ios::out | ios::app );
if (file1.is_open())
{
file1 << value;
file1 << " ";
file1.close();
}
cout<<a<<" "<<b<<" "<<c<<" "<<d<<" "<<value<<endl;
}while(!file.eof());
file.close();
return 0;
}
Output:-
Original :-
5 10 6 9 38
23 7 13 40 563
My Output:
5 10 6 9 38
23 7 13 40 563
23 7 13 40 563
because of ' ' character at the end of line. we use eof so it will take 1 extra character.
Don't ever use .eof() or .good() as loop condition. It almost always produces buggy code (as it does in this case);
So There is Only Single solution with do, while and eof in this case is to remove that extra space from file.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.