Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- using namespace std;
- bool divisibleByThree(int divisor) { // This funcion validates that the given integer is fully divisible by three
- if((divisor % 3) == 0) return true;
- else return false;
- }
- float divide(float operandA, float operandB) { // This function is used to define ratios of price/pound in the betterDeal function
- return (operandA / operandB);
- }
- void betterDeal(float priceOne, float priceTwo, float poundsOne, float poundsTwo) {
- float firstRatio = divide(priceOne, poundsOne), secondRatio = divide(priceTwo, poundsTwo);
- cout << setprecision(2) << fixed;
- cout << "Item 1 costs $" << firstRatio << " per pound, and" << endl;
- cout << "Item 2 costs $" << secondRatio << " per pound." << endl << endl;
- if(firstRatio < secondRatio) {
- cout << "Item 1 is the better deal!" << endl << endl;
- } else if(secondRatio < firstRatio) {
- cout << "Item 2 is the better deal!" << endl << endl;
- } else if(firstRatio == secondRatio) {
- cout << "Both items are the same per pound! They're BOTH a great deal!" << endl << endl;
- }
- }
- int main() {
- cout << "Counter-Controlled Loop Demonstration" << endl << endl;
- cout << "This is a counter loop that will display numbers on a line" << endl;
- cout << "based on what lower bound you provide to it." << endl << endl;
- cout << "To start, enter the lower bound as an integer." << endl;
- cout << "For this loop, the upper bound will be preset to 10, " << endl;
- cout << "and all numbers that are divisible by three will be excluded." << endl;
- int lowBound;
- cout << "Enter the lower bound: ";
- cin >> lowBound;
- int highBound = 10;
- cout << endl;
- for(int counter = highBound; counter >= lowBound; counter--) { // This counter uses the divisibleByThree function to include values that do not meet the function's conditions
- if(divisibleByThree(counter) == false) {
- cout << counter << " ";
- }
- }
- cout << endl << endl << "--------------------" << endl << endl;
- cout << "Character-Printing Function Demonstration" << endl << endl;
- cout << "This is a loop that will display the letters of the string" << endl;
- cout << "\"Hello World!\" in order, one line for each letter." << endl << endl;
- string message = "Hello World!";
- for(int index = 0; index < message.length(); index++) { // This loop uses the indexes of the given string to print them one-by-one per line
- cout << message[index] << endl;
- }
- cout << endl << "--------------------" << endl << endl;
- cout << "Better Deal Function Loop" << endl << endl;
- cout << "This is a loop that will run a function that will help you" << endl;
- cout << "find which of two given item-per-cost transactions are the better deal." << endl << endl;
- cout << "Would you like to find the better deal? (y/n): ";
- char input;
- cin >> input;
- while(input == 'Y' || input == 'y') { // As long as the given input is one of these two, the loop will continue to run
- float priceOne, priceTwo, poundsOne, poundsTwo;
- cout << endl << "Wonderful. To begin, enter the price of item 1, in U.S. dollars: $";
- cin >> priceOne;
- cout << "Next, enter its weight, in pounds: ";
- cin >> poundsOne;
- cout << "Now enter the price of item 2, in U.S. dollars: $";
- cin >> priceTwo;
- cout << "And its weight, in pounds: ";
- cin >> poundsTwo;
- cout << endl;
- if(priceOne > 0 && priceTwo > 0 && poundsOne > 0 && poundsTwo > 0) { // This condition validates that all given inputs are positive
- betterDeal(priceOne, priceTwo, poundsOne, poundsTwo);
- } else { // ... if one or more is negative, an error occurs.
- cout << "Error: One or more of your given prices or weights is" << endl;
- cout << "less than zero, and in order to run this function," << endl;
- cout << "all values must be greater than zero. Please try again." << endl << endl;
- }
- cout << "Would you like to find another great deal? (y/n): "; // Rerun the loop if the char inputs 'Y' or 'y' are met again
- cin >> input;
- if(input == 'N' || input == 'n') break; // Inputting the char 'N' or 'n' will terminate the loop
- }
- cout << endl << "Thank you for viewing these awesome loop demonstrations!" << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement