Vous êtes sur la page 1sur 2

// Assignment2.cpp : Defines the entry point for the console application.

//
#include "stdafx.h" //precompiled header, unique to Visual Studio 2015. Remove t
his if you're using a different IDE
#include <iostream>
using namespace std;
int main()
{
//This section instantiates and initializes the variables that are calle
d throughout the function.
int retailPrice = 0; //We have instantiated a variable "retailPrice" of
data type "int" and initialized it to 0.
double discountPrice = 0; //Instantiation" means creating the variable,
i.e "double discountPrice".
int salesTaxPct = 0; //"Initialization means setting the variable to som
e value, i.e " =0;".
double totalDiscount = 0; //It is good practice to instantiate and initi
alize values at the very beginning of a code file--others will be looking for in
puts and outputs of your program.
double totalTax = 0;
double finalPrice = 0;
//This section takes user input to replace the initialized value of reta
ilPrice to whatever the user (you) input into the console when the program runs.
cout << "This is the beginning of my most amazing program ever. It will
do taxes!" << endl;
cout << "To begin, enter a sales price for an item to be purchased (-999
to exit)" << endl;
cin >> retailPrice;
cin.clear(); //I believe this is doing some type of "garbage" disposal.
Probably too high level for you now, but it helps the computer from collecting t
oo much junk in its memory.
cin.ignore(10000, '\n'); //This is ignoring all new characters you input
until a newline exists. So it doesn't pick up any digits after the first one yo
u enter
if (retailPrice == -999) //this is an if-conditional statement that says
"If the user hits -999 as an input, then we will jump to the "finishup" section
and ignore any code up to that section.
{
goto finishup;
}
//This section is taking the discount price--similar to above.
cout << "Enter a discount percentage for this wonderful and lovely item,
my dearest." << endl;
cin >> discountPrice;
cin.clear();
cin.ignore(10000, '\n');
//This section is taking the sales tax--similar to above
cout << "Enter the sales tax for this wonderful and lovely item, my dear
est." << endl;
cin >> salesTaxPct;
cin.clear();
cin.ignore(10000, '\n');
//This section is doing the calculation and math to get your final answe
r.

totalDiscount = retailPrice * (discountPrice / 100);


totalTax = (retailPrice - totalDiscount) * (salesTaxPct / 100);
finalPrice = retailPrice - totalDiscount + totalTax;
//Display your final answer.
cout << "The final, absolute, non-negotiable, and no-bullshit price is:"
<< finalPrice << endl;
getchar(); //this little method just stops the console window from autom
atically closing. If you remove it the console box immediately closes before you
can see the finalPrice.
finishup:
cout << "Just gonna end myself." << endl; //finishup statement for the a
forementioned if-conditional statement. Program ends itself and returns 0.
return 0;
}

Vous aimerez peut-être aussi