c & c++ lab assignment #1 first c or c++ lab paycheck-v1+paycheckv1.0-windows.… · 12/23/2013...
TRANSCRIPT
12/23/2013
1
C & C++ LAB ASSIGNMENT #1
First C or C++ Lab Paycheck-V1.0
Using Microsoft®Visual Studio®
Copyright © 2013 Dan McElroy
Paycheck-V1.0 The purpose of this lab assignment is to enter a C or C++ into Visual Studio and make it work. You can choose to create the project using either C or C++, or both, but you only get credit for one project.
All of the program code is already provided for you. You only need to create the project, enter the code, test the program and write the lab report.
As the course progresses, you will be required to create the entire project.
12/23/2013
2
Paycheck-V1.0
This presentation is in three parts: 1) Project Definition – Discussion of the project
and what is to be done. 2) Microsoft implementation in C or C++
Detailed description on how to enter your first program on the computer using Microsoft Visual Studio and make it work.
3) Project Documentation – Description of how to submit your lab report.
1 - Project Definition Design a program using C or C++ that does the following:
1) Read the number of hours worked and the pay rate
2) Compute the pay including overtime at time-and-a-half
3) Display the gross pay, not including taxes or any other deductions.
12/23/2013
3
2 - Paycheck – Develop Algorithm Develop an algorithm in pseudo-code. Use a HIPO (Hierarchical Input Process Output) chart to define the input, process and output for the program.
INPUT PROCESS OUTPUT
Hours PayRate
Read Hours from keyboard Read PayRate from keyboard Determine RegularHours (up to 40) Determine OvertimeHours (hours over 40) RegularPay = RegularHours * PayRate OvertimePay = OvertimeHours * PayRate * 1.5 GrossPay = RegularPay + OvertimePay Display GrossPay
GrossPay
3 – Outline What the Screen Should Look Like When the Program Runs
12/23/2013
4
4 – Convert the Algorithm into Code The #include files go here. The include files will be different for C and C++. They help the compiler build the program.
The request for input (prompt) and keyboard input go here. The C language uses the printf and scanf functions for output and input. C++ uses cout and cin.
The code to display the amount of the paycheck goes here. The C language uses printf and C++ uses cout.
C and C++ Input and Output Routines
C Language Input/Output scanf – formatted input scan pronounced Scan-F printf – print formatted pronounced Print-F (old computer terminals were printing TeleTypes) C++ Language Input/Output cin – Console IN-put pronounced See-In (not like SIN ) cout – Console OUT-put pronounced See-Out (not KOWT )
12/23/2013
5
5 – Test the Program
Since this program can process a paycheck that does not have overtime (40 hours or less), and also a paycheck that has overtime (more than 40 hours), it is important to test both conditions.
It also important to test the program at 40 hours. For example, use these values:
6 – Document the Project
A text document for the lab report is provided. Most of the lab report for this first project is already filled in. You need to complete the rest of the report.
12/23/2013
6
Implementation Using Microsoft Visual Studio
O Start Visual Studio O Open a New Project, name it Paycheck-V1.0 O Enter the program O Test the program with multiple inputs
Your First C or C++ Program
Although it may seem difficult to get the first program to work using Microsoft Visual Studio, it becomes much easier after you have done it a few times. Since this is your first C or C++ program, the steps are very detailed. Make sure you read each of the directions carefully and don’t move to the next step until you have completed the current step. Microsoft Visual Studio works easiest with C++. There is a few extra steps that are required to get Visual Studio to compile a program in the C language.
12/23/2013
7
Start Visual Studio
Start Visual Studio and select “New Project”
Set the Project Name
Set the Project Name and Location where you want it stored on the disk
12/23/2013
8
Click the Finish Button
An Empty Project
Your project should look like this after: 1) Click View, on the menu bar, select Solution Explorer 2) Click the name of the project 3) Click Source Files
1
2
3
12/23/2013
9
If You Are Writing a C Program Microsoft Visual Studio comes configured to develop C++ programs. You will need to make a few changes to successfully develop a C program. 1) Change the file extension of the program to .c instead of .cpp 2) Delete the #include "stdafx.h" line 3) Change the line that has int _tmain from: int _tmain(int argc, _TCHAR* argv[]) to: int main(int argc, char* argv[]) 4) Turn off Precompiled Headers These changes are described in more detail in the next slides.
Change the File Extension for C Programs Delete #include "stdafx.h"
These steps are not necessary for C++ programs
For C programs, right-click the name of your program. Change the file extension from .cpp to .c
For C programs, delete the line that has #include "stdafx.h“ This line may not appear in all versions of Microsoft C++, and will not appear in any non-Microsoft C++
12/23/2013
10
For C programs, change: int _tmain(int argc, _TCHAR* argv[ ]) to: int main(int argc, char* argv[ ]) The second version of int main(… will work for all versions of C and C++.
Change the int main Line for C Programs This step can be done for either C or C++ programs
Turn Off Precompiled Headers for C programs
1) View Solution Explorer Ctrl-Alt-L 2) Open the Properties Page by clicking the mouse inside the Solution Explorer area OR pressing Alt+Enter 3) Expand the C/C++ options by clicking the tiny arrow next to C/C++ 4) Click “Precompiled Headers”, and then click the down-arrow on the right 5) Select Not Using Precompiled Headers
This step is not necessary for C++ programs
12/23/2013
11
Enter the Code for the C or C++ Program C C++
Get a copy of the C or C++ program from Moodle
Notes on the Paycheck Program 1/4 Be very careful when typing computer programs. 1. Except for comments, C and C++ are case sensitive, which means
that upper-case letters (A-Z) are treated completely differently from lower-case letters (a-z).
2. Do not put spaces in the middle of variable names. Example: OvertimePay has no spaces.
3. Watch the placement of the semicolon ; character. It does not appear at the end of every line.
4. Use \n for C programs and endl for C++ programs. The \n uses a backslash \ not a forward slash /. The endl stands for end-of-line and the last character is a small-L, not the number 1.
5. Pay close attention to the parentheses ( ), the curly braces { } and the angle brackets < >
12/23/2013
12
Notes on the Paycheck Program 2/4 TITLE BLOCK – Include a block of comments that identify the name of the file, your name, date, and inputs and outputs used by the program.
/* Paycheck-V1.0.c Dan McElroy January 20, 2014 CIS-54 C/C++ Programming Inputs: Hours, PayRate Outputs: Paycheck */
// Paycheck-V1.0.cpp // Dan McElroy // January 20, 2014 // CIS-54 C/C++ Programming // Inputs: Hours, PayRate // Outputs: Paycheck
C C++
C style comments use a /* to start the comment, and */ to end the comment. A comment can either be on one line or it can span several lines. It can be used in C++.
C++ style comments start with // and stop at the end of the line. C++ style commends cannot cover multiple lines. Most of the modern C compilers will also accept C++ style comments.
Notes on the Paycheck Program 3/4 INCLUDE FILES – The compiler uses header files that are read into your program with the #include statement. You may need to include multiple header files depending on which routines you use in your program.
#include <stdio.h>
#include "stdafx.h“ // Only Microsoft #include <iostream> #include <iomanip> using namespace std;
C C++
C programs need to include stdio.h to provide access to printf and scanf.
NOTE: stdio.h is pronounced as individual letters: S-T-D-I-O-dot-H Do not pronounce it as one word STUDIO. It is not a dance or art studio.
C++ programs need to include iostream to use cin and cout. The iomanip include file is needed for cout to format the output for the number of digits past the decimal. The using namespace std is needed for iostream and iomanip. The stdafx.h is used only by some versions of Microsoft.
12/23/2013
13
Run and Test the Program
Press Ctrl-F5 to run the program. If you select the Debug menu and click “Start Debugging” or just press the F5 key, the program will run but the output screen will disappear as soon as the program finishes and you won’t be able to see the output screen.
Verify That the Program Produces Correct Results with and without Overtime
12/23/2013
14
Project Documentation and Lab Report
A text document for the lab report is provided. Most of the lab report for this first project is already filled in. The lab report is a two page document. You need to complete the rest of the report that is shown in red text.
Lab Report Document
On Moodle, click the link: Paycheck V1.0 – Lab Project and Report The lab report document is named C-Paycheck-Lab.doc Complete the lab report by replacing the red text with your report. Save the file with a new name. Submit your own lab report, not a copy of someone else’s report.
Page 2
Page 1
12/23/2013
15
Submit Your Work on Moodle All of your lab assignments should be submitted using Moodle. Make sure you click the correct link on the class Moodle page for your assignment, select the file you want to upload, click the Upload button and verify that the file was sent. Detailed instructions follow.
1. Find the Correct Submit Link
In this example, submit the C or C++ Payroll lab report. Click the link, Submit the Paycheck V1.0 Lab Report
12/23/2013
16
2. Submit Your Assignment
Moodle will load a new screen with instructions, a due date and the “Add submission” button. Verify that you will be submitting your report to the correct location then click the “Add submission” button.
3. Choose to File to Be Submitted Moodle then displays the “File submissions” screen. You can either: 1) Use the mouse to drag your completed lab report on top of the big blue arrow (works with most browsers), or 2) Click the “Files” link and find the file on your computer. After you have selected the file, click the “Save changes” button
12/23/2013
17
4. Verify That the File Was Uploaded If your file name shows up here, then the instructor can see the file.
If you want to view the status of your submission, go back to the “Submit your Report” link on the Moodle class web page.
If you want to update and resubmit your lab report before it has been graded, click the “Edit submissions” button. Your old submission will be deleted an be replaced with the updated version.
Congratulations ! Congratulations on completing your first C or C++ assignment. Refer back to this lab assignment when working on future labs for instructions on creating a C or C++ program using Microsoft Visual Studio, and instructions on how to upload a file to Moodle.