Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions