EIE2111 Lab 1: Getting Started and Debugging

Hello, if you have any need, please feel free to consult us, this is my wechat: wx91due

Department of Electronic and Information Engineering

EIE2111 Lab 1: Getting Started and Debugging

Introduction

This laboratory exercise is designed to let you knowhow to write a C++ program and debug a C++ program under Microsoft Visual Studio 2019. It may take less than 30 minutes to finish all if you know computer programming very well.

Part A: Develop your first C++ program, HelloWorld.cpp.

1.   Run the application software “Microsoft Visual Studio 2019” .

-    Choose and click “Visual Studio 2019”  from the program menu.

2.   Create a new project called HelloWorld.

-    Under the “All languages” pull-down list, choose “C++” .

-    Click the option “Windows Desktop Wizard” and then click the button “Next” .

-    Type “HelloWorld” in the field “Project name” .

-    Click the browse button  next to the Location field to change the location. It is important to remember the location so that you can retrieve your project. (The default location is too deep to find and you should better change it. For example, you could save your project on your own Desktop.)

-    Press “Create” button to create your project.

-    Under   “Application   type”,   choose   “Console Application”  and  select  “Empty  project.  Then, click button “OK” .

3.   Create a C++ program called HelloWorld.cpp.

-    Goto the window “Solution Explorer” and right click the folder “Source Files” (see the figure below).

-    Click the option “Add” and then “New Item …”.

-    Select “C++ File (.cpp)”, type “HelloWorld.cpp” in the field “Name” and then click the button “Add” (see the figure below). (TipsNever change your file location here.)

-

-    Type the following statements in the window “HelloWorld.cpp” (i.e., the editor window):

#include 

int main() {

std::cout << "Hello World!\n"; return 0;

}

4.   Compile and execute the program.

-    Goto the main menu. Click the option “Build” and then “Build Solution” . If there is no error, you should find the following message on the window “Output” (Fix any errors if any):

========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

-    Goto the main menu. Click the option “Debug” and then “Start Without Debugging” (or Ctrl + F5). You should find that the following window comes out:

Part B: Develop your second C++ program.

Go to the main menu. Click “File” and then “Close Solution” to close the current project. By following the steps in Part A, create a new project and write a program to display the following messages to the screen:

Iama HD EIE student.

Iam writing my second C++ program.

It is fun!

Part C: Debug a C++ program.

1.   Create a new project named “TestDebug” and type the following code in the editor window: #include 

int main() {

int a, b, c;

std::cout << "Hello World!\n"; a = 2;

b = 3;

c = (a + b) * 2;

std::cout << "The result is " << c << "\n"; return 0;

}

2.   You can use the following way to display the line number:

“Tools” (from the main menu) → “Options …” → “TextEditor” → “C/C++” → “General” → select “Line numbers” .

3.   A breakpoint is an intentional stopping place where the execution of a program will be paused by a debugger. When your program stops on a breakpoint, you can inspect the current values of the variables in your program. You may set a breakpoint in the program by using the following way:

-    Click a line on the editor window (e.g., line 8: “ a = 2”).

-    “Debug” (from the main menu) → “Toggle Breakpoint”

Visual Studio will show a small red circle on the lefthand side (see the figure below). Remove a breakpoint by clicking on the breakpoint mark and it will turn off the breakpoint.

4.   You can use the following way to debug your program:

“Debug” (from the main menu) → “Start Debugging” .

Then the values of all variables will be displayed in the window “Autos” .

5.   Once your program has stopped executing at a breakpoint, you can trace the execution of your program using the following options:

The Debug menu provides three commands for stepping through code: “Step Into”, “Step Over”, and “Step Out” . “Step Into” and “Step Over” differ in only one respect, the way they handle function calls. Either command instructs the debugger to execute the next line of code. If the line contains a function call, “ Step Into” executes only the call itself, then halts at the first line of code inside the function. “Step Over” executes the entire function, then halts at the first line outside the function. Use “Step Into” if you want to look inside the function call. Use “Step Over” if you want to avoid stepping into functions.

Use “Step Out” when you are inside a function call and want to return to the calling function. “Step Out” resumes execution of your code until the function returns, then breaks at the return point in the calling function.

Use “Continue” to continue the execution of program until the next breakpoint or until the program terminates.

Use “Step Over” to finish the execution of the program. You can see the change of the variables a, b, and c.

Alternatively, you may always access these debug commands at the menu bar shown below:

6.   You may use the following way to stop the debugger:

“Debug” (from the main menu) → “Stop Debugging” .

Part D: Develop your first C++ program, TestDebug2.cpp

1.   Similar   to    Part   C,    create   a   project    called    TestDebug2 and   a    C++   source    file TestDebug2.cpp.

2.   Type the following code into your C++ program TestDebug2.cpp:

#include  // allows program to perform input and output using std::cout; // program uses cout

using std::endl; // program uses endl using std::cin; // program uses cin

int main(){

int number1; // first integer read from user  int number2; // second integer read from user int number3; // third integer read from user

cout >> "Input three different integers: "; // prompt

cin >> number1 >> number2 >> number3; // read three integers largest = number1; // assume first integer is largest

if ( number2 > largest ) // is number2 larger?

largest = number2; // number2 is now the largest

if ( number3 > largest ) // is number3 larger?

largest = number3; // number3 is now the largest

smallest = number1; // assume first integer is smallest

if ( number2 < smallest ) // is number2 smaller?

smallest = number2; // number2 is now the smallest

if ( number3 < smallest ) // is number3 smaller?

smallest = number3; // number3 is now the smallest

cout << "Sum is " << number1 + number2 + number3

<< "\nAverage is " << ( number1 + number2 + number3 ) / 3 << "\nProduct is " << number1 * number2 * number3

<< "\nSmallest is " << smallest

<< "\nLargest is " << largest << endl

return 0; // indicate successful termination } // end main

3.     Click  “Build” and then  “Build  Solution” . Read the error messages. Correct the errors and compile the program again. Execute the program by clicking “Debug” and then “Start Without Debugging” (or Ctrl + F5). The sample output is shown below:

Instructions

a.  You are required to submit ALL your C++ programs (i.e. whole project folders with source code inside; each project is stored inside a single folder which is created by Microsoft Visual Studio 2019) in Part A, B, C, and D to Blackboard. Use Zip or 7-Zip to compress all of them into a single file. Please add your student name & ID as a comment (e.g. //Student Name/ID: XXX XXX) at the beginning of each .cpp file. Remember to close any output window and Visual Studio before compression and submission.

b. The deadline of the submission: Check the course information.




发表评论

电子邮件地址不会被公开。 必填项已用*标注