Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "myLibraryBMR.h"
- using namespace std;
- //Blake Robinson COSC 1336
- //****************************readInt*********************************
- //description: reads and validates an interger in the range between min and max
- // errorMsg: describes the type of data to reenter if error encountered
- // min: the minimum accetable value
- // max: the maximum accetable value
- // precondition: user has been prompted for input
- // errorMsg may be assigned a value,
- // otherwise default argument is empty string
- // min and max may be assigned values,
- // otherwise default arguments are INT_MIN and INT_MAX
- //uses header files: iostream, string, limits, cctype
- // postcondition: a valid doubleerger in the range min to max is returned
- int readInt(string errorMsg, int min, int max)
- {
- int someInt; //stores each integer entered
- bool valid = false; //loop control flag indicating validity of entry
- do {
- //read an integer delimited by a whitespace character
- cin >> someInt;
- //valid if within min to max range; && is logical AND operator
- valid = someInt >= min && someInt <= max;
- //if input has failed or is invalid or if next char is invalid clear
- //fail state and remove invalid input; || is logical OR operator
- if (cin.fail() || !valid || !isspace(cin.peek()))
- {
- cout << "\nError! Invalid input.\n" << errorMsg << "must be a "
- << "whole number between " << min << " and " << max
- << ".\nTry Again: ";
- cin.clear(); //clear the fail state
- //remove up to max number of chars in input stream or up to '\n'
- cin.ignore(numeric_limits<streamsize>::max(), '\n');
- valid = false; //no valid entry has been read
- }
- } while (!valid); //loop again, prompting user to re-enter a valid integer
- //when valid input received, remove from input stream up to 100 remaining
- //characters or until reaches the '\n' <ENTER> character
- cin.ignore(100, '\n');
- return someInt; // return a valid integer to calling enviroment
- }
- //****************************readDouble*********************************
- //description: reads and validates an double in the range between min and max
- // errorMsg: describes the type of data to reenter if error encountered
- // min: the minimum accetable value
- // max: the maximum accetable value
- // precondition: user has been prompted for input
- // errorMsg may be assigned a value,
- // otherwise default argument is empty string
- // min and max may be assigned values,
- // otherwise default arguments are INT_MIN and INT_MAX
- //uses header files: iostream, string, limits, cctype
- // postcondition: a valid double in the range min to max is returned
- double readDouble(string errorMsg, double min, double max)
- {
- double someDouble; //stores each double entered
- bool valid = false; //loop control flag indicating validity of entry
- do {
- //read an double delimited by a whitespace character
- cin >> someDouble;
- //valid if within min to max range; && is logical AND operator
- valid = someDouble >= min && someDouble <= max;
- //if input has failed or is invalid or if next char is invalid clear
- //fail state and remove invalid input; || is logical OR operator
- if (cin.fail() || !valid || !isspace(cin.peek()))
- {
- cout << "\nError! Invalid input.\n" << errorMsg << " must be a "
- << " double between " << setprecision(6)
- << (min >= -9e6 && min <= -1e-6 || min <= 9e6 && min >= 1e-6 || min == 0 ? fixed : scientific)
- << min << " and "
- << (max >= -9e6 && max <= -1e-6 || max <= 9e6 && max >= 1e-6 || max ==0 ? fixed : scientific)
- << max
- << ".\nTry Again: ";
- cin.clear(); //clear the fail state
- //remove up to max number of chars in input stream or up to '\n'
- cin.ignore(numeric_limits<streamsize>::max(), '\n');
- valid = false; //no valid entry has been read
- }
- } while (!valid); //loop again, prompting user to re-enter a valid Doubleeger
- //when valid input received, remove from input stream up to 100 remaining
- //characters or until reaches the '\n' <ENTER> character
- cin.ignore(100, '\n');
- return someDouble; // return a valid double to calling enviroment
- }
- //takes in loan amount, number of payments, apr rate, and length of loan to calculate total of each payment
- double payment(double rate, double terms, int perYear, double loan)
- {
- double nper;
- nper = perYear * terms;
- double owed;
- if (rate == 0.0) //if statement to deal with 0% apr
- {
- owed = loan / nper;
- }
- else {
- rate /= 100; //convert rate to decimal. example: 6.25=>.0625
- rate /= nper; // divide rate by nper to to convert rate again
- //convert nper by multiply by terms to get exact amout of total payments that will be made
- double n = pow(1 + rate, nper); //saving exponent as variable n
- owed = ((rate*n) / (n - 1)) *loan; //loan formula
- }
- return owed;
- }
- //takes in length and width and returns the area
- double getArea(double length, double width)
- {
- return length * width;
- }
- //****************************readChar*********************************
- //description: reads and validates if one of the two char parameters have been entered
- // errorMsg: describes the type of data to reenter if error encountered
- // char1 is passed one of the char entered and validates it
- // char2 is passed the other char entered and validates it
- // precondition: user has been prompted for input
- // errorMsg may be assigned any string,
- // otherwise default argument is "Yes or No"
- // char1 or char2 can be any char otherwise defaults are 'Y' or 'N'
- //uses header files: iostream, string, cctype
- // postcondition: a valid char that is equal to char1, or char2
- char readChar(string errorMsg, char char1, char char2)
- {
- char someChar; //stores each char entered
- bool valid = false; //loop control flag indicating validity of entry
- do {
- //read an char delimited by a whitespace character
- cin >> someChar;
- someChar = toupper(someChar);
- //valid if within min to max range; && is logical AND operator
- valid = someChar == char1 || someChar == char2;
- //if input has failed or is invalid or if next char is invalid clear
- //fail state and remove invalid input; || is logical OR operator
- if (cin.fail() || !valid || !isspace(cin.peek()))
- {
- cout << "\nError! Invalid input.\n" << errorMsg << " must be a "
- << "letter " << char1 << " or " << char2
- << ".\nTry Again: ";
- cin.clear(); //clear the fail state
- //remove up to max number of chars in input stream or up to '\n'
- cin.ignore(numeric_limits<streamsize>::max(), '\n');
- valid = false; //no valid entry has been read
- }
- } while (!valid); //loop again, prompting user to re-enter a valid integer
- //when valid input received, remove from input stream up to 100 remaining
- //characters or until reaches the '\n' <ENTER> character
- cin.ignore(100, '\n');
- return someChar; // return a valid char to calling enviroment
- }
- //prompts user to enter a name then returns name. Parameter type is what type of name you want to enter
- string getName(string type)
- {
- string name;
- cout << "Enter " << type << " Name: ";
- getline(cin,name);
- return name;
- }
- //takes in a sum and divides it by the counter total to return an average of the sum
- double getAverage(double sum, int count)
- {
- if (count == 0)
- {
- return 0.0;
- }
- else{
- return sum / count;
- }
- }
- //takes in string as parameter and caps every first alpha char in a string and
- //lowers every other char in that string until reaching a whitespace where it repeats the process
- string formatString(string theString)
- {
- int capital = true;
- int length = theString.length();
- for (int i = 0; i < length; i++)
- {
- char c = theString[i];
- if (!capital)
- {
- if (isalpha(c))
- theString[i] = tolower(theString[i]);
- }
- else
- {
- capital = false;
- if (isalpha(c))
- theString[i] = toupper(theString[i]);
- }
- if (isspace(theString.at(i)))
- {
- capital = true;
- }
- }
- return theString;
- }
- //CE Static Arrays displayArray function
- void displayArray(const int array[], const int size)
- {
- for (int i = 0; i < size;)
- {
- if (i % 5 == 0)
- {
- cout << endl;
- }
- cout << setw(5) << right << array[i++];
- }
- cout << endl;
- }
- //CE Static Arrays copyArray function
- void copyArray(int toArray[], const int fromArray[], const int size)
- {
- for (int i = 0; i < size; i++)
- {
- toArray[i] = fromArray[i];
- }
- }
- //opens i/o file
- void openIOFile(fstream&file, string fileName, ios::openmode mode)
- {
- file.open(fileName, mode);
- if (file.fail()) //exit program if file open fails
- {
- cerr << "\n\nERROR OPENING " << fileName << " FILE FOR "
- << (mode == 1 ? "input" : "output") << ". \n\n"
- << "PROGRAM TERMINATED. \n\n";
- system("pause");
- exit(EXIT_FAILURE);
- }
- }
- //parameter 1 is passed address of array parameter 2 is passed size of array
- //the function saves first value in array as smallest and then compares each
- //one with the rest of the array if any value is smaller it is saved as smallest
- int min(const int theArray[], const int size)
- {
- int smallest = theArray[0];
- for (int i = 1; i < size; i++)
- {
- if (smallest > theArray[i])
- {
- smallest = theArray[i];
- }
- }
- return smallest;
- }
- //parameter 1 is passed address of array parameter 2 is passed size of array
- //the function saves first value in array as largest and then compares each
- //one with the rest of the array if any value is larger it is saved as largest
- int max(const int theArray[], const int size)
- {
- int largest = theArray[0];
- for (int i = 1; i < size; i++)
- {
- if (largest < theArray[i])
- {
- largest = theArray[i];
- }
- }
- return largest;
- }
- //takes in a grade rounds it to nearest 0th place
- //divides average grade by 10 to get a single digit
- //uses switch case to compare that digit if the digit matches it
- //returns the corresponding letter grade
- char grade(double averageGrade)
- {
- if(averageGrade <= 100 && averageGrade >= 0)
- {
- int rounded = static_cast<int>(round(averageGrade, 0));
- switch (rounded/10)
- {
- case 10:
- case 9:
- return 'A';
- case 8:
- return 'B';
- case 7:
- return 'C';
- case 6:
- return 'D';
- default:
- return 'F';
- }
- }
- else return '*';
- }
- //overloaded function that takes in an array address as parameter 1 and size
- //as parameter 2 adds up each value in an array then divides by the size of array
- double getAverage(const int theArray[], const int size)
- {
- double total = 0;
- for (int i = 0; i < size; i++)
- {
- total += theArray[i];
- }
- if (total == 0 || size == 0)
- {
- return 0;
- }
- else
- {
- return total / size;
- }
- }
- //takes string of name in format last, first middle
- //returns as first middle last
- string reverseName(string name)
- {
- string first;
- string last;
- string::size_type pos = name.find(',');
- if (pos != string::npos)
- {
- first = name.substr(pos + 2); //extracts until end of string
- last = name.substr(0, pos); //extracts from start of string to right before comma
- return first + " " + last;
- }
- else
- {
- return name.insert(0, "Invalid: ");
- }
- }
- //function which takes a format string as an argument
- string getDate(string fmtStr)
- {
- const int SIZE = 50; //size of char arrray to store date
- char strDate[SIZE]; //c-style string - array of chars to store date
- time_t today; //represent time as a time_t integer which represents the
- //number of seconds that have elapsed since 1/1/1970 UTC
- //time_t good until 2038, then will need 64 bit _times64_t
- time(&today); //get time from system storing in the address of today
- tm thisTime; //a tm struct object used to store, retrieve
- //and format various time and date components
- //localtime_s function converts a time stored as a time_t value in today and stored the
- // result in the thisTime structure of type tm
- localtime_s(&thisTime, &today);
- //strftime function extracts time components form the tm structure thisTime
- //and returns a time formatted according to the fmtStr. The string function
- //c_str() converts a c-style string to a string class object.
- //the formatted results are stored in strDate.
- strftime(strDate, sizeof(strDate) - 1, fmtStr.c_str(), &thisTime);
- return strDate;
- }
- //takes in value of number as parameter 1 and parameter 2 is the number of digits you'd like to round to
- //uses cmath libary fabs and floor functions to round a number to the desired decimal place
- double round (double num, int place)
- {
- auto factor = pow(10.0, place);
- double rounded = floor(fabs(num) * factor + 0.5) / factor;
- return rounded;
- }
- GradeType getGrade(double gradeAvg)
- {
- if (gradeAvg <= 100 && gradeAvg >= 0)
- {
- int rounded = static_cast<int>(round(gradeAvg, 0));
- switch (rounded / 10)
- {
- case 10:
- case 9:
- return A;
- case 8:
- return B;
- case 7:
- return C;
- case 6:
- return D;
- default:
- return F;
- }
- }
- else return X;
- }
- char gradeTypeToChar(GradeType grade)
- {
- switch (grade)
- {
- case A:
- return 'A';
- case B:
- return 'B';
- case C:
- return 'C';
- case D:
- return 'D';
- case F:
- return 'F';
- case X:
- return '*';
- }
- }
- void displayArray(const double array[], const int size)
- {
- for (int i = 0; i < size;)
- {
- if (i % 5 == 0 && i >= 10)
- {
- cout << endl;
- }
- cout << setw(8) << right << array[i++];
- }
- cout << endl;
- }
- //Blake Robinson COSC 1336
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement