// Name: Alan Helms // Class: CPT 232 C++ I // Times: MWF 9:00 - 9:50 // Semester: Spring 2-22-2003 // Project Name: Bdate_vfx.cpp // Program Purpose: This program was designed to allow users to enter the // current date: month, day, year, also allow user to enter // the users birth date: month, day, year. Once the dates have // been collected, the program will output the users age and // then request if any more birth dates need to be entered. // Bdate_vfx.cpp works for any number of birth dates. if user // requests no more entries, then the program will output the // average age and the oldest age of the group. // // // ****NOTE: THIS VERSION WAS CREATED WITH BORLAND'S C++ v5.02 EDITIOR**** // ****NOTE: MODIFIED FOR MICORSOFT VISUAL STUDIO C++**** #include using std::cin; using std::cout; using std::endl; #include using std::setprecision; using std::setiosflags; using std::ios; int main(){ // int vars for current date. int cMonth=0, cDay=0, cYear=0; // int vars for user-entered dates. int uMonth=0, uDay=0, uYear=0; // int vars: counters, accumulators, ect. int oldest=0, counter=0, totalAges=0, nAge=0; // check figure for yes and no. char yes_NO; // prompt user to enter the current date. cout << "What is today's date?\n"; // "current date" do-while stucture. do { //prompts user to enter the current month. cout << " month (1-12)\r"; cin >> cMonth; // that value is stored here. // tests to see value entered is between the range (1-12). // before moving onto the next section of code. } while (!(cMonth >= 1 && cMonth <=12)); do { //prompts user to enter the current day. cout << " day (1-31)\r"; cin >> cDay; // that value is stored here. // tests to see value entered is between the range (1-31). // before moving onto the next section of code. } while (!(cDay >= 1 && cDay <=31)); do { //prompts user to enter the current year. cout << " year (1800-2003)\r"; cin >> cYear; // that value is stored here. // tests to see value entered is between the range (1-12). // before moving onto the next section of code. } while (!(cYear >= 1800 && cYear <=2003)); // end "current date" do-while structure. // "user entered dates" do-wile stucture. do { // prompts user to enter their Birth date. cout << "\nWhat is your birthday?\n"; do { // propmts user to enter the month of there birth date. cout << " month (1-12)\r"; cin >> uMonth; // that value is stored here. // test to see value entered is between the range (1-12). // before moving onto the next section of code. } while (!(uMonth >= 1 && uMonth <=12)); do { // propmts user to enter the day of there birth date. cout << " day (1-31)\r"; cin >> uDay; // that value is stored here. // test to see value entered is between the range (1-12). // before moving onto the next section of code. } while (!(uDay >= 1 && uDay <=31)); do { // propmts user to enter the year of there birth date. cout << " year (1800-2003)\r"; cin >> uYear; // that value is stored here. // test to see value entered is between the range (1-12). // before moving onto the next section of code. } while (!(uYear >= 1800 && uYear < 2003)); // substract the values of current year and user year and assign it // to the variable "nAge". nAge = cYear - uYear; // this section of code determines if the age needs to be decremented // by one year or not, also determines the oldest age and accumulates // the total number of ages entered. Outputs the result to // the screen. // user month >= current month. if ( uMonth >= cMonth ){ // TRUE: user day >= current day. if ( uDay >= cDay ){ // output age. cout << "You are " << nAge << " years old." << endl; //TRUE: age > oldest. if ( nAge > oldest ){ //TRUE: age added to "totalages". totalAges += nAge; //oldest set to age. oldest = nAge; } else{ //ELSE: age added to "totalages". totalAges += nAge; } } else{ // user month not >= current month. //age = age - 1 nAge -= 1; // output age. cout << "You are " << nAge << " years old." << endl; //TRUE: age > oldest. if ( nAge > oldest ){ //TRUE: age added to "totalages". totalAges += nAge; //oldest set to age. oldest = nAge; } else{ //ELSE: age added to "totalages". totalAges += nAge; } } } else{ nAge -= 1; // output age. cout << "You are " << nAge << " years old." << endl; //TRUE: age > oldest. if ( nAge > oldest ){ //TRUE: age added to "totalages". totalAges += nAge; //oldest set to age. oldest = nAge; } else{ //ELSE: age added to "totalages". totalAges += nAge; } } // prompt user to enter more dates if needed. cout << "Are there any more folks who'd like to enter their "; cout << "birth dates?\n(Enter y for yes, n for no)\n"; cin >> yes_NO; // hold char value n||N. ++counter; // increments the counter by one. // test to see if var "yes_NO" is a 'y' or 'n' before moving on to // next section of code. // if var "yes_NO" is equal to 'n' or 'N' then // the loop is terminated, otherwise loop until condition is met. } while ( yes_NO != 'n' && yes_NO != 'N' ); // end "user entered dates" do-wile stucture. // output the average age. cout << "\nAverage age is = " // set the visiable decimal point to 1 to the right of the decimal. << setprecision(1) // set flags not to convert output to scientific nototation. // set flags to show the decimal point. << setiosflags(ios::fixed | ios::showpoint) // type casting of double to int totalAges to perform real division. // cast double totalAges divied by the counter gives average age // of group. << (double) totalAges / counter << endl; // output the oldest person of the group cout << "The elder statesperson of your group is "; cout << oldest << " years old." << endl; return 0; } // end program