//----------------------------------------------- // LetterGrade.cpp // // Illustrate nested if-then-else structure. // Given a test score, determine the letter // grade. // // D. Searls // Asbury College // Sept. 2006 //----------------------------------------------- #include<iostream> using namespace std; int main() { int testScore; char letterGrade; cout << "Enter test score (0 - 100): "; cin >> testScore; if (testScore >= 90) { letterGrade = 'A'; } else if (testScore >= 80) { letterGrade = 'B'; } else if (testScore >= 70) { letterGrade = 'C'; } else if (testScore >= 60) { letterGrade = 'D'; } else { letterGrade = 'F'; } cout << endl << "A score of " << testScore << " corresponds to a grade of " << letterGrade << "." << endl; return 0; }
//*********************************************** // FileDemo.cpp // // Demonstrate basic file input and output. // // D. Searls // Asbury College // May 2006 //*********************************************** #include <iostream> #include <string> #include <fstream> using namespace std; int main() { ifstream infile; // The input file ofstream outfile; // The output file string inFilename; string outFilename; string firstName; char middleInitial; string lastName; string streetAddress; string city; string state; string zipcode; // Open the input file cout << "Enter the name of the input file (FileDemo.dat): "; cin >> inFilename; cout << endl; infile.open(inFilename.c_str()); if (infile.fail()) { cout << "FATAL ERROR: The input file could not be opened." << endl; } else { // Open the output file cout << "Enter the name of the output file: "; cin >> outFilename; cout << endl; outfile.open(outFilename.c_str()); if (outfile.fail()) { infile.close(); cout << "FATAL ERROR: The output file could not be opened." << endl; } else { // Read the address data from the input file infile >> firstName >> middleInitial >> lastName; infile.ignore(1); getline(infile, streetAddress); infile >> city >> state >> zipcode; infile.close(); // Write the formatted address to the output file if (!infile.fail()) { outfile << firstName << ' '; outfile << middleInitial << ". "; outfile << lastName << endl; outfile << streetAddress << endl; outfile << city << ", " << state << " " << zipcode << endl; if (!outfile.fail()) { cout << "The output was sent to the file " << '"' << outFilename << '"' << endl; } else { cout << "FATAL ERROR: Output file write error." << endl; } } else { cout << "FATAL ERROR: Input file read error. " << "No output generated." << endl; } outfile.close(); } } return 0; }