I NEED HELP PLEASE C++ Write a C++ program using a WHILE loop which reads a data
ID: 671782 • Letter: I
Question
I NEED HELP PLEASE C++
Write a C++ program using a WHILE loop which reads a data file that you create using the information given below.
There will three variations of the file as follows. There should be ten value entries (refer to the verification file below).
File 1; Place a return keystroke immediately after the number final value with no space. Also, do not insert periods or commas in the file.
File 2; place a space after the final value and then a return.
File 3; end the file with neither a space nor a return. The cursor should remain immediately after the final value.
Create the data files using the filenames pa6input1.dat, pa6input2.dat, and pa6input3.dat respectively.
Sum all data items in the file and print the total to the screen.
Process all three files in this manner.
After each file is read, the output should be checked for proper results. Note: proper sum = 55 for the verification file. The data file content should be output to the screen during the loop for each part.
Both parts will be located sequentially in main().
Verification data for the three files will be as follows:
1 2 3 4 5 6 7 8 9 10
This is numeric data and should not be read as a character string. Place a space between each number preceding the final number.
Describe your results in a comment block at the end of the program and state your conclusions about the file reading methods.
Explanation / Answer
Create a .dat files
-------------------
pa6input1.dat
--------------
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int main() {
fstream inOutGrades( "pa6input1.dat", ios::in | ios::out | ios::binary );
if ( !inOutGrades ) {
cerr << "The file could not be opened." << endl;
exit(1);
}
return 0;
}
pa6input2.dat
--------------
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int main() {
fstream inOutGrades( "pa6input2.dat", ios::in | ios::out | ios::binary );
if ( !inOutGrades ) {
cerr << "The file could not be opened." << endl;
exit(1);
}
return 0;
}
pa6input3.dat
--------------
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int main() {
fstream inOutGrades( "pa6input3.dat", ios::in | ios::out | ios::binary );
if ( !inOutGrades ) {
cerr << "The file could not be opened." << endl;
exit(1);
}
return 0;
}
Counting Number of words in file
--------------------------------
ifstream infile;
//char mystring[6];
//char mystring[20];
int main()
{
infile.open("file.txt");
if(infile.fail())
{
cout << " Error " << endl;
}
int numb_char=0;
char letter;
while(!infile.eof())
{
infile.get(letter);
cout << letter;
numb_char++;
}
cout << " the number of characters is :" << numb_char << endl;
infile.close();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.