Lab Overview—Scenario/Summary Welcome to programming with C++. The purpose of this three-part lab is to walk you through the following tutorial to become familiar with the actions of compiling and executing a C++ program. In general, this lab will instruct you on how to create a project; how to enter and save a program; how to compile and run a program; how to, given a simple problem using input and output, code and test a program that meets the specifications; and how to debug a simple program of any syntax and logic errors.
|
CIS170C Week 1 Lab Instructions
Lab 1 of 7: Getting Started (Your First C++ Programs)
Lab Overview—Scenario/Summary
Welcome to programming with C++. The purpose of this three-part lab is to walk you through the following tutorial to become familiar with the actions of compiling and executing a C++ program.
In general, this lab will instruct you on
- how to create a project;
- how to enter and save a program;
- how to compile and run a program;
- how to, given a simple problem using input and output, code and test a program that meets the specifications; and
- how to debug a simple program of any syntax and logic errors.
Deliverables
Section | Deliverable | Points |
Part A | Step 6: Program Listing and Output | 10 |
Part B | Program Listing and Output | 10 |
Part C | Program Listing and Output | 10 |
All Parts | Total | 30 |
Lab Steps
Preparation:
If you are using the Citrix remote lab, follow the login instructions located in the lab (under General Resources within Course Resources) in Course Home.
Part A: Getting Started | |
Step 1: Start the Application | |
| |
Step 2: How to Add a Source Code File to Your Project (.cpp file) | |
| |
Step 3: Create a Source Code File | |
Now enter the following C++ program exactly as you see it. Use the tab where appropriate. [Note: C++ is case sensitive.] Instead of John Doe, type your name.
// ————————————————————— // Programming Assignment: LAB1A // Developer: ______________________ // Date Written: ______________________ // Purpose: Ticket Calculation Program // ————————————————————— #include <iostream>
using namespace std;
void main() {
cout << “John Doe ticket program\n”;
int childTkts, adultTkts, totalTkts; cout << “Please enter the number of child tickets:”; cin >> childTkts; cout << “Please enter the number of adult tickets:”; cin >> adultTkts; totalTkts = childTkts + adultTkts;
cout << “The total tickets are: “<<totalTkts << endl;
}
When you execute a program in debug mode (F5), the console screen may appear and disappear before you have an opportunity to view your output. There are several techniques you can use to pause the console screen so you can read the output. On the very last line in the main() function, a. insert the statement: system(“pause”); -OR- b. insert an input statement: cin >> myVarable;
| |
Step 4: Output | |
The black screen or console should read | |
Step 5: Save Program | |
Save your program by clicking File on the menu bar and then clicking Save Program.cpp, or by clicking the Save button on the toolbar, or Ctrl + S.
| |
Step 6: Build Solution | |
To compile the program, click Build on the menu bar, and then click the Build Solution or Build LabA option. You should receive no error messages. If you see some error messages, check the code above to make sure you didn’t key in something wrong. Once you make your corrections to the code, go ahead and click Build >> Build Solution again. | |
Step 7: Execute the Program | |
Once you have no syntax errors, to execute or run your program, click Debug on the menu bar, and then click Start Without Debugging. | |
Step 8: Capture the Output | |
Print a picture of your screen output. (Do a print screen, and paste this into MS Word.) | |
Step 9: Print the Source Code | |
Copy your source code and paste it into the same Word document as your screen print. Save the Word Document as Lab01_LastName_FirstInitial. Note: Using the Visual Studio editor to compile your programs creates a lot of overhead. These additional files will become important as you create more sophisticated C# projects. Projects can contain one or more source-code files. For this course, you will not have to worry about all the extra files that are created. | |
End of Part A | |
Part B: Debugging a program | |
Step 1: Create New Project | |
Now create a new project and name it LAB1B. Make sure you close your previous program by clicking File >> Close Solution. | |
Step 2: Type in Program | |
Like before, enter the following program. Type in your name for Developer and current date for Date Written.
This program has three errors.
// ————————————————————— // Programming Assignment: LAB1B // Developer: ______________________ // Date Written: ______________________ // Purpose: Average Program // ————————————————————— #include <iostream>
using namespace std;
void main() {
cout << “Find the Average Program\n”;
double num1, num2, num3, average; cout << “Please enter number 1: “; cin >> num1; cout << “Please enter number 2: ” cin << num2; cout << “Please enter number 3: “; cin >> num3; average = num1 + num2 + num3 / 3;
cout << “The average is: ” << average << endl; system(“pause”);
} When you build the solution you will see the debugger. Double click on an error to locate the error. If it is a syntax error, the error will be listed in the error list. Some errors will be underlined in red. Once you fix the syntax errors, use the F11 (step into code) and F10 (step over code) commands to locate the logic error in the code.
| |
Step 3: Save Program | |
Save your program by clicking File on the menu bar and then clicking Save Program.cpp, or by clicking the Save button on the toolbar, or Ctrl + S.
| |
Step 4: Build Solution | |
To compile the program, click Build on the menu bar, and then click the Build Solution or Build LabB option. You should receive no error messages. If you see some error messages, check the code above to make sure you didn’t key in something wrong. Once you make your corrections to the code, go ahead and click Build >> Build Solution again.
| |
Step 5: Execute the Program | |
Once you have no syntax errors, to execute or run your program, click Debug on the menu bar, and then click Start Without Debugging.
| |
Step 6: Capture the Output | |
1. Capture a screen print of your output. (Do a PRINT SCREEN and paste into the Word document from Part A.) 2. Copy your code and paste it into the same MS Word document that contains the screen print of your output. 3. List all three errors. 4. Save this in the same Word document as Part JA. | |
End of Part B |
Part C: Payroll Program |
Step 1: Create a New Project |
Create a new project and name it LAB1C. Make sure you close your previous program by clicking File >> Close Solution.
Include a comment box like what you coded in Part B. This can go at the very top of your program. |
Step 2: Processing Logic |
You need to write a program that calculates and displays the revenue earned from ticket sales at a movie theater.
Input: Prompt the user for the number of adult tickets. Prompt the user for the number of child tickets. Process: Perform the calculations. The adult tickets cost $10.00 and the child tickets cost $6.00. The theater keeps 20% of the gross box office profit, and the rest goes to the movie distributor. Output: Display the results. Sample Output from Lab 1: Flowchart: (continued on next page)
Pseudo Code:
1. Declare variables 2. Accept Input – child tickets and adult tickets 3. Calculate Child ticket total = child tickets* 6 4. Calculate adult ticket total = adult tickets * 10 5. Calculate gross profit = child ticket total + adult ticket total 6. Calculate net profit = gross profit *0.2 7. Calculate distributor amount = gross profit – net profit 8. Display the following on separate lines and format variables with $ and decimal. a. Gross Profit value of gross profit b. Net Profit value of net profit c. Distributor Amount value of distributor amount
//include the iomanip header file at the top of the file #include <iomanip>
//use fixed and setprecision(2) to format the number //use setw(8) to control the width of the field //use \t to control the spacing between fields cout << fixed << setprecision(2); cout << “Gross Profit :\t $” << setw(8) << grossProfit << endl; |
Step 3: Save Program |
Save your program by clicking File on the menu bar and then clicking Save Program.cpp, or by clicking the Save button on the toolbar, or Ctrl + S.
|
Step 4: Build Solution |
To compile the program, click Build on the menu bar and then click the Build Solution, or Build LabC option. You should receive no error messages. If you see some error messages, check the code above to make sure you didn’t key in something wrong. Once you make your corrections to the code, go ahead and click Build >> Build Solution again.
|
Step 5: Execute the Program |
Once you have no syntax errors, to execute or run your program, click Debug on the menu bar, and then click Start Without Debugging.
|
Step 6: Capture the Output |
1. Capture a screen print of your output. (Do a PRINT SCREEN and paste into an MS Word document.) 2. Copy your code and paste it into the same MS Word document that contains the screen print of your output. 3. Use the same Word document as Part A and B.
|
End of Part C |
END OF LAB |