Directory:Derek Elder/Programs/Grade Calculator

MyWikiBiz, Author Your Legacy — Saturday November 30, 2024
< Directory:Derek Elder‎ | Programs
Revision as of 22:45, 1 November 2007 by Derek Elder (talk | contribs) (start of page)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cmath>
using namespace std;

int main()
{
	string first_name = string();
	string last_name = string();
	int test_score = 0;
	int test_number = 0;
	float total_score = 0.0;
	bool done = false;
	char grade = ' ';
	char ch = ' ';

	ifstream Input;
	ofstream Output;
	string Input_File_Name = string();
	string Output_File_Name = string();

	while(!done)
	{
		Input.clear();
		cout<<"Please enter the name of your input file: ";
		getline(cin,Input_File_Name);
		Input.open(Input_File_Name.c_str());

		if(!Input)
		{
			cout<<"The file name is invalid or the file is read protected.\n";
			done = false;
		}
		else
		{
			cout<<"The file "<<Input_File_Name<<" exists.\n";

			ch = Input.peek();

			if(ch == EOF)
			{
				cout<<"The file "<<Input_File_Name<<" has no data in it."<<endl;
				done = false;
				Input.close();
			}
			else
			{
				cout<<"The file "<<Input_File_Name<<" has data in it."<<endl;
				done = true;
			}
		}
	}

	done = false;

	while(!done)
	{
		Output.clear();
		cout<<"Please enter the name of your output file: ";
		getline(cin,Output_File_Name);
		Output.open(Output_File_Name.c_str());

		if(!Output)
		{
			cout<<"Output file creation failed\n";
			done = false;
		}
		else
		{
			cout<<"Output file creation successful\n";
			done = true;
		}
	}

	Output<<"-----------------------------Grade Calculator-----------------------------"<<endl;
    Output<<endl;
    Output<<"This program will process test scores to provide individuals"<<endl;
    Output<<"with letter grades according to the following scale:"<<endl;
    Output<<endl;                                                    //  ||
    Output<<"		Average 90-100                   A"<<endl;
    Output<<"		Average 80-89.9                  B"<<endl;
    Output<<"		Average 70-79.9                  C"<<endl;
    Output<<"		Average 60-69.9                  D"<<endl;
    Output<<"		Average <  59.9                  F"<<endl;
    Output<<"--------------------------------------------------------------------------"<<endl;
    Output<<"--------------------------------------------------------------------------"<<endl;

	Input>>first_name>>last_name;
	while(Input)
	{
		Output<<"Your name is             :"<<first_name<<" "<<last_name<<endl;
		Input>>test_score;

		if(test_score < 0)
		{
			Output<<first_name<<" "<<last_name<<" you didn't take any tests"<<endl;
			Output<<"--------------------------------------------------------------------------"<<endl;
		}
		else
		{
			Output<<"Your scores are          :";
			total_score = 0;
			test_number = 0;
			while(test_score >= 0)
			{
				total_score = total_score + test_score;
				test_number++;
				Output<<" "<<test_score;
				Input>>test_score;
				while(Input.fail())
				{
					Input.clear();
					Input.ignore(100,' ');
					Input>>test_score;
				}
			}

			float test_average = total_score/test_number;

			if(test_average >= 90)
				grade = 'A';
			else if(test_average >= 80)
				grade = 'B';
			else if(test_average >= 70)
				grade = 'C';
			else if(test_average >= 60)
				grade = 'D';
			else 
				grade = 'F';

			Output<<endl<<first_name<<" "<<last_name<<", your average is "<<setprecision(1)<<fixed<<test_average
				  <<", and your letter grade is "<<grade<<endl;
			Output<<"--------------------------------------------------------------------------"<<endl;
		}
			Input>>first_name>>last_name;
	}
	Output<<"Thank you for using this Grade Calculator.";

	cout<<"Good day.\n";
	Input.close();
	Output.close();
	return 0;
}