Page 1 of 1

Zen and the Art of Debugging C/C++ Programs in Visual Studio Beginners HelloWorld Tutorial on Debugging in Visual Studio Rate Topic: -----

#1 jwwicks  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 24
  • View blog
  • Posts: 162
  • Joined: 31-July 08

Posted 01 August 2008 - 04:52 PM


Introduction

This tutorial describes how to use Visual Studio to create a new C++ program, compile/run a program, resume work on an existing program and debug a program. It is aimed at the beginning CIS student who is struggling to get their programs working. I work in the computer lab at the college I'm attending and I see many students who don't know how to use the IDE for best results.

Visual Studio automatically creates a number of files which will fill up a diskette quickly. For this reason work should be done on the C: drive or on a flash drive. When you are finished working, just copy from the temporary projects folder to your flash. If you decide to work directly from your flash drive no copying will is required. One quick note about working from a Flash drive is that for versions of Visual Studion greater than 2003 it will be slow. This seems to stem from Intellisense but even turning this off doesn't help much. Your best bet in this case is to create a temporary projects folder on the machine you are using for development and copy your project there.


Creating a Project

A project must be created before work on a program can take place. This is true for new programs and for those on which you have already begun work. The steps below describe how to create a project.
1. Start Microsoft Visual Studio .NET by selecting Microsoft Visual Studio .NET 2003 from the Start Menu. The screen below should appear:
Posted Image
2. Click the New Project button near the bottom of the screen on the Start Page.
3. Select the Visual C++ Projects folder under Project Types.
4. Select the Win32 folder under Project Types.
5. Select the Win32 Console Project icon under Templates.
Posted Image
6. Replace <Enter name> in the Name text box with MyProjectName1 where MyProjectName1 is some descriptive name for the program you are working on. For instance, if you are writing a program that computes income taxes type in MyTaxProgram or something similar.
7. Type C:\projects in the Location text box to save your work on the hard drive. Your files will be saved in the C:\projects\MyProjectName1 directory which will be created if it does not yet exist. If you are using a flash drive to store your programs on, just replace the C:\ in the example above for the drive letter of your flash drive.
8. Click OK.
9. Click on Application Settings on the Win32 Application Wizard window which appears.
10. Click the Empty project check box under Additional options.
11. Click the Finish button and the project will be created.
Posted Image

If you look in the Solution Explorer located on the far right of the Visual Studio window you should see the following:
Posted Image


Creating a New Source Code File

If you are working on a new program, a new source code file must be created after the project has been created. The steps below describe how to create the new source code file and add it to the project.
1. Select File from the menu and then Add New Item…
Posted Image
2. When the Add New Item dialog appears click on the Visual C++ folder under Categories. Then click the Code folder.
3. Click the C++ File(.cpp) icon under Templates.
4. Replace <Enter Name> in the Name text box with Source1 which is the name of your source code file. You don’t have to include the .cpp extension but if you do that is OK. In general terms use a descriptive name for the major function of the file, for instance, if the program is a tax calculator name the file taxcalculator.cpp or something similar. The Location text box should not have to be changed but if this is not the directory you want the source file in change it to the correct one for your project.
5. Click the Open button and a new, empty .cpp file will be created and opened into which you can type your code.
Posted Image
6. If the Source1.cpp file doesn’t appear in the Solution Explorer like the above screen then do the following to add your source code file to the project (which must be done before attempting to compile)
a) Select File from the menu.
B) Select Move Source1.cpp into Project (where Source1.cpp is the name of your source code file) from the drop down menu.
c) Select MyProjectName1 (where MyProjectName1 is the name of your project) when it appears.
Posted Image


Compiling my first Program

1. Add the following text to your Source1.cpp file:
////////////////////////// 
/// source1.cpp - 
/// Purpose: Some source code file 
/// 
/// Author: My Name 
/// Date: January 1st, 2xxx 
/// 
//////////////////////// 
/// History 
/// INI 2xxx-01-01 v1.00.00 Initial release 

// Includes 
#include <iostream> 
#include <iomanip> 
using namespace std; 

int main(int argc, char** argv) 
{ 
	int ret_val = 0; // holds return value for main program 

	cout << "Hello World..." << endl; 
 
	return ret_val; 
} 



