Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For this project, you will read input from 2 sources (from the keyboard and from

ID: 3566927 • Letter: F

Question

For this project, you will read input from 2 sources (from the keyboard and from a file) as well as get more experience writing a complex function. You will read numbers from a file and 1) print the ith number [The user enters i.] and 2) print the biggest and smallest numbers in the file. You must use a function with reference parameters to do this, which is why the requirements given below specify what code must reside in main() and what code must reside in the processFile() function.

Getting Started:

To begin with, copy the code I provided to read data from a file and find the smallest number (simpleExample.cpp from Week 6) This is posted at the very bottom of this page. As for the previous assignment, make the code prompt the user for an input filename and check that the file opend OK. Then, for this assignment, you need to modify the code to prompt the user to input a value that is used as the position of the number in the file to print. For example, if the user enters 100, then you will print the 100th number as part of the output. If the user enters a number greater that the number of values in the file, an error message is printed (see below).

Further, the code determines both the minimum and the maximum values of all of the values provided in the file. All 3 of these values (max, min, and the ith value) are to be determined inside the processFile() function and returned to the main() function via reference parameters. Using reference parameters is actually the point of this assignment.

Further Requirements:

int processFile(ifstream& ifile, long i, long& retIthVal, long& minimum, long& maximum);

Sample Output

Here are 2 sample outputs so that you know what to cout. User input is in blue.

Sample 1 (There are 21 other numbers before 44.):

Input File Name: input.txt

Which number do you want to return? 22

Min is {1}.

Max is {5440245}.

Value 22 is {44}.

Sample 2 (There are less than 111 numbers in the file.):

Input File Name: anotherInput.dat

Which number do you want to return? 111

Min is {-111}.

Max is {544245}.

There aren't that many numbers in the file!

Sample 3 (empty file):

Input File Name: empty.dat

Which number do you want to return? 111

The file is empty!

Sample 4 (no file):

Input File Name: Input.dat

That file does not exist!

Sample Code:

#include <iostream> // Example Finding a Min, Reading a File
#include <fstream>
#include <string>
using namespace std;
int pay = 999;
int main()
{
   int min = INT_MAX;
   int num;
   string strVar;
   ifstream myInfile;
  
   cout << "Enter a file name to read from: ";
   cin >> strVar;
   myInfile.open (strVar.c_str()) ;
  
   if ( ! myInfile) // the ! means myInfile is non 0, indicating it is OK
   {
       //       someFunct();
       cout << "Error ";
       return 1;
   }
  
   myInfile >> num; // priming read
   // myInfile here means

Explanation / Answer

#include #include #include using namespace std; int processFile(ifstream& ifile, long i, long& retIthVal, long& minimum, long& maximum); int main() { long i, retIthVal, minimum, maximum; int returnvalue=1; ifstream ifile; //variable for myfile string strVar; //variable for textfile cout > strVar; //inputs textfile into string ifile.open(strVar.c_str()); if (!ifile) // the ! means myInfile is non 0, indicating it is OK { cout i; returnvalue=processFile(ifile, i, retIthVal, minimum, maximum); if(returnvalue==0) { cout
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote