From 58bc7e885ffbefd842a6a8a319804873ff40aded Mon Sep 17 00:00:00 2001 From: Jesus Hilario Hernandez Date: Sun, 10 Feb 2019 10:11:07 -0600 Subject: [PATCH] added & modified --- .../Programming Challenges/18.cpp | 40 +-- .../Programming Challenges/19.cpp | 13 +- .../Programming Challenges/20.cpp | 50 +-- .../Programming Challenges/25.cpp | 299 ++++++++++++------ .../Examples/7-1.cpp | 30 ++ .../Examples/7-2.cpp | 24 ++ .../Examples/7-3.cpp | 25 ++ .../Examples/7-4.cpp | 27 ++ .../Examples}/SavedNumbers.txt | 0 .../Examples/TenNumbers.txt | 10 + .../Examples/numbers.txt | 12 + .../7-1.cpp | 0 .../7-2.cpp | 0 .../checkpoint-7-7.cpp | 0 .../7-3.cpp | 0 .../TenNumbers.txt | 0 .../7-4.cpp | 0 .../SavedNumbers.txt | 10 + .../{ => Other}/array-initialization/7-6.cpp | 0 .../{ => Other}/array-initialization/7-7.cpp | 0 .../{ => Other}/array-initialization/7-8.cpp | 0 .../{ => Other}/array-initialization/7-9.cpp | 0 InputValidation/000.cpp | 77 +++++ InputValidation/validate-int.cpp | 77 +++++ .../validate-integer-as-string.3.INT.cpp | 2 +- 25 files changed, 548 insertions(+), 148 deletions(-) create mode 100644 Chapter-7-Arrays/7.2 Accessing Array Elements/Examples/7-1.cpp create mode 100644 Chapter-7-Arrays/7.2 Accessing Array Elements/Examples/7-2.cpp create mode 100644 Chapter-7-Arrays/7.2 Accessing Array Elements/Examples/7-3.cpp create mode 100644 Chapter-7-Arrays/7.2 Accessing Array Elements/Examples/7-4.cpp rename Chapter-7-Arrays/{Writing-the-Contents-of-an-Array-to-a-File => 7.2 Accessing Array Elements/Examples}/SavedNumbers.txt (100%) create mode 100644 Chapter-7-Arrays/7.2 Accessing Array Elements/Examples/TenNumbers.txt create mode 100755 Chapter-7-Arrays/7.2 Accessing Array Elements/Examples/numbers.txt rename Chapter-7-Arrays/{ => Other}/Inputting-and-Outputting-Array-Contents/7-1.cpp (100%) rename Chapter-7-Arrays/{ => Other}/Inputting-and-Outputting-Array-Contents/7-2.cpp (100%) rename Chapter-7-Arrays/{ => Other}/Inputting-and-Outputting-Array-Contents/checkpoint-7-7.cpp (100%) rename Chapter-7-Arrays/{ => Other}/Reading-Data-from-a-File-into-an-Array/7-3.cpp (100%) rename Chapter-7-Arrays/{ => Other}/Reading-Data-from-a-File-into-an-Array/TenNumbers.txt (100%) rename Chapter-7-Arrays/{ => Other}/Writing-the-Contents-of-an-Array-to-a-File/7-4.cpp (100%) create mode 100644 Chapter-7-Arrays/Other/Writing-the-Contents-of-an-Array-to-a-File/SavedNumbers.txt rename Chapter-7-Arrays/{ => Other}/array-initialization/7-6.cpp (100%) rename Chapter-7-Arrays/{ => Other}/array-initialization/7-7.cpp (100%) rename Chapter-7-Arrays/{ => Other}/array-initialization/7-8.cpp (100%) rename Chapter-7-Arrays/{ => Other}/array-initialization/7-9.cpp (100%) create mode 100644 InputValidation/000.cpp create mode 100644 InputValidation/validate-int.cpp diff --git a/Chapter-4-Making-Decisions/Review Questions and Exercises/Programming Challenges/18.cpp b/Chapter-4-Making-Decisions/Review Questions and Exercises/Programming Challenges/18.cpp index 2e0c43b..d195230 100644 --- a/Chapter-4-Making-Decisions/Review Questions and Exercises/Programming Challenges/18.cpp +++ b/Chapter-4-Making-Decisions/Review Questions and Exercises/Programming Challenges/18.cpp @@ -20,35 +20,32 @@ * calories or fat grams were incorrectly entered. * * Jesus Hilario Hernandez -* February 20, 2018 +* February 10, 2019 * ********************************************************************/ #include using namespace std; int main() { - // Varibles - float cal_from_fat, + float calories_from_fat, fat_grams, - per_of_cal, - total_cal; + percentage_of_calories, + total_calories; - // Ask for the number of calories & fat grams in food cout << "\nEnter number of calories: "; - cin >> total_cal; + cin >> total_calories; - if (total_cal > 0) + if (total_calories > 0) { cout << "Enter number of fat grams: "; cin >> fat_grams; if (fat_grams > 0) { - // Calculate - cal_from_fat = fat_grams * 9; - per_of_cal = (cal_from_fat / total_cal) * 100; - - if (cal_from_fat > total_cal) + calories_from_fat = fat_grams * 9; + percentage_of_calories = (calories_from_fat / total_calories) * 100; // .45 == 45 + + if (calories_from_fat > total_calories) { cout << "\nCalories from fat cannot be greater\n"; cout << "then the total number of calories." << endl; @@ -58,30 +55,25 @@ int main() } else { - // Display the % of calories from fat - cout << "Total calories = " << total_cal << " cal"<< endl; + cout << "Total calories = " << total_calories << " cal"<< endl; cout << "Total fat grams = " << fat_grams << " g" << endl; - cout << "Total calories from fat = " << cal_from_fat << " cal from fat"<< endl; - cout << "Total percentage of calories from fat = " << per_of_cal << '%' << endl; + cout << "Total calories from fat = " << calories_from_fat << " cal from fat"<< endl; + cout << "Total percentage of calories from fat = " << percentage_of_calories << '%' << endl; cout << endl; } } else { - // Explain error and try again - cout << "We're sorry. Total fat grams must be more than 0.\n"; + cout << "We're sorry. Fat grams must be more than 0.\n"; cout << "Please rerun the progarm and try again." << endl; } } else { - // Explain error and try again cout << "We're sorry. Total calories must be more than 0.\n"; cout << "Please rerun the progarm and try again." << endl; } + - // If calories from fat are less than 30%, display "food is low in fat" - - // Terminate program return 0; -} +} \ No newline at end of file diff --git a/Chapter-4-Making-Decisions/Review Questions and Exercises/Programming Challenges/19.cpp b/Chapter-4-Making-Decisions/Review Questions and Exercises/Programming Challenges/19.cpp index c6eceb8..5a03146 100644 --- a/Chapter-4-Making-Decisions/Review Questions and Exercises/Programming Challenges/19.cpp +++ b/Chapter-4-Making-Decisions/Review Questions and Exercises/Programming Challenges/19.cpp @@ -17,13 +17,11 @@ using namespace std; int main() { - // Variables double wavelength; - // Ask for wavelength (in meters) - cout << "/nEnter wavelength (in meters): "; - cin >> wavelength; - // Display the type of wave according to chart + cout << "\nEnter wavelength (in meters): "; + cin >> wavelength; // .0001 + if (wavelength >= 1E-2) cout << "Radio Waves" << endl; else if (wavelength <= 1E-2 && wavelength >= 1E-3) @@ -39,8 +37,7 @@ int main() else if (wavelength <= 1E-11) cout << "Gamma Rays" << endl; - // Format line break cout << endl; - // Terminate program + return 0; -} +} \ No newline at end of file diff --git a/Chapter-4-Making-Decisions/Review Questions and Exercises/Programming Challenges/20.cpp b/Chapter-4-Making-Decisions/Review Questions and Exercises/Programming Challenges/20.cpp index 4a22fe3..9cee058 100644 --- a/Chapter-4-Making-Decisions/Review Questions and Exercises/Programming Challenges/20.cpp +++ b/Chapter-4-Making-Decisions/Review Questions and Exercises/Programming Challenges/20.cpp @@ -23,7 +23,7 @@ * distances less than 0. * * Jesus Hilario Hernandez -* February 21, 2018 +* February 21, 2019 * ********************************************************************/ #include @@ -31,13 +31,16 @@ using namespace std; int main() { - // Variables - int user_select; + const int AIR_FEET_PER_SECOND = 1100, + WATER_FEET_PER_SECOND = 4900, + STEEL_FEET_PER_SECOND = 16400, + MINIMUM_DISTANCE = 0; + + int user_menu_choice; + float length_of_travel, amount_of_time; - // Display menu - // Ask user to select from menu cout << "===================" << endl; cout << " Choose one: " << endl; cout << "-------------------" << endl; @@ -45,54 +48,55 @@ int main() cout << " 2. Water " << endl; cout << " 3. Steel " << endl; cout << "===================" << endl; - cin >> user_select; + cin >> user_menu_choice; - // Decision statement - switch (user_select) + switch (user_menu_choice) { case 1: case 2: case 3: - // Ask user to enter the distance sound wave will travel cout << "\nHow far will the sound wave travel? "; cin >> length_of_travel; - // If less that 0, error check - if (length_of_travel < 0) + if (length_of_travel < MINIMUM_DISTANCE) { - cout << "\nWe're sorry. Distance must be more than 0.\n"; + cout << "\nError. Distance must be more than 0.\n"; cout << "Rerun the program and try again.\n" << endl; } else { - if (user_select == 1) + // Valid + if (user_menu_choice == 1) { - amount_of_time = length_of_travel / 1100; + amount_of_time = length_of_travel / + AIR_FEET_PER_SECOND; cout << "In air "; + } - else if (user_select == 2) + else if (user_menu_choice == 2) { - amount_of_time = length_of_travel / 4900; + amount_of_time = length_of_travel / + WATER_FEET_PER_SECOND; cout << "In water "; } - else if (user_select == 3) + else if (user_menu_choice == 3) { - amount_of_time = length_of_travel / 16400; + amount_of_time = length_of_travel / + STEEL_FEET_PER_SECOND; cout << "In steel "; } - - // Display amount of time it will take cout << setprecision(4) << fixed; cout << "the sound wave will take " << amount_of_time; cout << " seconds to travel." << endl << endl; } break; - + default: cout << "\nWe're sorry. Choose a number between 1 and 3." << endl; cout << "Rerun the program and try again.\n" << endl; + break; } - // Terminate program + return 0; -} +} \ No newline at end of file diff --git a/Chapter-6-Functions/Review Questions and Exercises/Programming Challenges/25.cpp b/Chapter-6-Functions/Review Questions and Exercises/Programming Challenges/25.cpp index 6b00768..f476a1d 100644 --- a/Chapter-6-Functions/Review Questions and Exercises/Programming Challenges/25.cpp +++ b/Chapter-6-Functions/Review Questions and Exercises/Programming Challenges/25.cpp @@ -100,7 +100,7 @@ int carMilage(); double vehicleExpenses(double); double parkingFees(double &); -double taxiFees(double &, double &); +double taxiFees(double &); double conferenceFees(); double hotelExpenses(double &); @@ -110,7 +110,10 @@ double inputValidate(double); int inputValidate(int); int validateTime(); char validateChoice(); + void pressEnterToContinue(); +void display(string, int); +void display(string, double); int main() { @@ -131,25 +134,50 @@ int main() meal_amount, total_expenses, total_allowable_expenses = 0, // Reference - total_savings; + total_savings, + potential_savings; // Function Calls days_spent = daysSpent(); + display("days_spent", days_spent); + departure_time = departureTime(); + display("departure_time", departure_time); + arrival_time = arrivalTime(); + display("arrival_time", arrival_time); + round_trip_airfare = roundTripAirfare(); + display("round_trip_airfare", round_trip_airfare); car_rental_amount = carRentalAmount(); + display("car_rental_amount", car_rental_amount); + car_milage = carMilage(); + display("car_milage", car_milage); vehicle_expense = vehicleExpenses(car_milage); + display("vehicle_expenses", vehicle_expense); + parking_fees = parkingFees(total_allowable_expenses); - taxi_fees = taxiFees(amount_paid_by_employee, total_allowable_expenses); + display("total_allowable_expenses", total_allowable_expenses); + display("parking_fees", parking_fees); + + taxi_fees = taxiFees(total_allowable_expenses); + display("total_allowable_expenses", total_allowable_expenses); + display("taxi_fees", taxi_fees); + conference_fees = conferenceFees(); - hotel_expenses = hotelExpenses(amount_paid_by_employee); + display("conference_fees", conference_fees); + + hotel_expenses = hotelExpenses(total_allowable_expenses); + display("total_allowable_expenses", total_allowable_expenses); + display("hotel_expenses", hotel_expenses); - meal_amount = mealAmount(departure_time, arrival_time, amount_paid_by_employee); + meal_amount = mealAmount(departure_time, arrival_time, total_allowable_expenses); + display("total_allowable_expenses", total_allowable_expenses); + display("meal_amount", meal_amount); /******************************************************** @@ -168,17 +196,18 @@ int main() taxi_fees + conference_fees + hotel_expenses + - meal_amount + - amount_paid_by_employee; + meal_amount; + amount_paid_by_employee = total_expenses - total_allowable_expenses; total_savings = total_expenses - amount_paid_by_employee; + potential_savings = amount_paid_by_employee - total_allowable_expenses; cout << setprecision(2) << fixed; cout << "\n---------------------------------------------" << endl << "Total expenses = $" << total_expenses << endl << "Total Allowable expenses = $" << total_allowable_expenses << endl << "Total reimbursement expenses = $" << amount_paid_by_employee << endl - << "Total potential savings = $" << total_savings << endl + << "Total potential savings = $" << potential_savings << endl << "---------------------------------------------" << endl; // calculateAndDisplay(); @@ -218,9 +247,7 @@ int departureTime() { int departure_time; - cout << "\nNext, Let's start with the time of departure on 1st day.\n"; - - pressEnterToContinue(); + cout << "\nNext, Let's start with the time of departure on 1st day.\n\n"; departure_time = validateTime(); return departure_time; @@ -237,9 +264,7 @@ int arrivalTime() { int arrival_time; - cout << "\nNext, what was the time of arrival on last day?\n"; - - pressEnterToContinue(); + cout << "\nNext, what was the time of arrival on last day?\n\n"; arrival_time = validateTime(); return arrival_time; @@ -415,7 +440,7 @@ double parkingFees(double &total_allowable_expenses) * taxiFees ask for, receives, validates, and * * returns, if any, taxi fees. * ********************************************************/ -double taxiFees(double &amount_paid_by_employee, double &total_allowable_amount) +double taxiFees(double &total_allowable_expenses) { const double TAXI_LIMIT = 10.00; @@ -438,15 +463,10 @@ double taxiFees(double &amount_paid_by_employee, double &total_allowable_amount) << (i + 1) << ": "; fee = inputValidate(0); - if (fee <= TAXI_LIMIT) - taxi_fees += fee; - else - { - taxi_fees += TAXI_LIMIT; - amount_paid_by_employee += fee - TAXI_LIMIT; + if (fee > TAXI_LIMIT) total_allowable_expenses += TAXI_LIMIT; - } + taxi_fees += fee; } return taxi_fees; @@ -504,7 +524,7 @@ double conferenceFees() * hotelExpenses ask for, receives, validates, and * * returns, if any, hotel expenses. * ********************************************************/ -double hotelExpenses(double &amount_paid_by_employee) +double hotelExpenses(double &total_allowable_expenses) { const double HOTEL_LIMIT = 90.00; @@ -527,14 +547,11 @@ double hotelExpenses(double &amount_paid_by_employee) << (i + 1) << ": "; fee = inputValidate(0); - if (fee <= HOTEL_LIMIT) - hotel_expenses += fee; - else - { - hotel_expenses += HOTEL_LIMIT; - amount_paid_by_employee += fee - HOTEL_LIMIT; - } - + if (fee > HOTEL_LIMIT) + total_allowable_expenses += HOTEL_LIMIT; + + hotel_expenses += fee; + } return hotel_expenses; @@ -562,7 +579,7 @@ double hotelExpenses(double &amount_paid_by_employee) ********************************************************/ double mealAmount(int departure_time, int arrival_time, - double &amount_paid_by_employee) + double &total_allowable_expenses) { const double BREAKFAST_LIMIT = 9.00, LUNCH_LIMIT = 12.00, @@ -576,7 +593,7 @@ double mealAmount(int departure_time, // FOR DEPATURE!! // FOR DEPATURE!! // FOR DEPATURE!! - cout << "Upon depature of your first day, did you have a meal? "; + cout << "Upon depature of your first day, did you have a meal? (y/n): "; user_choice = validateChoice(); if (user_choice == 'Y' || user_choice == 'y') @@ -588,13 +605,10 @@ double mealAmount(int departure_time, << "What was amount for breakfast? "; fee = inputValidate(0); - if (fee < BREAKFAST_LIMIT) - meal_amount += fee; - else - { - meal_amount += BREAKFAST_LIMIT; - amount_paid_by_employee += fee - BREAKFAST_LIMIT; - } + if (fee > BREAKFAST_LIMIT) + total_allowable_expenses += BREAKFAST_LIMIT; + + meal_amount += fee; } else if(departure_time > 700 && departure_time < 1200) // Lunch { @@ -603,13 +617,10 @@ double mealAmount(int departure_time, << "What was amount for lunch? "; fee = inputValidate(0); - if (fee < LUNCH_LIMIT) - meal_amount += fee; - else - { - meal_amount += LUNCH_LIMIT; - amount_paid_by_employee += fee - LUNCH_LIMIT; - } + if (fee > LUNCH_LIMIT) + total_allowable_expenses += LUNCH_LIMIT; + + meal_amount += fee; } else if(departure_time > 1200 && departure_time < 1800) // Dinner { @@ -618,13 +629,10 @@ double mealAmount(int departure_time, << "What was amount for dinner? "; fee = inputValidate(0); - if (fee < DINNER_LIMIT) - meal_amount += fee; - else - { - meal_amount += DINNER_LIMIT; - amount_paid_by_employee = fee - DINNER_LIMIT; - } + if (fee > DINNER_LIMIT) + total_allowable_expenses += DINNER_LIMIT; + + meal_amount += fee; } else // If not within time restrains of Breakfast, Lunch, or Dinner. { @@ -633,28 +641,25 @@ double mealAmount(int departure_time, << "What was amount for your meal? "; fee = inputValidate(0); - meal_amount = 0; - amount_paid_by_employee += fee; + meal_amount += fee; + } - return meal_amount; } else if (user_choice == 'N' || user_choice == 'n') { meal_amount = 0; - return meal_amount; } else { // cout << "Error: Y or N must be chosen to proceed: "; meal_amount = 0; - return meal_amount; } // FOR ARRIVAL // FOR ARRIVAL // FOR ARRIVAL - cout << "Upon arrival on your last day, did you have a meal? "; + cout << "Upon arrival on your last day, did you have a meal? (y/n): "; user_choice = validateChoice(); if (user_choice == 'Y' || user_choice == 'y') @@ -666,13 +671,10 @@ double mealAmount(int departure_time, << "What was amount for breakfast? "; fee = inputValidate(0); - if (fee < BREAKFAST_LIMIT) - meal_amount += fee; - else - { - meal_amount += BREAKFAST_LIMIT; - amount_paid_by_employee = fee - BREAKFAST_LIMIT; - } + if (fee > BREAKFAST_LIMIT) + total_allowable_expenses += BREAKFAST_LIMIT; + + meal_amount += fee; } else if(arrival_time > 1300 && arrival_time < 1900) // Lunch { @@ -681,13 +683,10 @@ double mealAmount(int departure_time, << "What was amount for lunch? "; fee = inputValidate(0); - if (fee < LUNCH_LIMIT) - meal_amount += fee; - else - { - meal_amount += LUNCH_LIMIT; - amount_paid_by_employee = fee - LUNCH_LIMIT; - } + if (fee > LUNCH_LIMIT) + total_allowable_expenses += LUNCH_LIMIT; + + meal_amount += fee; } else if(arrival_time > 1900) // Dinner { @@ -696,13 +695,10 @@ double mealAmount(int departure_time, << "What was amount for dinner? "; fee = inputValidate(0); - if (fee < DINNER_LIMIT) - meal_amount += fee; - else - { - meal_amount += DINNER_LIMIT; - amount_paid_by_employee = fee - DINNER_LIMIT; - } + if (fee > DINNER_LIMIT) + total_allowable_expenses += DINNER_LIMIT; + + meal_amount += fee; } else // If not within time restrains of Breakfast, Lunch, or Dinner. { @@ -711,8 +707,7 @@ double mealAmount(int departure_time, << "What was amount for your meal? "; fee = inputValidate(0); - meal_amount = 0; - amount_paid_by_employee = fee; + meal_amount += fee; } return meal_amount; @@ -979,8 +974,6 @@ int validateTime() << endl << endl; - pressEnterToContinue(); - cout << "Ok, in this next portion you will need to input\n" << "the time in sections. \n\nThe first will be the hour\n" << "that you departed and the second will be the minutes.\n" @@ -1010,6 +1003,52 @@ int validateTime() is_num_bool = 1; // cout << hour << "(hour) is a number!" << endl; // cout << int_hour << "(int_hour) is a number!" << endl; + if (time_of_day == "PM") + { + switch (int_hour) + { + case 1: + hour = "13"; + break; + case 2: + hour = "14"; + break; + case 3: + hour = "15"; + break; + case 4: + hour = "16"; + break; + case 5: + hour = "17"; + break; + case 6: + hour = "18"; + break; + case 7: + hour = "19"; + break; + case 8: + hour = "20"; + break; + case 9: + hour = "21"; + break; + case 10: + hour = "22"; + break; + case 11: + hour = "23"; + break; + + } + } + else if (time_of_day == "AM") + { + if (int_hour == 12) + hour = "0"; + + } } else { @@ -1029,7 +1068,7 @@ int validateTime() is_num = 0; cout << "You've entered: " - << hour << endl; + << int_hour << endl; do { @@ -1050,10 +1089,56 @@ int validateTime() is_num_bool = 1; // cout << minutes << "(minutes) is a number!" << endl; // cout << int_minutes << "(int_minutes) is a number!" << endl; + if (time_of_day == "PM") + { + switch (int_hour) + { + case 1: + hour = "13"; + break; + case 2: + hour = "14"; + break; + case 3: + hour = "15"; + break; + case 4: + hour = "16"; + break; + case 5: + hour = "17"; + break; + case 6: + hour = "18"; + break; + case 7: + hour = "19"; + break; + case 8: + hour = "20"; + break; + case 9: + hour = "21"; + break; + case 10: + hour = "22"; + break; + case 11: + hour = "23"; + break; + + } + } + else if (time_of_day == "AM") + { + if (int_hour == 12) + hour = "0"; + + } } else { - cout << "Must be anumber and between 0 and 12. "; + cout << "Must be a number and between 0 and 12. "; cin.clear(); cin.ignore(numeric_limits::max(), '\n'); @@ -1068,7 +1153,7 @@ int validateTime() cout << "Time = " << int_hour << ":" - << setw(2) << right << int_minutes + << setw(2) << right << minutes << " " << time_of_day << endl; @@ -1079,16 +1164,21 @@ int validateTime() if (correct_time == 'N' || correct_time == 'n') { cout << "You've chosen no ['N'].\n" - << "Let's try again."; - pressEnterToContinue(); + << "Let's try again.\n"; } } while (correct_time == 'n' || correct_time == 'N'); - if (int_minutes >= 0 || int_minutes < 10) + if (int_minutes >= 0 && int_minutes < 10) + { + user_time_string = hour + "0" + minutes; + stringstream str_stream_object(user_time_string); + str_stream_object >> user_time; + } + else { - user_time_string = int_hour + 0 + int_minutes; + user_time_string = hour + minutes; stringstream str_stream_object(user_time_string); str_stream_object >> user_time; } @@ -1129,10 +1219,35 @@ char validateChoice() } +/** + * + * + * + * + * + * + * + * + * Stubs and Driver + */ void pressEnterToContinue() { cout << "\n\nPress [Enter] to continue:\n"; cin.clear(); cin.ignore(numeric_limits::max(), '\n'); cin.get(); +} +void display(string description, int variable) +{ + cout << endl + << description << " = " + << variable + << endl; +} +void display(string description, double variable) +{ + cout << endl + << description << " = " + << variable + << endl; } \ No newline at end of file diff --git a/Chapter-7-Arrays/7.2 Accessing Array Elements/Examples/7-1.cpp b/Chapter-7-Arrays/7.2 Accessing Array Elements/Examples/7-1.cpp new file mode 100644 index 0000000..e5b6f7d --- /dev/null +++ b/Chapter-7-Arrays/7.2 Accessing Array Elements/Examples/7-1.cpp @@ -0,0 +1,30 @@ +// This program asks for the number of hours worked. +// by six employees. It stores the values in an array. +#include +using namespace std; +int main() +{ + const int NUM_EMPLOYEES = 6; + int hours[NUM_EMPLOYEES]; + + // Get hours worked by each employee + cout << "Enter the hours worked by " + << NUM_EMPLOYEES << " employees: "; + cin >> hours[0]; + cin >> hours[1]; + cin >> hours[2]; + cin >> hours[3]; + cin >> hours[4]; + cin >> hours[5]; + + // Display the values in the array. + cout << "The hours you entered are: "; + cout << " " << hours[0]; + cout << " " << hours[1]; + cout << " " << hours[2]; + cout << " " << hours[3]; + cout << " " << hours[4]; + cout << " " << hours[5] << endl; + + return 0; +} \ No newline at end of file diff --git a/Chapter-7-Arrays/7.2 Accessing Array Elements/Examples/7-2.cpp b/Chapter-7-Arrays/7.2 Accessing Array Elements/Examples/7-2.cpp new file mode 100644 index 0000000..31dc89e --- /dev/null +++ b/Chapter-7-Arrays/7.2 Accessing Array Elements/Examples/7-2.cpp @@ -0,0 +1,24 @@ +// This program asks for the number of hours worked +// by six employees. It stores the values in an array. +#include +using namespace std; +int main() +{ + const int NUM_EMPLOYESS = 6; + int hours[NUM_EMPLOYESS]; + int count; + + for( count = 0; count < NUM_EMPLOYESS; count++) + { + cout << "Enter the hours worked by employee " + << (count + 1) << ": "; + cin >> hours[count]; + } + + cout << "The hours you entered are: "; + for( count = 0; count < NUM_EMPLOYESS; count++) + cout << " " << hours[count]; + cout << endl; + + return 0; +} \ No newline at end of file diff --git a/Chapter-7-Arrays/7.2 Accessing Array Elements/Examples/7-3.cpp b/Chapter-7-Arrays/7.2 Accessing Array Elements/Examples/7-3.cpp new file mode 100644 index 0000000..55376a9 --- /dev/null +++ b/Chapter-7-Arrays/7.2 Accessing Array Elements/Examples/7-3.cpp @@ -0,0 +1,25 @@ +#include +#include +using namespace std; + +int main() +{ + const int ARRAY_SIZE = 10; + int numbers[ARRAY_SIZE]; + int count = 0; + ifstream inputFile; + + inputFile.open("numbers.txt"); + + while (count < ARRAY_SIZE && inputFile >> numbers[count]) + count++; + + inputFile.close(); + + cout << "The numbers are: "; + for (count = 0; count < ARRAY_SIZE; count++) + cout << numbers[count] << " "; + cout << endl; + + return 0; +} \ No newline at end of file diff --git a/Chapter-7-Arrays/7.2 Accessing Array Elements/Examples/7-4.cpp b/Chapter-7-Arrays/7.2 Accessing Array Elements/Examples/7-4.cpp new file mode 100644 index 0000000..a2c9263 --- /dev/null +++ b/Chapter-7-Arrays/7.2 Accessing Array Elements/Examples/7-4.cpp @@ -0,0 +1,27 @@ +// This program write the contents of an array to a file. +#include +#include +using namespace std; + +int main() +{ + const int ARRAY_SIZE = 10; + int numbers[ARRAY_SIZE]; + int count; + ofstream outputFile; + + for (count = 0; count < ARRAY_SIZE; count++) + numbers[count] = count; + + outputFile.open("SavedNumbers.txt"); + + for (count = 0; count < ARRAY_SIZE; count++) + outputFile << numbers[count] << endl; + + outputFile.close(); + + cout << "The numbers were saved to the file.\n"; + + + return 0; +} diff --git a/Chapter-7-Arrays/Writing-the-Contents-of-an-Array-to-a-File/SavedNumbers.txt b/Chapter-7-Arrays/7.2 Accessing Array Elements/Examples/SavedNumbers.txt similarity index 100% rename from Chapter-7-Arrays/Writing-the-Contents-of-an-Array-to-a-File/SavedNumbers.txt rename to Chapter-7-Arrays/7.2 Accessing Array Elements/Examples/SavedNumbers.txt diff --git a/Chapter-7-Arrays/7.2 Accessing Array Elements/Examples/TenNumbers.txt b/Chapter-7-Arrays/7.2 Accessing Array Elements/Examples/TenNumbers.txt new file mode 100644 index 0000000..e28f71f --- /dev/null +++ b/Chapter-7-Arrays/7.2 Accessing Array Elements/Examples/TenNumbers.txt @@ -0,0 +1,10 @@ +25 +52 +66 +65 +23 +89 +76 +35 +1 +54 \ No newline at end of file diff --git a/Chapter-7-Arrays/7.2 Accessing Array Elements/Examples/numbers.txt b/Chapter-7-Arrays/7.2 Accessing Array Elements/Examples/numbers.txt new file mode 100755 index 0000000..7d2cdb3 --- /dev/null +++ b/Chapter-7-Arrays/7.2 Accessing Array Elements/Examples/numbers.txt @@ -0,0 +1,12 @@ +47 +89 +65 +36 +12 +25 +17 +8 +62 +10 +87 +62 diff --git a/Chapter-7-Arrays/Inputting-and-Outputting-Array-Contents/7-1.cpp b/Chapter-7-Arrays/Other/Inputting-and-Outputting-Array-Contents/7-1.cpp similarity index 100% rename from Chapter-7-Arrays/Inputting-and-Outputting-Array-Contents/7-1.cpp rename to Chapter-7-Arrays/Other/Inputting-and-Outputting-Array-Contents/7-1.cpp diff --git a/Chapter-7-Arrays/Inputting-and-Outputting-Array-Contents/7-2.cpp b/Chapter-7-Arrays/Other/Inputting-and-Outputting-Array-Contents/7-2.cpp similarity index 100% rename from Chapter-7-Arrays/Inputting-and-Outputting-Array-Contents/7-2.cpp rename to Chapter-7-Arrays/Other/Inputting-and-Outputting-Array-Contents/7-2.cpp diff --git a/Chapter-7-Arrays/Inputting-and-Outputting-Array-Contents/checkpoint-7-7.cpp b/Chapter-7-Arrays/Other/Inputting-and-Outputting-Array-Contents/checkpoint-7-7.cpp similarity index 100% rename from Chapter-7-Arrays/Inputting-and-Outputting-Array-Contents/checkpoint-7-7.cpp rename to Chapter-7-Arrays/Other/Inputting-and-Outputting-Array-Contents/checkpoint-7-7.cpp diff --git a/Chapter-7-Arrays/Reading-Data-from-a-File-into-an-Array/7-3.cpp b/Chapter-7-Arrays/Other/Reading-Data-from-a-File-into-an-Array/7-3.cpp similarity index 100% rename from Chapter-7-Arrays/Reading-Data-from-a-File-into-an-Array/7-3.cpp rename to Chapter-7-Arrays/Other/Reading-Data-from-a-File-into-an-Array/7-3.cpp diff --git a/Chapter-7-Arrays/Reading-Data-from-a-File-into-an-Array/TenNumbers.txt b/Chapter-7-Arrays/Other/Reading-Data-from-a-File-into-an-Array/TenNumbers.txt similarity index 100% rename from Chapter-7-Arrays/Reading-Data-from-a-File-into-an-Array/TenNumbers.txt rename to Chapter-7-Arrays/Other/Reading-Data-from-a-File-into-an-Array/TenNumbers.txt diff --git a/Chapter-7-Arrays/Writing-the-Contents-of-an-Array-to-a-File/7-4.cpp b/Chapter-7-Arrays/Other/Writing-the-Contents-of-an-Array-to-a-File/7-4.cpp similarity index 100% rename from Chapter-7-Arrays/Writing-the-Contents-of-an-Array-to-a-File/7-4.cpp rename to Chapter-7-Arrays/Other/Writing-the-Contents-of-an-Array-to-a-File/7-4.cpp diff --git a/Chapter-7-Arrays/Other/Writing-the-Contents-of-an-Array-to-a-File/SavedNumbers.txt b/Chapter-7-Arrays/Other/Writing-the-Contents-of-an-Array-to-a-File/SavedNumbers.txt new file mode 100644 index 0000000..8b1acc1 --- /dev/null +++ b/Chapter-7-Arrays/Other/Writing-the-Contents-of-an-Array-to-a-File/SavedNumbers.txt @@ -0,0 +1,10 @@ +0 +1 +2 +3 +4 +5 +6 +7 +8 +9 diff --git a/Chapter-7-Arrays/array-initialization/7-6.cpp b/Chapter-7-Arrays/Other/array-initialization/7-6.cpp similarity index 100% rename from Chapter-7-Arrays/array-initialization/7-6.cpp rename to Chapter-7-Arrays/Other/array-initialization/7-6.cpp diff --git a/Chapter-7-Arrays/array-initialization/7-7.cpp b/Chapter-7-Arrays/Other/array-initialization/7-7.cpp similarity index 100% rename from Chapter-7-Arrays/array-initialization/7-7.cpp rename to Chapter-7-Arrays/Other/array-initialization/7-7.cpp diff --git a/Chapter-7-Arrays/array-initialization/7-8.cpp b/Chapter-7-Arrays/Other/array-initialization/7-8.cpp similarity index 100% rename from Chapter-7-Arrays/array-initialization/7-8.cpp rename to Chapter-7-Arrays/Other/array-initialization/7-8.cpp diff --git a/Chapter-7-Arrays/array-initialization/7-9.cpp b/Chapter-7-Arrays/Other/array-initialization/7-9.cpp similarity index 100% rename from Chapter-7-Arrays/array-initialization/7-9.cpp rename to Chapter-7-Arrays/Other/array-initialization/7-9.cpp diff --git a/InputValidation/000.cpp b/InputValidation/000.cpp new file mode 100644 index 0000000..5dcf08d --- /dev/null +++ b/InputValidation/000.cpp @@ -0,0 +1,77 @@ +#include +#include // isdigit() +#include // stringstream +using namespace std; + +int main() +{ + string user_string_num = ""; + + int is_num = 0, + decimal_count = 0, + user_converted_num; + + bool is_num_bool = 0; + + do + { + cout << "Enter a number: "; + cin >> user_string_num; + + if (user_string_num[0] == '-') // jesus -8 + is_num++; + + if (user_string_num[0] == '0' && + isdigit(user_string_num[1])) // 0934939 + is_num = 0; + else + { + for (int i = 0; i < user_string_num.size(); i++) + { + if (isdigit(user_string_num[i])) + is_num++; + if (user_string_num[i] == '.') + decimal_count++; + } + } + + if (decimal_count == 1) // 66.7 // 8..9.9.9..9.9 + is_num = 0; + + if (is_num == user_string_num.size()) + { + stringstream str_stream_object(user_string_num); + str_stream_object >> user_converted_num; + + is_num_bool = 1; + + cout << endl + << user_string_num << "(user_string_num) " + << "is a number!" << endl + << user_converted_num << "(user_converted_num) " + << "is a number!" << endl + << endl; + } + else + { + cout << endl + << "Number must NOT contain spaces.\n" + << "Number must NOT contain letters.\n" + << "Number must NOT contain symbols.\n" + << "Number must NOT be a decimal number.\n" + << endl; + + cin.clear(); + cin.ignore(numeric_limits::max(), '\n'); + + is_num_bool = 0; + is_num = 0; + decimal_count = 0; + } + + + } while (is_num_bool == 0); + + + return 0; +} \ No newline at end of file diff --git a/InputValidation/validate-int.cpp b/InputValidation/validate-int.cpp new file mode 100644 index 0000000..5dcf08d --- /dev/null +++ b/InputValidation/validate-int.cpp @@ -0,0 +1,77 @@ +#include +#include // isdigit() +#include // stringstream +using namespace std; + +int main() +{ + string user_string_num = ""; + + int is_num = 0, + decimal_count = 0, + user_converted_num; + + bool is_num_bool = 0; + + do + { + cout << "Enter a number: "; + cin >> user_string_num; + + if (user_string_num[0] == '-') // jesus -8 + is_num++; + + if (user_string_num[0] == '0' && + isdigit(user_string_num[1])) // 0934939 + is_num = 0; + else + { + for (int i = 0; i < user_string_num.size(); i++) + { + if (isdigit(user_string_num[i])) + is_num++; + if (user_string_num[i] == '.') + decimal_count++; + } + } + + if (decimal_count == 1) // 66.7 // 8..9.9.9..9.9 + is_num = 0; + + if (is_num == user_string_num.size()) + { + stringstream str_stream_object(user_string_num); + str_stream_object >> user_converted_num; + + is_num_bool = 1; + + cout << endl + << user_string_num << "(user_string_num) " + << "is a number!" << endl + << user_converted_num << "(user_converted_num) " + << "is a number!" << endl + << endl; + } + else + { + cout << endl + << "Number must NOT contain spaces.\n" + << "Number must NOT contain letters.\n" + << "Number must NOT contain symbols.\n" + << "Number must NOT be a decimal number.\n" + << endl; + + cin.clear(); + cin.ignore(numeric_limits::max(), '\n'); + + is_num_bool = 0; + is_num = 0; + decimal_count = 0; + } + + + } while (is_num_bool == 0); + + + return 0; +} \ No newline at end of file diff --git a/InputValidation/validate-integer-as-string.3.INT.cpp b/InputValidation/validate-integer-as-string.3.INT.cpp index 53ad65f..188a8a4 100644 --- a/InputValidation/validate-integer-as-string.3.INT.cpp +++ b/InputValidation/validate-integer-as-string.3.INT.cpp @@ -49,7 +49,7 @@ int main() stringstream str_stream_object(user_string_num); str_stream_object >> user_converted_num; - if (is_num == user_string_num.size() /* [&& user_converted_num > 0] -- USE IF less than or greater than */ ) + if (is_num == user_string_num.size() ) { is_num_bool = 1;