My professor assigned us a project that requires us to use "eof" (quotation marks for emphasis) in a "while" loop. I have done quite a bit of research and realize why I have the issue I will relate in a bit. However, using "eof" is part of what will determine my grade, meaning that even though I am aware that I could achieve what I need using other code, I must use eof in this case.
I also understand that there are other more elegant, and effective, ways to write the program that will accomplish what I need, but that is a battle for another day. I need to focus on my issue with "eof" for now.
When using "eof", I have noticed that my program outputs the last set of data twice. As I understand, the loop checks for eof only after it reads the file, which causes it to repeat the last set of data.
What I need to accomplish is to use "eof" without repeating the last set of data twice in order to accomplish what my professor expects.
The code I wrote follows below.
Thanks in advance for your assistance.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
|
#include <iostream>
#include <fstream> // File stream (output and input to a file).
#include <iomanip> // Output and input manipulation.
using namespace std;
int main()
{
// char variables will be set to up to 20 characters.
ifstream inData; // Variable that will store input file stream data.
ofstream outData; // Variable that will store output file stream data.
char exit; //Variable that will be used to terminate program.
char fiName [20]; // Variable that will store the name of the file the user creates.
char firstName [20]; // Variable that will store the first names.
char lastName [20]; // Variable that will store last names.
double gradeA, gradeB, gradeC, gradeD, gradeE; //variables that will store grades.
double average; // Variable that will be used to calculate average grade.
int roundedAverage; // Variable that will be used to round calculated average to an integer.
int counter1; // Variable to start for loop that will request user to input data 5 times.
int counter2=0; // Variable to start while loop that will check user for invalid input.
// It is set to begin at 0 to allow for 3 attempts.
// Request user input file name to create file.
cout << "\n Please enter a file name to store information and press enter: ";
cin >> fiName;
cout << "\n You set your file name to: " << fiName << "\n";
outData.open (fiName); //Open file created by user to write into it.
//Request data from user.
cout << "\n\n Enter first name and press enter: ";
cin >> firstName;
cout <<"\n Enter last name and press enter: ";
cin >> lastName;
cout << "\n Enter five grades separated by a space and press enter: ";
cin >> gradeA >> gradeB >> gradeC >> gradeD >> gradeE;
// Write data entered by user into the file.
outData << firstName << " " << lastName << " ";
outData << gradeA << " " << gradeB << " " << gradeC << " " << gradeD << " " << gradeE << " ";
for (counter1=0;counter1<4;counter1++) //For loop to ask and store names and grades four more times.
{
//Request data from user.
cout << "\n\n Enter another first name and press enter: ";
cin >> firstName;
cout <<"\n Enter another last name and press enter: ";
cin >> lastName;
cout << "\n Enter five grades separated by a space and press enter: ";
cin >> gradeA >> gradeB >> gradeC >> gradeD >> gradeE;
// Write data entered by user into the file.
outData << firstName << " " << lastName << " ";
outData << gradeA << " " << gradeB << " " << gradeC << " " << gradeD << " " << gradeE << " ";
}
outData.close (); // Close file created by user once it contains all requested data.
// Request user to input file name to open file.
cout << "\n\n Enter file name to read and press enter: ";
cin >> fiName;
inData.open(fiName); //Open file created by user to read from it.
// While loop to check for incorrect file name.
// It will display an error message, up to three times, before terminating
// if the user conitnues to enter the wrong file name.
while (!inData&&counter2<2)
{
cout << "\n File not found." << endl;
cout << "\n Reenter file name: ";
cin >> fiName;
inData.open (fiName);
counter2++;
}
// if statement to terminate program in case user enters incorrect
// file name three times.
if (!inData)
{
cout << "\n File not found. Program Terminated.";
/*On my notes, a break; statement followed the statement above, but the
compiler told me that I could not use break with an if statement. My
solution to terminate the program at this point was to use the three
lines of code below.*/
cout << "\n\n To exit the program, enter any key and press enter: ";
cin >> exit;
}
while (!inData.eof())
{
// Read data entered by user from file in the same order it was stored.
inData >> firstName >> lastName >> gradeA >> gradeB >> gradeC >> gradeD >>gradeE;
cout << "\n Name: " << firstName << " " << lastName;
cout << "\n Grades: " << gradeA << ", " << gradeB << ", " << gradeC << ", ";
cout << gradeD << ", " << gradeE;
// Calculate and display average grade.
average=(gradeA+gradeB+gradeC+gradeD+gradeE)/5.0; //Calculate average grade.
cout << "\n " << firstName << " " << lastName << "'s" <<" average grade is: ";
cout << average; //sets output width to six characters.
roundedAverage=average+0.5; // Round average grade to an integer.
cout << "\n Rounded and final grade is: " << roundedAverage << "\n\n";
}
inData.close();
cout << "\n To exit the program, enter any key and press enter: ";
cin >> exit;
return 0;
}
|
I tried placing:
1 2
|
if (inData.eof())
break;
|
Just before the end of the loop, hoping it would check then and not repeat the last line, but it did not work.