2. Select Build from the menu and then Build Solution. You could also press the Ctrl+Shift+B keys together as a shortcut instead. Messages from the compiler (error messages, warning messages and a successful compile/build message) will be displayed in the Output window at the bottom of the screen.
Posted Image


Running a Program

Once the compile/build is successful, select Debug from the menu and then Start Without Debugging from the drop down menu to run the program. You could also type Ctrl-F5 instead. If a window appears which states “These project configuration(s) are out of date:” and asks “Would you like to build them?” click Yes.
Posted Image

A new window, called a console window, will open and display the program’s output statements and also allow for program input.
Posted Image

Let’s add the following text to our code to make this program a little more interesting.
1. Add the following text before main:
// Global constants and variables 
#define PRG_NAME "MyProjectName1 - v 1.0.00" 
#define CPY_RGHT "Copyright © 2xxx - " 
#define AUTHOR "My Name" 

// function prototypes 
void show_copyright( void ); 


2. Add this call to the copyright function before the “Hello World…” statement in main:
show_copyright(); 


3. After main add the following function:
///////////////////////// 
/// show_copyright 
/// Purpose: Show program name and copyright notice 
/// 
/// Parameters : None 
/// Output : couts PRG_NAME, CPY_RGHT and AUTHOR defines 
/// 
/// Returns : None 
/// SideEffects : None 
//////////////////////// 
void show_copyright ( void ) 
{ 
	cout << AUTHOR << "'s " << PRG_NAME << endl; 
	cout << CPY_RGHT << AUTHOR << endl << endl; 
	return; 
} 


Your Source1.cpp file should look like the following when you are done with all these changes:
Posted Image

4. Now build and run the solution as you did before. You should see the following screen:
Posted Image


Debugging a Program

It can sometimes be difficult to determine why a program does not run successfully. A debugger allows a programmer to step through a program line-by-line to watch it execute and fail. The values of variables can be watched as execution takes place as well.
Visual Studio has a built in debugger which can be used to step through a program. It is important to note that the program must compile first. Let’s add some more code to our Source1.cpp file (Note: this code has a logic problem in it to help you get used to debugging):

1. Add the following code before main after the show_copyright function prototype:
char get_choice( void ); 
bool is_valid_choice( char ); 


2. Add the following in main:
char choice = NULL; 


3. Change the cout statement with “Hello World…” to the following:
	do{ 
		cout << "Hello World..." << endl; 
		choice = get_choice(); 
	}while(!is_valid_choice( choice)); 


4. Add the following functions after the show_copyright function:
///////////////////////// 
/// get_choice 
/// Purpose: Check to see if user wants to continue 
/// 
/// Parameters: None 
/// Output: Prompts user to continue program 
/// 
/// Returns: char ch - Choice entered by user 
/// SideEffects: None 
///////////////////////// 
char get_choice( void ) 
{ 
	char ret_val; 
	char ch; 
	cout << "Do you want to continue (1 = Yes) (0 = No) ?: "; 
	cin >> ch; 
	ret_val = ch; 	
	return ret_val; 
} 

///////////////////////// 
/// is_valid_choice 
/// Purpose: Checks for valid yes or no entry to continue 
/// 
/// Parameters : char val - Current choice to continue 
/// Output : Displays an error message if an invalid choice is passed 
/// 
/// Returns : true if choice is valid and false otherwise 
/// SideEffects : None 
//////////////////////// 
bool is_valid_choice( char val ) 
{ 
	bool ret_val = true; 
	switch (val){ 
		case 0: 
		case 1: 
			break; 
		default: 
			ret_val = false; 
			cout << "Invalid choice : " << val << endl; 
			cout << "Please enter one of the following(0 or 1) : " << endl; 
			break; 
	} 
	return ret_val; 
} return ret_val; 


5. Build and run the solution as before and you should see the following screen:
Posted Image

6. Press 0 and instead of stopping the program like we asked our program continues. We have a bug here Watson. Press Ctrl+C to stop executing the program and close the window down. Now we’ll work on finding the bug in our code using the debugger in Visual Studio.
Posted Image


Breakpoints

