Change your main program to be dealing with files instead of console interaction
ID: 3677909 • Letter: C
Question
Change your main program to be dealing with files instead of console interaction:
Set up a file called "inputs.dat" in the same directory as your program formatted with one pair of rectangular coordinates per line. Give the x coordinate first, then whitespace (a single space or multiple spaces or a tab will all work the same), and then the y coordinate. Use one of the sets of test data from last week where you know what results to expect.
Remove the interactive input phase from your main program. Instead, change your program to read from any file called "inputs.dat" in the program's working directory and formatted as above and store the data in paraellel arrays. You must handle any file size up to 1000 lines. You may not assume any particular size of file, nor may you determine the file size in advance; you must read until the end of the file.
Additionally, we only want to be working in the first quadrant, so we should validate our input. Check every input you read in to make sure it's in the first quadrant and save only those that are. For those that are not, print to the console an error message reporting the bad input, e.g. "Read point (-20, 7), which is not in Quadrant I...ignoring."
When reading in data, the user doesn't really have any indication what's going on, and with long files, it might take a while. So, after reading every 10th data point, print a status message like "Successfully read 10 lines of input so far," "Successfully read 20 lines of input so far," etc.
You may assume your input file is properly formatted, but you may not assume it exists. If it does not, tell the user, and make sure the rest of your program doesn't do anything weird. (Do not, under any circumstances, prematurely terminate the program.)
We read in from a file, so why not send results to a file. Produce an output file called "outputs.dat" in the program's working directory formatted similarly to the input file, with r first on each line.
The output file doesn't show anything in the console, but give the user a message saying where to find the output.
Have a program setting that determines whether or not to give the console output you programmed last week. It should be something that must able to be turned on or off via setting a constant at compiletime.
Explanation / Answer
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
double first;
double second;
int count = 0;
// creating input and output file object
ifstream inFile("inputs.dat");
ofstream outFile("outputs.dat");
while (inFile >> first >> second) {
count++;
if(first < 0 || second <0){
cout<<"("<<first<<", "<<second<<") is not in QuadrentI..ignoring"<<endl;
}
else{
outFile<<first<<" "<<second<<endl;
}
if(count!=0 && (count % 10 == 0))
cout<<count<<" records read successfully"<<endl;
}
cout<<count<<" records read successfully"<<endl;
inFile.close();
outFile.close();
return 0;
}
/*
Input: inputs.dat
3 4
4 -4
5 5
1 2
3 2
1 6
2 1
6 7
1 -2
-3 2
5 5
-2 -2
Output:
(4, -4) is not in QuadrentI..ignoring
(1, -2) is not in QuadrentI..ignoring
(-3, 2) is not in QuadrentI..ignoring
10 records read successfully
(-2, -2) is not in QuadrentI..ignoring
12 records read successfully
outputs.dat:
3 4
5 5
1 2
3 2
1 6
2 1
6 7
5 5
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.