/* Filename: makegradefile.cpp Author: Br. David Carlson; Br. Isidore Minerd Date: October 25, 2004; July 16, 2009 This program reads floating point GPAs (0.00 to 4.00) from the keyboard and writes them to the gradefile.txt text file, one per line. The numbers should be entered one at a time, as prompted. The numbers are written to the file with exactly 2 decimal places. Use CTRL z to end data input when using Visual C++ on a PC. You may need to press ENTER a couple of times. However, this must be changed to using CTRL d when using g++ under Linux, as CTRL d is the Unix/Linux end of file. Input will also terminate if non-float input is entered. Tested with: Microsoft Visual Studio 2005 Microsoft Visual Studio 2008 g++ under Linux */ #include #include #include // in order to use setw using namespace std; int main(void) { float Num; fstream OutFile; OutFile.open("gradefile.txt", ios::out); if (OutFile.fail()) { cout << "Open of gradefile.txt failed" << endl; exit(1); } // Set up output file formatting: OutFile.setf(ios::fixed); OutFile.setf(ios::showpoint); OutFile.precision(2); while (! cin.fail()) { // Warning: Change this to CTRL d for Linux: cout << "Enter a GPA number (or CTRL z to end): " << endl; cin >> Num; // Process the data: if (Num < 0.0 || Num > 4.0) cout << "Invalid value for GPA number. It must be between 0.0 and 4.0!" << endl; else OutFile << setw(12) << Num << endl; } OutFile.close(); return 0; }