A breakpoint tells the debugger to pause execution of a program until you tell it to continue. In order to step through a program (i.e. watch a program execute), a breakpoint should be set on the executable statement at which you want execution to pause. To set a breakpoint on a statement, click on the statement and then press F9. A red dot will appear on the left of the screen to indicate a breakpoint has been set. The breakpoint can be removed by pressing F9 again. Please note that breakpoints are not saved if you exit Visual Studio.
An executable statement is a line of code which performs an action when the program runs. Examples of statements which are not executable statements are comments, variable declarations, and the word do in a do loop. If a breakpoint is set on these types of statements, the debugger will not pause on them during execution. In addition, the breakpoint might automatically be moved to the next executable statement as determined by the compiler.

Determining where you want the breakpoint is a critical part of debugging. Put your breakpoint to soon in the execution of the program and you waste time stepping through code that you don’t need to. Put the breakpoint too late and you might just miss the bug altogether. In our sample program above we have to possible places in our code that could be causing the problem. Either the get_choice function isn’t returning the proper value or the is_valid_choice function isn’t correctly checking the value. Let’s set a breakpoint to check the return value of the get_choice function. There are two possible places to check the value, one being inside the function itself or in the assignment statement in main. We’ll set our breakpoint in main so we can check the return value. Place the cursor on the line that reads, choice = get_choice(); and either press F9 or click the right mouse button to show the context menu and then select Insert Breakpoint.
Posted Image
A red dot will appear on the left hand side of the screen like below:
Posted Image


Watching a Program Execute

Once a breakpoint is set, start the debugger by selecting Debug from the menu and then Start from the drop down menu. You could also type F5 instead. The program will begin to run but pause at the statement with the breakpoint prior to its execution. For our sample you should see something like the screen below:
Posted Image
At this point you have a number of choices (given below). The buttons mentioned can be found on the Debug toolbar. When you press F5 the toolbar should automatically appear but if not you can access the toolbar by doing the following:
1. Select View from the menu, then Toolbars from the drop down menu and then Debug.
Posted Image

Here’s what the toolbar looks like and what you can do with the buttons:
Posted Image
• Click the Step Over button
Posted Image to execute the current statement. Repeat this step to see the entire program execute.
• If you want to exit the debugger at any point, and also stop program execution, click the Stop Debugging button Posted Image .
• If you want to restart execution of the program, from the beginning and using the debugger, click the Restart button Posted Image .
If your program has functions, other than main, you have two additional choices.
• If the current statement is a function call, click the Step Into button
Posted Image to go into the function to watch it execute.
• If you are stepping through a function and do not want to watch the rest of it execute, click the Step Out button Posted Image to exit the function. The debugger will still step through the rest of the program.


Value of a Variable

It is often helpful to know the value of a variable at a certain point in an executing program. The debugger allows you to look at the contents of variables as a program executes. When you press F5 the Locals Windows should automatically be available at the bottom of the screen but if not you can do the following:
1. Select Debug from the menu, Windows from the drop down menu and then Locals. A window at the bottom of the screen will display the values of the variables in the current function (e.g. in main). As you step through the program, the displayed values of the variables will change as the variables themselves are given new values.
For our example you should see the following in the Locals Windows at the bottom of the screen.
Posted Image
Notice the value for choice in the window above. The char variable choice was initialized with a NULL or in this case a 0. We’ll be double-checking it in the next section once the get_choice function returns a value.


Input and Output

