BEFORE YOU START: The professor highly recommends you type these out, in this blog I quickly forgot that typing is also a good method of learning for beginner programmers, try typing the code and analyze it as you go. In this blog I suggest copy and paste but only do that if you are confident in learning the material in such a way.
Week 1
=Part A=
Step1:
Step One basically walks you step by step on how to create a new project to work on. Simply follow the steps one by one and you should be OK.
⦁ From the File menu, choose "New Project."
⦁ Choose “Win32 Console Application.”
⦁ Enter a name in the name field.
⦁ Click “OK”
⦁ Click “Next” and choose the following options:
⦁ Application Type: "Console Application"
⦁ Additional options: Check mark “Empty project”.
⦁ Click Finish. Your project is now created.
⦁ Choose “Win32 Console Application.”
⦁ Enter a name in the name field.
⦁ Click “OK”
⦁ Click “Next” and choose the following options:
⦁ Application Type: "Console Application"
⦁ Additional options: Check mark “Empty project”.
⦁ Click Finish. Your project is now created.
Step2:
After step1 you should have a project to work on but you don't have any code, and to write code you will need a source file (a file that contains your source code "The code you write"). Again this is a step by step process and simply following the steps, you should be fine.
⦁ In the Solution Explorer, right-click on the “Source Files” folder and select "Add" and then "New Item."
⦁ In the next dialog box, choose C++ file (.cpp), enter a name for your source code file, and press the Add button.
⦁ Type or copy and paste your code into the newly created source code file. Build the file by pressing F7, and then execute your program by pressing CTRL-F5 (start without debugging) or use the F5 key (Start Debugging).
At the end you will be asked to press F7 to build and then F5 to start debugging. Simple forget F7 and just press F5 as debugged actually builds the program in the process already. F5 is for debugging and as of yet in class we have yet to touch on that so I wont go into it.
⦁ In the next dialog box, choose C++ file (.cpp), enter a name for your source code file, and press the Add button.
⦁ Type or copy and paste your code into the newly created source code file. Build the file by pressing F7, and then execute your program by pressing CTRL-F5 (start without debugging) or use the F5 key (Start Debugging).
At the end you will be asked to press F7 to build and then F5 to start debugging. Simple forget F7 and just press F5 as debugged actually builds the program in the process already. F5 is for debugging and as of yet in class we have yet to touch on that so I wont go into it.
Step3:
The next part say to enter the following code EXACTLY as you see it:
#include <iostream>
using namespace std;
void main()
{
cout << "John Doe" << endl;
cout << "CIS170C - Programming using C++\n";
cout << "\n\n\nHello, world!\n\n";
}
but who has time to type that, simply copy and past it into your source file. You then have to change the name John Doe to your name, remember to leave the double quotes intact. Anything inside double quotes is considered a string, and a string is just a bunch of characters or if its easy its plain text. The next step says that the program will appear and disappear this is because once the program runs through all its steps it is complete and simply closes, you need to tell the program to not close and there are a number of ways to do this though simply typing system("pause"); will suffice apparently in this class and it looks easier to understand to beginners. So your code now should look something like this:
#include <iostream>
using namespace std;
void main()
{
cout << "YOUR NAME" << endl;
cout << "CIS170C - Programming using C++\n";
cout << "\n\n\nHello, world!\n\n";
system("pause");
}
Step4:
Simply states what the output should look like when you run the program (press F5 again).
John DoeThough this isn't true as you see in the code above there are multiple instances of \n which means NewLine. So in turn every time \n appears the cursor(blinking vertical line) should move down to the next line in the console(black box with white text in it), we also added the system("pause") so the actual output should look more like this:
CIS170C - Programming using C++
Hello, World
YOUR NAMETip: If you get a similar result to this, while the console or "command prompt" is selected Press Alt + Print-screen now and paste it into a new word document to be able to skip the repetitive steps later.
CIS170C - Programming using C++
Hello, world!
Press any key to continue . . .
Step5:
This part just tells you to save your project, but from my experience each time you debug it(press F5) the project is automatically saved, but in case I'm wrong just press Ctrl + S or what I do is press the little floppy disk in the top left corn of Visual Studios. There is also an icon what has two floppy disks, that is for saving projects with multiple source files etc, for the time being we are only working with only one source file but you can still alternatively press the double floppy icon.
Step6:
This again in my opinion is useless If you followed the tip from step4 skip this step if not continue reading. When debugging the program an executable is already generated so why is the purpose of building it again unless you have made some changes to the source file? it also goes on to mentions errors etc, if errors had come up it would have came up already in steps before this one, if you did get errors you have made recent changes. If you made no changes since last debug simply skip this step.
Step7:
Again we debug it but if you followed my Tip from Step4 you can skip this Step, if not shame on you and continue reading. Simply press F5 again to debug the program again.
Step8:
If you followed my tip from step4.....skip, if not read on. Having the console or "command prompt" selected press Alt + Print-screen. Open a new word document and paste into it.
Step9:
Copy the source code and paste it also into the same word document and save it with the following file name format: Lab01A_LastName_FirstInitial (keeping the underscores).
You are now done and can close Visual Studios or Visual C++.
Note: During submission the professor wants both the word document as well as the project files.
The project files are located in you "My Documents" or "Documents" folder depending on your version of the windows operating system. In there is a folder called either "Visual C++", "Visual Studios 2010" or "Visual Studios 2012". In there is a folder called "Projects" and in that folder is where you will find the project files for your programs you have made and/or are currently working on.
Visual Studios 2012 project to 2010 Conversion:
Open your solution file (*.sln) in notepad. Make 2 changes (the * means what ever file name)
- Replace "Format Version 12.00" with "Format Version 11.00" (without quotes.)
- Replace "# Visual Studio 2012" with "# Visual Studio 2010" (without quotes.)
Here is a video I found on YouTube of someone doing it:
=Part B=
Step1:
Simple Create a new project a source file like in PartA.
Step2:
Again we are lazy or what I'd rather consider myself efficient and copy the following source code into your source file.
// ---------------------------------------------------------------
// Programming Assignment: LAB1B
// Developer: ______________________
// Date Written: ______________________
// Purpose: Ticket Calculation Program
// ---------------------------------------------------------------
#include <iostream>
using namespace std;
void main()
{
int childTkts, adultTkts, totalTkts;
childTkts = 3;
adultTkts = 2;
totalTkts = childTkts + adultTkts;
cout << totalTkts << endl;
}
Change the Developer to your name and also set the date. The final result should look simething like this:
// ---------------------------------------------------------------
// Programming Assignment: LAB1B
// Developer: YOUR NAME
// Date Written: DATE
// Purpose: Ticket Calculation Program
// ---------------------------------------------------------------
#include <iostream>
using namespace std;
void main()
{
int childTkts, adultTkts, totalTkts;
childTkts = 3;
adultTkts = 2;
totalTkts = childTkts + adultTkts;
cout << totalTkts << endl;
}
// Programming Assignment: LAB1B
// Developer: YOUR NAME
// Date Written: DATE
// Purpose: Ticket Calculation Program
// ---------------------------------------------------------------
#include <iostream>
using namespace std;
void main()
{
int childTkts, adultTkts, totalTkts;
childTkts = 3;
adultTkts = 2;
totalTkts = childTkts + adultTkts;
cout << totalTkts << endl;
}
Step3:
Remember that awesome little floppy icon on the top left, yeah? well click it.
Step4:
Press F5.
Step5:
Skip it.
Step6:
Alt + Print-screen, Paste into a new word document along with the code and name it with the following format: Lab01B_LastName_FirstInitial (keeping the underscores).
All done.
=Part C=
Step1:
At this point I shouldn't have to tell you to make a new project and a new source file, it should come naturally like eating, pooping, peeing, sleeping, and hopefully showering, I mean the concept of soap and water isn't hard, neither should this.
At this point I shouldn't have to tell you to make a new project and a new source file, it should come naturally like eating, pooping, peeing, sleeping, and hopefully showering, I mean the concept of soap and water isn't hard, neither should this.
Step2:
Aws no copy and paste this time around, well TOO BAD we still gonna rock this assignment. Lets do it. Ok the task is to calculate and display some text and numbers in according to some everyday stuff.....yeah I don't know this crap because I still live in my moms basement.......Good thing there is an output sample and diagram provided that makes our lives that much easier....well at least on this assignment, though the sample is off from what the actual result is (we'll get into that later).
Enter Weekly Sales: 28000
Total Sales: 28000.00
Gross pay (7%): 1960.00
Federal tax paid: 352.80
Social security paid: 117.60
Retirement contribution: 196.00
Total deductions: 666.40
Take home pay: 1293.60
Press any key to continue . . .
But because I found that the Diagram was missing a step I edited it so its more clear and understandable to beginners.
uwww look at all the pretty shapes....we will be working on this one shape at a time. First the oval. The oval can be consider the step where you setup your project, including libraries and namespaces and the main function. So your project should the look something like this:
#include <iostream>
using namespace std;
void main()
{
}
PS: I will be using void main until the assignments switch over to int main.
The next part (first rectangle) is kind of misleading as I initially interpreted it as declaring everything as global. Global variables are variables that are outside functions and they can be accessed from anywhere else in your program, but for this simple program it doesn't really matter since we will only have one main function. Raise your hand if you think you know how to declare variables......put your hand down I can't see it anyways, if you know then skip this paragraph, if not read on. Variables are data and specific data at that. To declare a variable you first specify the type, for example you can use integer for whole numbers, string for plain text, boolean for a value that is true or false, float which is a decimal number to a specific decimal place, double which is like float but is more precise as it can store decimal numbers with more digits after the decimal place. Then the next thing is you want to give it a name, just like giving a name to a pet you will find that you will be calling this name to distinguish it from the rest. The thing about C++ is that it is cap sensitive and that means you can have variables with the same name but with different capitalization, for example TizzyT is not the same as tizzyt and is not the same as tiZZyt and not the same as....you get the idea. Lastly you can choose to give your variable an initial value, this is like mutant messenger pigeons that come once their names are called (smart ass pigeons), you can get a new pigeon, and not give it a message, but you will still have that pigeon to give a message to later on. So to recap variable are declared by first specifying type, then given a name, and then optionally given an initial value.
Here is a variable that is declared with no initial value:
int Hungry;
Here is a variable that is declared with an initial value:
int Bloated = 9000;
Also do not forget the semicolon as the end.
Now that we have that out of the way lets continue. the syntax in the box looks a lot like VB (another programming language) so converting it, one can use float or double in place of decimal. Do that for each variable, you should now have something like this:
#include <iostream>
using namespace std;
void main()
{
float weeklySales;
float grossPay;
float fedTax;
float socSecurity;
float retirement;
float totDeductions;
float takeHomePay;
}
Alternatively it can also look like this:
#include <iostream>
using namespace std;
void main()
{
float weeklySales,
grossPay,
fedTax,
socSecurity,
retirement,
totDeductions,
takeHomePay;
}
Now this part is where I added that extra rhombus thinga-mahjig, so that we don't have to work backwards later on like we would have to do if we followed the original diagram. Here we are basically asking the user for an input that is the number of sales he made in a week. For that we use cout which is short for C out, hehe pretty clever huh...no? oh wells moving on. We first use cout followed by << and then the text you want to display that is between two double quotes. Don't for get the semi colon, in this instance we are not using the endl because we still desire to work on the same line we are currently on. So the code you should have should look something like this:
#include <iostream>
using namespace std;
void main()
{
float weeklySales;
float grossPay;
float fedTax;
float socSecurity;
float retirement;
float totDeductions;
float takeHomePay;
cout << "Enter Weekly Sales: $";
}
Next is another rhombus looking du-hicky, It says "input weeklySales" where input means that the program is expecting the user of the program to put something in, and where weeklySales is the name of one of our smart ass pigeons I mean variables, basically its saying the user will assign a value to the variable named weeklySales. we do this with cin short for C in, get it? (hey writing this isn't exact fun you know) followed by >> and then the name of the variable weeklySales. Your code should look like this at this point:
#include <iostream>
using namespace std;
void main()
{
float weeklySales;
float grossPay;
float fedTax;
float socSecurity;
float retirement;
float totDeductions;
float takeHomePay;
cout << "Enter Weekly Sales: $";
cin >> weeklySales;
}
Next we go onto the next rectangle, it says "grossPay = weeklySales * .07". OK its saying the variable named grossPay will be assigned a value of weeklySales mulitplied by .07. First it looks at the variable weeklySales and looks what value it has then it does the multiplication then assigns grossPay with that resulting value. The next five rectangles are pretty much the same idea. Your code should now look something like this:
#include <iostream>
using namespace std;
void main()
{
float weeklySales;
float grossPay;
float fedTax;
float socSecurity;
float retirement;
float totDeductions;
float takeHomePay;
cout << "Enter Weekly Sales: $";
cin >> weeklySales;
grossPay = weeklySales * 0.07;
fedTax = grossPay * 0.18;
socSecurity = grossPay * 0.06;
retirement = grossPay * 0.1;
totDeductions = fedTax + socSecurity + retirement;
takeHomePay = grossPay - totDeductions;
}
OK now that all of our variables have values lets move onto the last rhombus what-chu-ma-call-it. It says....you can read scroll up. Basically it wants to output all the information we have onto the console so that the user can see it and along with a description for each piece of information, each on its own line, NOT A FREAKING PROBLEM. Again we are going to use the cout in combination with the information we have in our variables, but before all that we have to cout a special line:
cout << fixed << setprecision(2);
This one line is used to specify how many digits to show after the decimal point and because cent in currency takes up 2 digits as in 00 to 99 we specify 2 as a parameter. You will also need to include a new #include line at the top to be able to use this:
#include <iomanip>
Now we can go a head and cout our information.
this is how each line should look like:
Cout << "Description Here" << variableHere;
So in your code should look something like this:
Alternatively it can also look like this:#include <iostream>
#include <iomanip>
using namespace std;
void main()
{
float weeklySales;
float grossPay;
float fedTax;
float socSecurity;
float retirement;
float totDeductions;
float takeHomePay;
cout << "Enter Weekly Sales: $";
cin >> weeklySales;
grossPay = weeklySales * 0.07;
fedTax = grossPay * 0.18;
socSecurity = grossPay * 0.06;
retirement = grossPay * 0.1;
totDeductions = fedTax + socSecurity + retirement;
takeHomePay = grossPay - totDeductions;
cout << fixed << setprecision(2);
cout << "\nTotal Sales:\t\t $" << weeklySales;
cout << "\nGross pay (7%):\t\t $" << grossPay;
cout << "\n\nFederal tax paid:\t $" << fedTax;
cout << "\nSocial security paid:\t $" << socSecurity;
cout << "\nRetirement contribution: $" << retirement;
cout << "\nTotal deductions:\t $" << totDeductions;
cout << "\n\nTake home pay:\t\t $" << takeHomePay << endl;
}
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
float weeklySales,
grossPay,
fedTax,
socSecurity,
retirement,
totDeductions,
takeHomePay;
cout << "Enter Weekly Sales:\t $";
cin >> weeklySales;
grossPay = weeklySales * 0.07;
fedTax = grossPay * 0.18;
socSecurity = grossPay * 0.06;
retirement = grossPay * 0.1;
totDeductions = fedTax + socSecurity + retirement;
takeHomePay = grossPay - totDeductions;
cout << fixed << setprecision(2)
<< "\nTotal Sales:\t\t $" << weeklySales
<< "\nGross pay (7%):\t\t $" << grossPay
<< "\n\nFederal tax paid:\t $" << fedTax
<< "\nSocial security paid:\t $" << socSecurity
<< "\nRetirement contribution: $" << retirement
<< "\nTotal deductions:\t $" << totDeductions
<< "\n\nTake home pay:\t\t $" << takeHomePay << endl;
}
The reason why I only have an endl at the very end is because at the beginning of every description I have a \n and that means a new line, some have 2 \n and that is because there is double spacing (refer to the sample to see were double spacing comes in). You will also notice the \t, it represents a horizontal tab. If you already know what it is skip this, if not read on. If you were to imagine the console being split up into pieces the beginning of each piece is a tab. Anything to the right of \t will be started at the next tab, or if you put \t\t it will be the 2 tabs away or if \t\t\t it will be 3 tabs away. Here is an image that might help:
Finally we add a system("pause"); so that we can see the output. Your code should look something like this when completed:
In the console input 28000 and press enter, Alt + Print-screen and paste it into a new word document along with the source code.#include <iostream>
#include <iomanip>
using namespace std;
void main()
{
float weeklySales;
float grossPay;
float fedTax;
float socSecurity;
float retirement;
float totDeductions;
float takeHomePay;
cout << "Enter Weekly Sales: $";
cin >> weeklySales;
grossPay = weeklySales * 0.07;
fedTax = grossPay * 0.18;
socSecurity = grossPay * 0.06;
retirement = grossPay * 0.1;
totDeductions = fedTax + socSecurity + retirement;
takeHomePay = grossPay - totDeductions;
cout << fixed << setprecision(2);
cout << "\nTotal Sales:\t\t $" << weeklySales;
cout << "\nGross pay (7%):\t\t $" << grossPay;
cout << "\n\nFederal tax paid:\t $" << fedTax;
cout << "\nSocial security paid:\t $" << socSecurity;
cout << "\nRetirement contribution: $" << retirement;
cout << "\nTotal deductions:\t $" << totDeductions;
cout << "\n\nTake home pay:\t\t $" << takeHomePay << endl;
system("pause");
}
Gather the project folders for A,B and C and also the 3 word documents and zip them altogether for submission.
Week1 DONE!!!
Week 2
Note: Keep in mind this is just how I did it, this assignment was yet to be graded so I do not know if
it was correct.
=Part A=
Step1:
This step states what the program should do and then shows a sample output. You are to be able to input two numbers and then from those two numbers determine and output the smallest number, and if equal display a message stating so. The sample output:
You will be asked to enter two numbers.
The smallest value will be displayed or a message if they are the same.
Please enter a numeric value: 4
Please enter a numeric value: 7
The smallest value is 4
Press any key to continue . . .
AND THEN:
Please enter a numeric value: 7Please enter a numeric value: 4The smallest value is 4
Press any key to continue . . .
shows that the program was ran twice with the inputs first being 4 and 7 and the determined lowest number is 4, then the second time the input is 7 and 4 with the lowest determined number being 4.
Step2:
This step shows you a quick pseudo-code you will be using as a guide in making this program meet requirements.
Display description of program
Prompt the user for the first number
Prompt the user for the second number
If first number equals second number
Display the two numbers are equal
Else
If first number is greater than second number
Assign second number to smallest
Else
Assign first number to smallest
End-If
Display smallest number
End-If
Step3:
This is week 2 so we should all know how to create a project, so so with the specified name LAB2A.
(Since the assignment isn't step by step I will be using step 3 to show the actual coding steps)
Lets first prepare our c++ file:
#include <iostream>
using namespace std;
void main(){
}
Now we can start programming in main.
Lets step through the program one pseudo-code at a time.
The first line says:
Display description of program
Now if we go back up to the sample output, we can see that the program description is there, so we can just use that:
You will be asked to enter two numbers.
The smallest value will be displayed or a message if they are the same.
We are also in the second line of the pseudo-code still outputting so we can just continue to cout that line as well:
Please enter a numeric value:
So because we are outputting we will be using cout, so your code should look like this:
#include <iostream>
using namespace std;
void main(){
cout << "You will be asked to enter two numbers.\n"
<< "The smallest value will be displayed or a message if they are the same.\n"
<< "Please enter a numeric value: ";
}
The \n\n is because as you can see in the sample output, there is an empty line.
The next part we need to get and store the first number that the user inputs, so we will be needing to make a new variable to store the values. It seems we are working with integers so lets just make 2 integer variables, the second one being for the second value later. the first cin should be stored into the first variable and then the prompt comes up for the second value, the second one stored into the second variable so your code should now look something like this:
#include <iostream>
using namespace std;
void main(){
int firstNbr, secondNbr;
cout << "You will be asked to enter two numbers.\n"
<< "The smallest value will be displayed or a message if they are the same.\n\n"
<< "Please enter a numeric value: ";
cin >> firstNbr;
cout << "Please enter a numeric value: ";
cin >> secondNbr;
}
Note: The professor wants us to always initialize variables with a value, here I didn't so if you want to give them a value simple make this small change:
int firstNbr = 0, secondNbr = 0;
Now that we got the prompting and setting the values done we can move onto some simple logic that will determine which of the two numbers is the smallest or if they are equal.
in the pseudo-code it wants us to check for if the two numbers are equal first so we can use a simple if statement to determine whether this is true or false. If they are equal you want to display a message using cout. Your code should look something like this:
#include <iostream>
using namespace std;
void main(){
int firstNbr = 0, secondNbr = 0;
cout << "You will be asked to enter two numbers.\n"
<< "The smallest value will be displayed or a message if they are the same.\n\n"
<< "Please enter a numeric value: ";
cin >> firstNbr;
cout << "Please enter a numeric value: ";
cin >> secondNbr;
if (firstNbr == secondNbr) cout << "First number " << firstNbr << " is equal second number " << secondNbr << endl;
}
The cout is on the same line as the if statement because it is only one line of code and so this format is allowed.
Now because we need to check for either if the first value is smaller than the second or vice versa we can either add more if statements or simply extend the existing if statement into an if,elseif statement.
Your code should look something like this:
#include <iostream>
using namespace std;
void main(){
int firstNbr = 0, secondNbr = 0;
cout << "You will be asked to enter two numbers.\n"
<< "The smallest value will be displayed or a message if they are the same.\n\n"
<< "Please enter a numeric value: ";
cin >> firstNbr;
cout << "Please enter a numeric value: ";
cin >> secondNbr;
if (firstNbr == secondNbr) cout << "First number " << firstNbr << " is equal to second number " << secondNbr << endl;
else if (firstNbr < secondNbr) cout << "The smallest value is " << firstNbr << endl;
else if (secondNbr < firstNbr) cout << "The smallest value is " << secondNbr << endl;
}
The logic behind the if, else if, else if is as follows:
IF (first number is the same as the second number) THEN display "First number " what ever first number is "is equal to second number " what ever second number is, end of line.ELSE IF (first number is less than second number) THEN display "The smallest value is" what ever the first number is, end of line.ELSE IF (second number is less than first number) THEN display "The smallest value is" what ever the second number is, end of line.
Lastly we are going to use the system("pause"); so the program doesn't just close on us.
So your final code should look like this:
#include <iostream>
using namespace std;
void main(){
int firstNbr = 0, secondNbr = 0;
cout << "You will be asked to enter two numbers.\n"
<< "The smallest value will be displayed or a message if they are the same.\n\n"
<< "Please enter a numeric value: ";
cin >> firstNbr;
cout << "Please enter a numeric value: ";
cin >> secondNbr;
if (firstNbr == secondNbr) cout << "First number " << firstNbr << " is equal to second number " << secondNbr << endl;
else if (firstNbr < secondNbr) cout << "The smallest value is " << firstNbr << endl;
else if (secondNbr < firstNbr) cout << "The smallest value is " << secondNbr << endl;
system("pause");
}
Step4:
Debug your program (Press F5).
Step5:
Skip.
Step6:
Skip.
Step7:
This is the usual print screen and paste into word routine. but this time I print screen twice one for the 4,7 and the second time for 7,4. Paste your code into the document as well. Part A done.
=Part B=
Step1:
Like in part A just shows you the requirements and sample output.
Step2:
Shows the pseudo-code we will be using.
Step3:
Create new project called LAB2B.
At this point prepare your project.
Here you want to display the programs description, like Part A the description is shown in the output sample, you will also want to continue to output the prompt for amount as well and then input that amount into a variable, so before that lets make a double variable to store the amount in. Your code should look something like this:
#include <iostream>
using namespace std;
void main() {
double amount;
cout << "Enter a purchase amount to find out your shipping charges.\n\n"
<< "Please enter the amount of your purchase: $";
cin >> amount;}
Now that we have the outputs and inputs done we can do some simple logic like in Part A to output the shipping charges.
First though we have to make sure the user doesn't enter a number under 0.0, we can do that with a simple if statement. Your code should look like this now:
#include <iostream>
using namespace std;
void main() {
double amount;
cout << "Enter a purchase amount to find out your shipping charges.\n\n"
<< "Please enter the amount of your purchase: $";
cin >> amount;
if (amount <= 0.00)
cout << "Error: incorrect input\n" << endl;
}
Now we can continue this with and else statement for when the user inputs a value that is over 0.0.
In that statement we will have nested if statements, each one to output a different message, your code should now look like this:
#include <iostream>
#include <iomanip>
using namespace std;
void main() {
double amount;
cout << "Enter a purchase amount to find out your shipping charges.\n\n"
<< "Please enter the amount of your purchase: $";
cin >> amount;
if (amount <= 0.00) cout << "Error: incorrect input\n" << endl;
else {
cout << fixed << setprecision(2)
<< "The shipping charge on a purchase of $" << amount << " is $";
if (amount > 5000.00) cout << 20.00 << endl << endl;
else if (amount > 1000.00) cout << 15.00 << endl << endl;
else if (amount > 500.00) cout << 10.00 << endl << endl;
else if (amount > 250.00) cout << 8.00 << endl << endl;
else if (amount > 0.00) cout << 5.00 << endl << endl;
}
}
As you can see I've added the fixed and setprecision so that doubles will be displayed to the second digit after the decimal point, and to use setprecision we needed to include iomanip.
Lastly we add the system("pause"); and your code should now look something like this:
#include <iostream>
#include <iomanip>
using namespace std;
void main() {
double amount;
cout << "Enter a purchase amount to find out your shipping charges.\n\n"
<< "Please enter the amount of your purchase: $";
cin >> amount;
if (amount <= 0.00) cout << "Error: incorrect input\n" << endl;
else {
cout << fixed << setprecision(2)
<< "The shipping charge on a purchase of $" << amount << " is $";
if (amount > 5000.00) cout << 20.00 << endl << endl;
else if (amount > 1000.00) cout << 15.00 << endl << endl;
else if (amount > 500.00) cout << 10.00 << endl << endl;
else if (amount > 250.00) cout << 8.00 << endl << endl;
else if (amount > 0.00) cout << 5.00 << endl << endl;
}
system("pause");
}
Step4:
Press F5
Step5:
Skip
Step6:
Skip
Step7:
Print screen and save to word pad, including the source code.



