· Inside main(), you must prompt for a filename , open the input file , and prin
ID: 3554099 • Letter: #
Question
· Inside main(), you must prompt for a filename, open the input file, and print an error message if the file does not open properly.
· Also inside main(), you must prompt the user to enter a number (type long) and call the processFile() function to return the ith number in the file, where i is the number they entered. For example, if the user enters 11, then you must print the 11th number in the file. If this position number is greater than the number of numbers in the file, then print "There aren't that many numbers in the file!"
· You must use a function call with the following prototype: (please copy/paste this into your code)
intprocessFile(ifstream& ifile, long ithVal, long& retIthVal, long& minimum, long& maximum);
1. The first parameter indicates that you are passing the opened-file-variable, e.g. myInfile, as the first parameter! I use ifilehere because this is the formal parameter, not the actual function call.
2. ithVal is the formal parameter for the number the user entered.
3. retIthVal is the formal parameter thru which to return the ith value
4. minimum is the formal parameter thru which to return the minimum value
5. maximum is the formal parameter thru which to return the maximum value
6. The int return type must indicate whether the number was found (return 0) or not found in the file (return 1). Check this return value (in main() ) in order to print the "There aren't that many numbers..." message .
· You must read the data into the program in the processFile() function and process the data (check if it is min or max or the ith value) inside the function. (Start by creating a function with the above header, copying the while loop from example code in the slides into this function. Add code to find the ith number, max and min.)
· You must print your output from the main() function. You may not use cout in the processFile() function.
· Create your own data file and test your program so that you actually print the ith number. We will be using my input data file when you submit your solutions.
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 {544245}.
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 {1}.
Max is {544245}.
There aren't that many numbers in the file!
Explanation / Answer
#include <iostream>
using namespace std;
int main () {
char file[100]; // array created named file
long ithVal, retIthVal, min=0, max=0;
int status;
ifstream ifile;
cout << " Input File Name: ";
cin >> file;
ifile.open(file.c_str( )); //it is a function
if (ifile.fail())
{
cout << "Error! file does not open properly";
ifile.close();
}
else
{
cout<<" Which number do you want to return? ";
cin>> ithVal;
status= processFile(ifile, ithVal, retIthVal, minimum, maximum);
if(status==0)
{
cout<<" Min is {"<<minimum<<"} ";
cout<<"Max is {"<<maximum<<"{ ";
cout<<" Value{"<<ithVal<<"} is"<<retIthVal<<" ";
}
else
cout<<"invalid position";
}
ifile.close(); // fuction calling
return 0;
}
int processFile(ifstream& ifile, long ithVal, long& retIthVal, long& minimum, long& maximum)
{
long count=0, output;
if(ithVal==count)
return -1;
else
{
while (!ifile.eof()) {
min=1;
count++;
ifile >> output;
if(ithVal==count)
retIthVal=output;
}
max=count;
return 0;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.