Question: Write a complete C++ program in a project called 07_2_IsSorted that reads a number of integers from a text file named numbers.txt. The sentinel value
Write a complete C++ program in a project called 07_2_IsSorted that reads a number of integers from a text file named numbers.txt. The sentinel value is -999. The following data file has 12 values and a sentinel value of -999. 2 3 5 7 8 10 12 13 15 1 11 15 -999 Your program must print out to the screen each integer value (five values per line right justified in a field of 3). After outputting all values, output the number of integers read (excluding the sentinel value), the sum of all numbers, and SORTED if the values are in order from smallest to largest; otherwise, output NOT SORTED. HINT: Write and test the code to produce the Count, then the Sum, then the SORTED portion of the project.
*** Number Cruncher ***
2 3 5 7 8
10 12 13 15 1
11 15
Count: 12
Sum: 102
NOT SORTED
Testing: Test your input on these input files.
Test1.txt
1 2 3 4 5 6 7 8 -999
Test2.txt
2 3 5 7 8 10 12 13 15 1 11 15 -999
Test3.txt
-999
Test4.txt
1 -999
#include
#include
#include
#include
using namespace std;
int main() {
const string FILE_NAME = "numbers.txt";
fstream inFile;
fstream outFile;
string sentinel;
int count = 0;
string numFile;
cout << "Opening file \"" << FILE_NAME << "\"..." << endl;
inFile.open(FILE_NAME);
if (!inFile.is_open()) {
cout << "Could not open file \"" << FILE_NAME << "\"..." << endl;
system("pause");
return EXIT_FAILURE;
}
cout << "Opening file \"" << FILE_NAME << "\"..." << endl;
outFile.open(FILE_NAME);
if (!outFile.is_open()) {
cout << "Could not open file \"" << FILE_NAME << "\"..." << endl;
system("pause");
return EXIT_FAILURE;
}
outFile << setfill('*');
outFile << setw(3) << "" << "Number Cruncher" << setw(3) << "" << left << endl;
while (inFile.eof()) {
inFile >> numFile;
count++;
}
cout << "count:" << count << endl;
cout << "Closing file \"" << FILE_NAME << "\" ..." << endl << endl;
outFile.close();
cout << "Closing file \"" << FILE_NAME << "\" ..." << endl << endl;
inFile.close();
system("pause");
return EXIT_SUCCESS;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
