The procedures also provide the code for the header and .cpp files for the static libraries.
Create the CppDemo solution and the CodeDefects project
Open Visual Studio and select Create a new project
In the Create a new project dialog, change the language filter to C++.
Select Windows Desktop Wizard and choose the Next button.
On the Configure your new project page, in the Project name text box, enter CodeDefects.
In the Solution name text box, enter CppDemo.
Choose Create.
In the Windows Desktop Project dialog, change the Application type to Static Library (.lib).
Under Additional options, select Empty project.
Choose OK to create the solution and project.
Open Visual Studio. On the menu bar, choose File > New > Project.
In the New Project dialog, select Visual C++ > Windows Desktop.
Select Windows Desktop Wizard.
In the Name text box, enter CodeDefects.
In the Solution name text box, enter CppDemo.
Choose OK.
In the Windows Desktop Project dialog, change the Application type to Static Library (.lib).
Under Additional options, select Empty project.
Choose OK to create the solution and project.
Open Visual Studio. On the menu bar, choose File > New > Project.
In the New Project dialog, select Templates > Visual C++ > Win32.
Select Win32 Console Application.
In the Name text box, enter CodeDefects.
In the Solution name text box, enter CppDemo.
Choose OK.
In the Win32 Application Wizard dialog, choose the Next button.
Change the Application type to Static library.
Under Additional options, unselect Precompiled header.
Choose Finish to create the solution and project.
Add the header and source file to the CodeDefects project
In Solution Explorer, expand CodeDefects.
Right-click to open the context menu for Header Files. Choose Add > New Item.
In the Add New Item dialog box, select Visual C++ > Code, and then select Header File (.h).
In the Name edit box, enter Bug.h, and then choose the Add button.
In the edit window for Bug.h, select and delete the contents.
Copy the following code and paste it into the Bug.h file in the editor.
C++
#pragma once#include<windows.h>// Function prototypesboolCheckDomain(wchar_tconst *);
HRESULT ReadUserAccount();
// These constants define the common sizes of the// user account information throughout the programconstint USER_ACCOUNT_LEN = 256;
constint ACCOUNT_DOMAIN_LEN = 128;
In Solution Explorer, right-click to open the context menu for Source Files. Choose Add > New Item.
In the Add New Item dialog box, select C++ File (.cpp).
In the Name edit box, enter Bug.cpp, and then choose the Add button.
Copy the following code and paste it into the Bug.cpp file in the editor.
C++
#include"Bug.h"// the user accountwchar_t g_userAccount[USER_ACCOUNT_LEN] = { L"domain\\user" };
int len = 0;
boolCheckDomain(wchar_tconst* domain){
return (wcsnlen_s(domain, USER_ACCOUNT_LEN) > 0);
}
HRESULT ReadUserAccount(){
return S_OK;
}
boolProcessDomain(){
wchar_t* domain = newwchar_t[ACCOUNT_DOMAIN_LEN];
// ReadUserAccount gets a 'domain\user' input from//the user into the global 'g_userAccount'if (ReadUserAccount())
{
// Copies part of the string prior to the '\'// character onto the 'domain' bufferfor (len = 0; (len < ACCOUNT_DOMAIN_LEN) && (g_userAccount[len] != L'\0'); len++)
{
if (g_userAccount[len] == L'\\')
{
// Stops copying on the domain and user separator ('\')
break;
}
domain[len] = g_userAccount[len];
}
if ((len = ACCOUNT_DOMAIN_LEN) || (g_userAccount[len] != L'\\'))
{
// '\' was not found. Invalid domain\user string.
delete[] domain;
return false;
}
else
{
domain[len] = L'\0';
}
// Process domain stringbool result = CheckDomain(domain);
delete[] domain;
return result;
}
returnfalse;
}
int path_dependent(int n)
{
int i;
int j;
if (n == 0)
i = 1;
else
j = 1;
return i + j;
}
On the menu bar, choose File > Save All.
Add the Annotations project and configure it as a static library
In Solution Explorer, right-click CppDemo to open the context menu. Choose Add > New Project.
In the Add a new project dialog box, select Windows Desktop Wizard, and then choose the Next button.
On the Configure your new project page, in the Project name text box, enter Annotations, and then choose Create.
In the Windows Desktop Project dialog, change the Application type to Static Library (.lib).
Under Additional options, select Empty project.
Choose OK to create the project.
In Solution Explorer, right-click CppDemo to open the context menu. Choose Add > New Project.
In the Add New Project dialog, select Visual C++ > Windows Desktop.
Select Windows Desktop Wizard.
In the Name text box, enter Annotations, and then choose OK.
In the Windows Desktop Project dialog, change the Application type to Static Library (.lib).
Under Additional options, select Empty project.
Choose OK to create the project.
In Solution Explorer, right-click CppDemo to open the context menu. Choose Add > New Project.
In the Add New Project dialog, select Visual C++ > Win32.
Select Win32 Console Application.
In the Name text box, enter Annotations.
Choose OK.
In the Win32 Application Wizard dialog, choose the Next button.
Change the Application type to Static library.
Under Additional options, unselect Precompiled header.
Choose Finish to create the project.
Add the header file and source file to the Annotations project
In Solution Explorer, expand Annotations.
Right-click to open the context menu for Header Files under Annotations. Choose Add > New Item.
In the Add New Item dialog box, select Visual C++ > Code, and then select Header File (.h).
In the Name edit box, enter annotations.h, and then choose the Add button.
In the edit window for annotations.h, select and delete the contents.
Copy the following code and paste it into the annotations.h file in the editor.
In Solution Explorer, right-click to open the context menu for Source Files under Annotations. Choose Add > New Item.
In the Add New Item dialog box, select C++ File (.cpp).
In the Name edit box, enter annotations.cpp, and then choose the Add button.
Copy the following code and paste it into the annotations.cpp file in the editor.
C++
#include"annotations.h"#include<malloc.h>
_Ret_maybenull_ LinkedList* AllocateNode(){
LinkedList* result = static_cast<LinkedList*>(malloc(sizeof(LinkedList)));
return result;
}
LinkedList* AddTail(LinkedList* node, int value){
// finds the last nodewhile (node->next != nullptr)
{
node = node->next;
}
// appends the new node
LinkedList* newNode = AllocateNode();
newNode->data = value;
newNode->next = 0;
node->next = newNode;
return newNode;
}
On the menu bar, choose File > Save All.
The solution is now complete and should build without errors.
Note
In Visual Studio 2017, you may see a spurious warning E1097 unknown attribute "no_init_all" in the IntelliSense engine. You can safely ignore this warning.
This module helps you evaluate your debugging skills. You start with a C# console application that contains code logic issues and a specification that describes the requirements. You’re challenged to demonstrate your ability to identify and fix the issues using the Visual Studio Code debugger tools.