I'm supposed to use infile.ignore(INT_MAX, '\n') in the code, but I don't know exactly where it's supposed to be. Also, I'm getting a bunch of jibberish in the output file. If you can, please check my code for errors.
#include <iostream> // standard input/output operations
#include <fstream> // file stream operations
#include <string> // string operations
#include <iomanip> // output manipulation using setw and setprecision
#include <climits> // INT_MAX
using namespace std;
int main ()
{
ifstream infile;
ofstream outfile;
string inf, outf; // Variables assigned to the input and output files.
// Prompt the user to enter the name of the input file.
// Echo prints the name of the input file.
cout << "Enter the name of the input file: ";
cin >> inf;
cout << inf << '\n' << endl;
// Open the input file.
infile.open(inf.c_str());
// Test to make sure the input file opened successfully.
// If the input file does not open, an error message will be output.
if (infile.fail())
{
cout << string(15, '*') << " File Open Error " << string(15, '*') << endl;
cout << "==> Input file failed to open properly!!" << endl;
cout << "==> Attempted to open file: " << inf << endl;
cout << "==> Terminating program!!!" << endl;
cout << string(47, '*') << endl;
}
// If input file was successfully opened, prompt the user to enter
// the name of the output file.
// Test to make sure the output file opened successfully.
// If the output file does not open, an error message will be output.
else if (infile.good())
{
cout << "Enter name of the output file: ";
cin >> outf;
cout << outf << '\n' << endl;
}
// Open the output file.
outfile.open(outf.c_str());
if (outfile.fail())
{
cout << string(15, '*') << " File Open Error " << string(15,'*') << endl;
cout << "==> Output file failed to open properly!!" << endl;
cout << "==> Attempted to open file: " << outf << endl;
cout << "==> Terminating program!!!" << endl;
cout << string(47, '*') << endl;
}
// If both the input and output files opened successfully,
// attempt to read in the title from the input file.
// Test to see if the input file is empty.
// If the input file is empty, an error message will be output.
// Write the title to the output file if the title exists.
string title;
if (infile.eof())
{
cout << string(16, '*') << " File Is Empty " << string(16, '*') << endl;
cout << "==> Input file is empty." << endl;
cout << "==> No information to process." << endl;
cout << "==> Terminating program." << endl;
cout << string(47, '*') << endl;
}
else
{
getline(infile, title);
outfile << title << endl;
}
// Set up for the output file columns.
// Column headings: Last Name, First Name, Average, Letter.
// Series of dashes (-) under each column heading.
outfile << setw(12) << left << "Last Name" << setw(12) << "First Name"
<< setw(9) << "Average" << "Letter" << endl;
// Assign variables for while loop.
// float data type for numerical values.
// Assign the first and last names to data type string.
// char data type for the letter grades.
// Use the getline function to extract the first name from the
// input file. Extraction stops at the comma.
float w, x, y, z, avg;
string first_name, last_name;
char A, B, C, D, F, letter;
getline(infile, first_name, ',');
while(infile)
{
getline(infile, last_name, ',');
infile >> w >> x >> y >> z;
avg = (w + x + y + z)/4;
if (avg >= 90)
{
letter = A;
}
else if (avg >= 80 && avg < 90)
{
letter = B;
}
else if (avg >= 70 && avg < 80)
{
letter = C;
}
else if (avg >= 60 && avg < 70)
{
letter = D;
}
else (avg < 60);
{
letter = F;
}