The console window will still be used for program input and output. If you click the Step Over button on a cin statement, type the data through the console window. When you press Enter, execution of the program by the debugger will continue. If you click the Step Over button on a cout statement, the data will be displayed on the console window. For our example you’ll see the current console window for the program:
Posted Image
Since we haven’t executed the statement for the get_choice function only the “Hello World…” statement is showing so far. Click the Step Over button on the Debug toolbar and notice the following change in the console window for our program.
Posted Image
In the console window type in 1 at the prompt and then press Enter to continue the program. The console window should now look like this:
Posted Image
And the Locals Window at the bottom of the Visual Studio screen should now have the following values for choice.
Posted Image
Notice the change from before, 49 and ‘1’ instead of 0. The 49 is the ASCII value for the character 1, so the get_choice function is correctly returning a character value and choice is correctly storing that value. By stepping through the program we’ve discovered our bug isn’t in the get_choice function. Since our next statement in the program is the other function we need to check, is_valid_choice, and we know that a valid character is stored in choice and the is_valid_choice function should just keep looping with no error message. Use the Step Over button to execute the is_valid_choice function. Notice what happens to our console window.
Posted Image
This isn’t correct since we know that the character 1 was passed in the variable choice to the function is_valid_choice by the following statement in main: }while(!is_valid_choice( choice));. So the problem has to be in the way in which is_valid_choice is checking the value. So we’ll use the Step Into button to execute each statement in that function to find out where things are going wrong. But first you have to Step Over a couple of the statements get to the is_valid_function again. Notice that there is a yellow arrow on the left hand side of the Visual Studio window. That indicates the current statement.
Posted Image
In our example it’s the cout statement that displays the “Hello World…” message. Press the Step Over button to execute this statement. Press the Step Over button once again to execute the get_choice function statement. In the console window type in 1 at the prompt and then press Enter to continue the program. Now press the Step Into button to debug the is_valid_choice function. Notice that the yellow arrow is now inside the is_valid_choice function and that the Locals Window down at the bottom has variable called val that has the values passed in the variable choice, 49 and ‘1’, from main.
Posted Image
Press the Step Over button till the yellow arrow is positioned on the line with the switch statement. Press the Step Over button once more and notice that the arrow jumps all the way to the first statement inside the default: section of the switch. It should have hit the break statement after seeing
case 1: 
break; 


But it didn’t for some reason.


What You Are Looking For

There are two ways in which a debugger is most helpful: 1) to verify statements are executed as expected, and 2) to verify that variables have their expected values. Look at the case statement very carefully we seem to be comparing an integer 1 with the character 1, which is 49 in ASCII, so our program is functioning correctly but our logic test isn’t right. Change the case statement in the is_valid_choice function to look for character data:

