1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
|
// M4A1 Math expressions
#include <iostream>
#include <iomanip>
#include <limits>
int main () // initiates the source code
{
std::cout << std::setprecision (2);
std::cout << std::fixed << std::showpoint;
double unitPrice, quantityPurchased, purchasePrice, salesTax;
char stop;
do
{
do
{
std::cout << "Enter a unit price for product between $0 and $1000: \n "; // tells the user to to enter the value
std::cin >> unitPrice; // accepts user input for the variable unitPrice, and stores it as such
if (!std::cin) //checks the user input for a non integer type value, if an integer is input, then it does not fail, so the if statement returns why, and then breaks the loop.
{
std::cout << "Invalid Input\n"; // exits the if statement and then y is evaluated in the do while loop.
std::cin.clear(); // <--- Clears the state bits on the stream.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires heder file <limits>. Clears the input buffer.
std::cout << "Enter a unit price for product between $0 and $1000: \n "; // tells the user to to enter the value
std::cin >> unitPrice;
}
}
while (!std::cin || (unitPrice < 0 || unitPrice > 1000)); // this loop will continue to execute until a number is entered that is between 0 and 1000.
// once the input satisfies the conditions, the program moves onto the next step.
do // this do loop will execute until a positive value is entered
{
std::cout << "Enter the amount of product purchased, this cannot be a negative number: "; // tells the user to input a positive number
std::cin >> quantityPurchased; // accepts user input for the variable quantityPurchased, and stores it as such
if (!std::cin) //checks the user input for a non integer type value, if an integer is input, then it does not fail, so the if statement returns why, and then breaks the loop.
{
std::cout << "Invalid Input\n" <<std::endl; // exits the if statement and then y is evaluated in the do while loop.
std::cin.clear(); // <--- Clears the state bits on the stream.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires heder file <limits>. Clears the input buffer.
std::cout << "Enter the amount of product purchased, this cannot be a negative number: ";
std::cin >> quantityPurchased;
}
}
while (!std::cin || quantityPurchased <= 0); // loop will end once a positive number is entered for quantity and the program will continue
purchasePrice = unitPrice * quantityPurchased; // variable purchasePrice is calculated and stored
salesTax = purchasePrice * .05; // variable salesTax is calculated and stored, sales tax is 5%
std::cout << "The purchase price is: $" << purchasePrice << "\n" << "The sales tax on the pruchase is: $" << salesTax << "\n" << "The total cost is: $" << salesTax + purchasePrice << std::endl;
// program outputs the data for the transaction to the user displaying purchase price, sales tax, and total cost
do
{
std::cout << "If you would like to continue enter y or Y, to quit enter n or N: "; // tells the user to input a character value so the program can evaluate if the user would like to continue
std::cin >> stop;
if (!std::cin.fail()) //checks the user input for a non integer type value, if an integer is input, then it does not fail, so the if statement returns why, and then breaks the loop.
{
std::cout << "Invalid Input\n"; // exits the if statement and then y is evaluated in the do while loop.
std::cin.clear(); // <--- Clears the state bits on the stream.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires heder file <limits>. Clears the input buffer.
}
}
while (!std::cin.fail() && stop != 'y' && stop != 'Y' && stop != 'N' && stop != 'n');
}
while (stop != 'n' && stop != 'N'); // finishing up the first do loop, based on user input, the program can start over and repeat if they enter any character of then n or N, once complete the program moves onto the next segement
int num, i, min, max, product; // sets those variables as integer, and allocates memory for them
do // allows the user to try the multiplication table as many times as they want before
{
do
{
std::cout << "Enter any integer then press enter (only enter numbers without decimals): " << std::endl; // tells the user to input an integer
std::cin >> num; // accepts the user input for the variable num and stores it for later use
if (!std::cin) //checks the user input for a non integer type value, if an integer is input, then it does not fail, so the if statement returns why, and then breaks the loop.
{
std::cout << "Invalid Input\n" <<std::endl; // exits the if statement and then y is evaluated in the do while loop.
std::cin.clear(); // <--- Clears the state bits on the stream.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires heder file <limits>. Clears the input buffer.
std::cout << "Enter any integer then press enter: ";
std::cin >> num;
}
}
while (!std::cin);
do
{
std::cout << "Enter two numbers, a low value and a high value then press enter: " << std::endl; // tells the user to input a max and min value
std::cin >> min >> max; //accepts the user input for the variables min and max
if (!std::cin) //checks the user input for a non integer type value, if an integer is input, then it does not fail, so the if statement returns why, and then breaks the loop.
{
std::cout << "Invalid Input\n" <<std::endl; // exits the if statement and then y is evaluated in the do while loop.
std::cin.clear(); // <--- Clears the state bits on the stream.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires heder file <limits>. Clears the input buffer.
std::cout << "Enter two numbers, a low value and a high value then press enter:";
std::cin >> min >> max;
}
}
while (!std::cin);
for (i= min; i <= max; i++) // This for statement tells the computer to repeat the program if i <= max, once the max value specified by the user is reached the program runs through the code one last time and finished
// i = min, so the program inserts at this point, for example user inputs 1 for min and 2 for max, program starts at 1, runs through the code, reaches two, runs through and then completes the loop.
{
product = num * min; // defines product as user input num * user input min
std::cout << num << " x " << min << " = " << product << std::endl; // displays num * min = product
min++; // after the code is complete it adds 1 to the value of min, after the last run the min > max, and the loop will not run again
}
do
{
std::cout << "If you would like to continue enter y or Y, to quit enter n or N: "; // tells the user to input a character value so the program can evaluate if the user would like to continue
std::cin >> stop;
if (!std::cin) //checks the user input for a non integer type value, if an integer is input, then it does not fail, so the if statement returns why, and then breaks the loop.
{
std::cout << "Invalid Input\n"; // exits the if statement and then y is evaluated in the do while loop.
std::cin.clear(); // <--- Clears the state bits on the stream.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires heder file <limits>. Clears the input buffer.
}
}
while (!std::cin && stop != 'y' && stop != 'Y' && stop != 'N' && stop != 'n');
}
while (stop != 'n' && stop != 'N');
return 0; // terminates the program normally
}
|