switch (val){ 
case '0': 
case '1': 
break; 



After editing the code to correct the problem, save your changes, press the Stop button on the debug toolbar and rebuild the program. Now restart the debugger. Press the Step Over button till you get to the get_choice statement, then type a 1 again and press Enter as you did before in the console window. Notice that this time through, the yellow arrow jumps to the end of the program like it should.
Posted Image
Press the Stop button and remove the breakpoint by placing the cursor on the line with the red dot and pressing F9.
There is still one more logic problem in this program and you should use the same techniques to find it. (Hint: the program should continue printing “Hello World… and asking the user to continue if the user enters a 1 at the prompt.)


Saving Your Work

When you are finished working (either because your program is done or because you need to resume work later), copy your source code file from the work directory on the hard drive to your diskette. It is not necessary to copy any of the other files.
Before copying your source code file, close Visual Studio. If you are asked to save any files, say yes unless you know you have made changes to your program you do not want to save.
Once you are sure your source code file has been saved to your diskette, delete the work directory from the hard drive.


Resuming Work on an Existing Source Code File

If you want to resume work on a program, a project must first be created and then your existing source code file added to the project. This process is described below.
1. Create a directory named projects off the root directory on the C: drive.
2. Copy your source code file to the work directory.
3. Follow the steps under Creating a Project above.
4. Select File from the menu and then Add Existing Item
5. In the Add Existing Item window which appears, locate your source code file in the work directory and click it to select it. Click the Open button.
6. Select File from the menu and then select Move xxx.cpp into Project (where xxx.cpp is the name of your source code file) from the drop down menu.
7. Select the project name when it appears.
8. You can now continue working on your program.
Note: if you have exited Microsoft Visual Studio but have not gone any further, you can double-click the project file and Visual Studio and your program will be opened up for you to continue working. The project file will be in the work directory and will have a .vcproj file extension. Before attempting to compile, the source code file must be added to the project (steps 6 and 7 above).


Removing a Source Code File from a Project

It may sometimes be desirable to remove a source code file from an existing project without exiting Visual Studio. Note that the steps below will not delete the file from the work directory.
1. Select the Solution Explorer “tab” at the right of the screen.
2. Select the name of your source code file listed under the “folder” named Source Files. The folders listed do not really exist on the disk. They are solely used within the project to organize its files.
3. Press Delete to remove the file from the project.
Once a file is removed from a project, and Visual Studio not exited, an existing file can be added to the project by following the steps below.
1. Select File from the menu and then Add Existing Item...
2. In the Add Existing Item window which appears, locate your source code file in the work directory and click it to select it. Click the Open button.
3. Double-click your source code file in the “folder” named Source Files on the Solution Explorer “tab” to open it.
4. Select File from the menu and then select Move xxx.cpp into Project (where xxx .cpp is the name of your source code file) from the drop down menu. Select Solution Items when it appears.
5. Select File from the menu and then select Move xxx.cpp into Project from the drop down menu. Select the project name when it appears.
6. You can now continue working on your program.


I/O File Location

Place the file to read from, the input file, in the MyProjectName1 directory with your source code file. The file will be read from this directory when the program is run.
If your program creates a text file, an output file, the file will be created in the MyProjectName1 directory. Use Notepad to view the contents of the file or add the file to the project as a resource.


Solutions
Here's a strictly Standard C solution...
///////////////////////////////////////
/// \file main.c
/// \brief Some source code file Project Main Module
/// 
/// \author My Name
/// \version 00.99.00
/// \date January 1st, 2xxx
///
//////////////////////// 
// History 
// INI 2xxx-01-01 v1.00.00 Initial release 

// Includes 
#include <stdio.h> 
#include <stdlib.h>

// Global constants and variables 
#define PRG_NAME "MyProjectName1 - v 1.0.00" 
#define CPY_RGHT "Copyright © 2xxx - " 
#define AUTHOR "My Name" 

// function prototypes 
void show_copyright( void ); 
char get_choice( void ); 
unsigned int is_valid_choice( char ); 

// Uncomment to run just the testing main
//#define TEST 1

/////////////////////////
/// \fn main(int argc, char** argv)
/// \brief Main module for Hello World program.
///
/// \param argc - integer number of arguments passed from system to program
/// \param argv - pointer to array of characters of arguments passed to program from system
///
/// \return integer value for success or failure
/////////////////////////
#ifndef TEST
int main(int argc, char** argv) 
{ 
	int ret_val = 0; // holds return value for main program 
	char choice = '0';
	show_copyright();

	do{	
		do{ 
			printf("Hello World...\n"); 
			choice = get_choice(); 
		}while(!is_valid_choice( choice));
	}while(choice == '1');
 
	return ret_val; 
} 
#else // Testing Main
int main( int argc, char** argv)
{
	int ret_val = 0;
		
	return ret_val;	
}
#endif

///////////////////////// 
/// \fn show_copyright(void) 
/// \brief Show program name and copyright notice 
/// 
/// \b SideEffects : None\n
/// \b Output : couts PRG_NAME, CPY_RGHT and AUTHOR defines\n
///
/// \return None
//////////////////////// 
void show_copyright ( void ) 
{ 
	printf("%s 's %s\n", AUTHOR, PRG_NAME); 
	printf("%s %s\n", CPY_RGHT, AUTHOR); 
	return; 
} 

///////////////////////// 
/// \fn get_choice( void )
/// \brief Check to see if user wants to continue 
/// 
/// \b SideEffects: None\n 
/// \b Output: Prompts user to continue program\n
/// 
/// \param None 
/// 
/// \return char ch - Choice entered by user 
///////////////////////// 
char get_choice( void ) 
{ 
	char ret_val; 
	char ch; 
	printf("Do you want to continue (1 = Yes) (0 = No) ?: ");
	fflush(stdin);
	scanf("%c^%*",&ch); 
	ret_val = ch; 	
	return ret_val; 
} 

///////////////////////// 
/// \fn is_valid_choice( char val ) 
/// \brief Checks for valid yes or no entry to continue 
/// 
/// \b SideEffects : None\n 
/// \b Output : Displays an error message if an invalid choice is passed \n
/// 
/// \param val - Current choice to continue 
/// 
/// \return boolean true if choice is valid and false otherwise 
//////////////////////// 
unsigned int is_valid_choice( char val ) 
{ 
	unsigned int ret_val = 1; 
	switch (val){ 
		case '0': 
		case '1': 
			break; 
		default: 
			ret_val = 0; 
			printf("Invalid choice : %c\n", val); 
			printf("Please enter one of the following(0 or 1) : \n"); 
			break; 
	} 
	return ret_val; 
} 



A C++ with Streams solution
///////////////////////////////////////
/// \file main.cpp
/// \brief Some source code file Project Main Module
/// 
/// \author My Name
/// \version 00.99.00
/// \date January 1st, 2xxx
///
//////////////////////// 
// History 
// INI 2xxx-01-01 v1.00.00 Initial release 

// Includes 
#include <iostream> 
#include <iomanip> 
using namespace std; 

// Global constants and variables 
const char PRG_NAME[] = {"MyProjectName1 - v 1.0.00"};
const char CPY_RGHT[] = {"Copyright © 2xxx - "};
const char AUTHOR[] = {"My Name"}; 

// function prototypes 
void show_copyright( void ); 
char get_choice( void ); 
bool is_valid_choice( char ); 

// Uncomment to run just the testing main
//#define TEST 1

/////////////////////////
/// \fn main(int argc, char** argv)
/// \brief Main module for Hello World program.
///
/// \param argc - integer number of arguments passed from system to program
/// \param argv - pointer to array of characters of arguments passed to program from system
///
/// \return integer value for success or failure
/////////////////////////
#ifndef TEST
int main(int argc, char** argv) 
{ 
	int ret_val = 0; // holds return value for main program 
	char choice;
	show_copyright();

	do{
		do{ 
			cout << "Hello World..." << endl; 
			choice = get_choice(); 
		}while(!is_valid_choice( choice)); 
	}while(choice == '1');
 
	return ret_val; 
} 
#else // Testing Main
int main( int argc, char** argv)
{
	int ret_val = 0;
		
	return ret_val;	
}
#endif

///////////////////////// 
/// \fn show_copyright(void) 
/// \brief Show program name and copyright notice 
/// 
/// \b SideEffects : None\n
/// \b Output : couts PRG_NAME, CPY_RGHT and AUTHOR defines\n
///
/// \return None
//////////////////////// 
void show_copyright ( void ) 
{ 
	cout << AUTHOR << "'s " << PRG_NAME << endl; 
	cout << CPY_RGHT << AUTHOR << endl << endl; 
	return; 
} 

///////////////////////// 
/// \fn get_choice( void )
/// \brief Check to see if user wants to continue 
/// 
/// \b SideEffects: None\n 
/// \b Output: Prompts user to continue program\n
/// 
/// \param None 
/// 
/// \return char ch - Choice entered by user 
///////////////////////// 
char get_choice( void ) 
{ 
	char ret_val; 
	char ch; 
	cout << "Do you want to continue (1 = Yes) (0 = No) ?: "; 
	cin >> ch; 
	ret_val = ch; 	
	return ret_val; 
} 

///////////////////////// 
/// \fn is_valid_choice( char val ) 
/// \brief Checks for valid yes or no entry to continue 
/// 
/// \b SideEffects : None\n 
/// \b Output : Displays an error message if an invalid choice is passed \n
/// 
/// \param val - Current choice to continue 
/// 
/// \return boolean true if choice is valid and false otherwise 
//////////////////////// 
bool is_valid_choice( char val ) 
{ 
	bool ret_val = true; 
	switch (val){ 
		case '0': 
		case '1': 
			break; 
		default: 
			ret_val = false; 
			cout << "Invalid choice : " << val << endl; 
			cout << "Please enter one of the following(0 or 1) : " << endl; 
			break; 
	} 
	return ret_val; 
} 



And finally a C++ with classes solution

main.cpp
///////////////////////////////////////
/// \file main.cpp
/// \brief Some source code file Project Main Module
/// 
/// \author My Name
/// \version 00.99.00
/// \date January 1st, 2xxx
///
//////////////////////// 
// History 
// INI 2xxx-01-01 v1.00.00 Initial release 

// Includes 
#include <iostream> 
#include <iomanip>
#include "oApp.h"

using namespace std; 

// Uncomment to run just the testing main
//#define TEST 1

/////////////////////////
/// \fn main(int argc, char** argv)
/// \brief Main module for Hello World program.
///
/// \param argc - integer number of arguments passed from system to program
/// \param argv - pointer to array of characters of arguments passed to program from system
///
/// \return integer value for success or failure
/////////////////////////
#ifndef TEST
int main(int argc, char** argv) 
{ 
	int ret_val = 0; // holds return value for main program 
	oApp* myApp = new oApp;

	myApp->init("Your Message Here...\n");
	myApp->set_copyright("Copyright© 2006-2008 - ");
	myApp->set_author("Your Name");
	myApp->set_program_name("YourProgramName v00.99.00");
	myApp->run();
	myApp->done();

	delete myApp;
 
	return ret_val; 
} 
#else // Testing Main
int main( int argc, char** argv)
{
	int ret_val = 0;
		
	return ret_val;	
}
#endif



oApp.h
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

class oApp{

public:
	oApp(){
		m_choice = '0';
		m_author = "My Name";
		m_copyright = "Copyright © 2xxx - ";
		m_program = "MyProjectName1 v1.0.00";
		m_message = "Hello World...";
	}

	~oApp() {};
	void init( const string s );
	void run( void );
	void done( void );

	string get_copyright( void );
	bool set_copyright( const string copyright );
	string get_author( void );
	bool set_author( const string author );
	string get_program_name( void );
	bool set_program_name( const string name );

	void show_copyright( void );

private:
	string m_author;
	string m_program;
	string m_copyright;
	string m_message;
	char m_choice;

	char get_choice( void );
	bool is_valid_choice( char val );
};



oApp.cpp
#include "oApp.h"

void oApp::init( const string s )
{
	this->m_message = s;
}

void oApp::run( void )
{
	show_copyright();

	do{
		do{ 
			cout << this->m_message << endl; 
			this->m_choice = get_choice(); 
		}while(!is_valid_choice( this->m_choice )); 
	}while(this->m_choice == '1');
}

void oApp::done( void ){}

string oApp::get_copyright( void )
{
	string ret_val = this->m_copyright;
	return ret_val;
}

bool oApp::set_copyright( const string copyright )
{
	bool ret_val = false;
	m_copyright = copyright;
	ret_val = this->m_copyright == copyright;
	return ret_val;
}

string oApp::get_author( void )
{
	string ret_val = this->m_author;
	return ret_val;
}

bool oApp::set_author( const string author )
{
	bool ret_val = false;
	m_author = author;
	ret_val = this->m_author == author;
	return ret_val;
}

string oApp::get_program_name( void )
{
	string ret_val = this->m_program;
	return ret_val;
}

bool oApp::set_program_name( const string name )
{
	bool ret_val = false;
	m_program = name;
	ret_val = this->m_program == name;
	return ret_val;
}

///////////////////////// 
/// \fn get_choice( void )
/// \brief Check to see if user wants to continue 
/// 
/// \b SideEffects: None\n 
/// \b Output: Prompts user to continue program\n
/// 
/// \param None 
/// 
/// \return char ch - Choice entered by user 
///////////////////////// 
char oApp::get_choice( void )
{
	char ret_val; 
	char ch; 
	cout << "Do you want to continue (1 = Yes) (0 = No) ?: "; 
	cin >> ch; 
	ret_val = ch; 	
	return ret_val; 
}

///////////////////////// 
/// \fn is_valid_choice( char val ) 
/// \brief Checks for valid yes or no entry to continue 
/// 
/// \b SideEffects : None\n 
/// \b Output : Displays an error message if an invalid choice is passed \n
/// 
/// \param val - Current choice to continue 
/// 
/// \return boolean true if choice is valid and false otherwise 
//////////////////////// 
bool oApp::is_valid_choice( char val )
{
	bool ret_val = true; 
	switch (val){ 
		case '0': 
		case '1': 
			break; 
		default: 
			ret_val = false; 
			cout << "Invalid choice : " << val << endl; 
			cout << "Please enter one of the following(0 or 1) : " << endl; 
			break; 
	} 
	return ret_val; 
}

///////////////////////// 
/// \fn show_copyright(void) 
/// \brief Show program name and copyright notice 
/// 
/// \b SideEffects : None\n
/// \b Output : couts m_ProgramName, m_Copyright and m_Author\n
///
/// \return None
//////////////////////// 
void oApp::show_copyright ( void ) 
{ 
	cout << this->m_author << "'s " << this->m_program << endl; 
	cout << this->m_copyright << this->m_author << endl << endl; 
	return; 
} 



This post has been edited by jwwicks: 06 August 2008 - 06:06 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Zen and the Art of Debugging C/C++ Programs in Visual Studio

#2 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Posted 03 August 2008 - 12:29 AM

Approved.
Well done jwwicks.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1