Please critique my debugging method

I am hopeful that this introductory paragraph will properly frame my level of understanding, software used, and resources to target my goal. I am engaged in independent study which is not related to any classroom work. My source is Ivor Horton's Beginning Visual C++ 2010. I am using Visual C+ 2010 Express, not a paid version of Visual Studio. Although I know a little C and C++, I am trying to confine my studies to the concepts presented in the book without jumping ahead to something I already know. I believe I will gain a better understanding of the concepts this way.

The example is a trivial CLR console program using precompiled headers. While previewing code presented in the book as an example, I saw potential problems with it. I am presenting the mindset I used to debug the program in hopes that replies may teach me a better way to debug.

In the code that follows, I saw two problem areas which I attacked step-wise. The first problem I noted was that the console window would not stay open. The second was that the statements after the last if block would execute every time. This would not provide the desired output. I am posting the book code, followed by my debugging, and my revised code in that order.

Here is the code from the book.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// ex3_15.cpp : main project file.

#include "stdafx.h"

using namespace System;

int main(array<System::String ^> ^args)
{
    wchar_t letter;									//corresponds to the C++/CLI Char type
	Console::Write(L"Enter a letter: ");
	letter = Console::Read();

	if(letter >= 'A')								//test for 'A' or larger
		if(letter <= 'Z')							//test for 'Z' or smaller
		{
			Console::WriteLine(L"You entered a capital letter.");
			return 0;
		}

	if(letter >= 'a')								//test for 'a' or larger
		if(letter <= 'z')							//test for 'z' or larger
		{
			Console::WriteLine(L"You entered a small letter.");
			return 0;
		}  

	Console::WriteLine(L"You did not enter a letter.");
    
	return 0;
}


My first step was to include Console::ReadLine(); between each grouping of Console::WriteLine(...); and return 0; in the book example code. There were no problems with the build, but the console window still would not stay open.

Next I removed all but the last return 0; statement. There was no problem with the build. Testing input data provided the correct capital/small messages plus the not a letter message. I expected this because the false evaluation of the if statements transferred the flow of the program to the last statement before the final return 0; statement.

The easiest resolution of that problem seemed to be the to compose another if block. Keep in mind that I am attempting to avoid jumping to concepts not covered by the book yet. The resulting if statement is a somewhat complex combination of logical operators. It looks ugly to me, but it works.

Here is my revised code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// ex3_15.cpp : main project file.

#include "stdafx.h"

using namespace System;

int main(array<System::String ^> ^args)
{
    wchar_t letter;									//corresponds to the C++/CLI Char type
	Console::Write(L"Enter a letter: ");
	letter = Console::Read();
		
	if(letter >= 'A')								//test for 'A' or larger
		if(letter <= 'Z')							//test for 'Z' or smaller
		{
			Console::WriteLine(L"You entered a capital letter.");
			Console::ReadLine();
		}

	if(letter >= 'a')								//test for 'a' or larger
		if(letter <= 'z')							//test for 'z' or larger
		{
			Console::WriteLine(L"You entered a small letter.");
			Console::ReadLine();
		}

	if(!(((letter >= 'A') && (letter <= 'Z')) || 
		 ((letter >= 'a') && (letter <= 'z'))))
	{
		Console::WriteLine(L"You did not enter a letter.");
		Console::ReadLine();
	}

	Console::ReadLine();				
	
	return 0;
}

		


Please critique my debugging method and the code I used to revise the program. I will find your input helpful to build good debugging skills.

Thank you for your time and efforts.
drpepper
Last edited on
I think you certainly solved your problems :). I don't know that you need the three ReadLines() in the if statements.


I guess your book expects you to be running this program in a console, rather than in an IDE. This would solve both of those problems. The console wouldn't close because it was already open (and the IDE didn't have to open it). The purpose of the returns in the if statements would be that if is was a letter, then the program would end. This would make the final "You did not enter a letter." appear only when you didn't enter a letter.
I don't see a method description.

The first problem I noted was that the console window would not stay open.
¿Why should it?
The second was that the statements after the last if block would execute every time
Nope, there are return 0; inside the if statements.

But yes, the style is poor.
Keep in mind that I am attempting to avoid jumping to concepts not covered by the book yet.
I suppose that else should be covered
1
2
3
4
5
6
7
8
9
if( letter>='A' and letter<='Z' ){ //isupper(letter)
//...
}
else if(letter>='a' and letter<='a'){ //islower(letter)
//...
}
else{
//...
}

Don't know about CLI
Thank you both for your replies.

The console window would not stay open. This is an issue the author indicated as a nuisance in Visual C++ 2010, definitely not a desired behavior. If the window does not stay open until receiving a command to close it, how can it be viewed to verify proper output?
_________________

LowestOne:

Testing input data revealed that the ReadLines(); were necessary. Failure to include them resulted in the window failing to stay open for my inspection of the output.

Yes, the book example is for a console program. The console program is opened from within the IDE using a Wizard. The options include an empty project and one with precompiled headers. The author recommends not using empty projects. In native C++ I do use empty projects and include the preprocessor directives and namespaces manually. In C++/CLI I don't have sufficient knowledge to know which ones are necessary. I have tried. It is a disaster.

I just added the return 0; statements to double check. With the return 0; statements included the window closes prematurely. I understand why the program should terminate after any block executed by a true evaluation of the if statement. This is what it did, but it did not wait for me to view the window as explained. That is why I had to eliminate the return 0; statement.

During testing of input data all non-letter input did fall through to the not a letter message. Once again to view the window the added ReadLines(); and removal of return 0; were necessary to view the window. I tried it all step-wise to arrive at this conclusion. Previous book exercises had already pointed out this behavior to me.

As stated in my first post, I am using the free version. I do not know if the paid version resolves this issue or not. I cannot afford or justify the expense of the paid version. The cheapest price I found was over $1000. The budget simply will not support this expenditure.
__________________

ne555:

I don't see a method description.
I did not consider the programming definition of method. I was not referring to the code. I meant the method I used to debug the program as described in the post.

Why keep the window open? The initial paragraph in this post answers that.

the use of the return 0; statements inside the if statements ... As indicated in my reply to LowestOne, I tried that again before posting my reply. It did not behave as expected. I do not know why. It just did not.

the style is poor ... I agree. That is why I stated that the code looked ugly to me. The author stated very early in the book that some of the example code is not a preferred method. The examples are written to display proper syntax, construction, and a visual example of the current concept being addressed.

I had considered using else if and if. This is not the first book example which did not perform as expected. Up to this point in time I have made every effort to avoid massive rewriting programs which did not perform properly. Adding an additional if block seemed to be the easiest way to avoid a more massive rewrite of the code supplied by the book.

As stated above, I cannot justify or afford the paid version of Visual C++. I do not know if my problems are related to features and capabilities not available in the free version.
_________________

With my explanations of program performance and the steps I took to resolve the issues in mind, did I use good troubleshooting? Should I have attacked it in a different manner? The reason for my questions is that I want to adopt good techniques before I get into more advanced programming. It will be much easier to learn good techniques now on the trivial programs in the early stages of the book than to learn on more complex programs.

drpepper

I meant the method I used to debug the program as described in the post.
Me too. You just say:
_ This was the problem
_ This is the solution.
But you never say 'how' did you arrive at that. (code inspection, step-by-step execution, analysis tools, etc)
Maybe I didn't read close enough.

The first problem I noted was that the console window would not stay open.
The second was that the statements after the last if block would execute every time.
¿How did you observe the second problem (always "You did not enter a letter") if the window would not stay open?

I understand why the program should terminate after any block executed by a true evaluation of the if statement. This is what it did, but it did not wait for me to view the window as explained.
Look at your last code.
It doesn't matter what path you take, you always execute Console::ReadLine(); two times.
I suppose that it works as istream::getline(), the first call will return an empty string because that's what remains in the buffer after the character is read.

You may want to try http://stackoverflow.com/a/1152873 so the code doesn't need modifications.
ne555,

We were not communicating. My apologies!

I examined the code as presented in the example in the book before opening a new project. Because all of the previous CLR console programs failed to keep the window open in the absence of Console::ReadLine();, I was confident that this example would exhibit the same behavior. Building and starting the program without debug confirmed this analysis. For the purpose of this reply I need to add the fact that the book has not provided any solution other than using Console::ReadLine(); to keep the window open as of this stage of instruction.

The second problem presents a slightly more complicated explanation. I had the correct initial analysis for what may be the wrong reasons. The final return 0; statement presented a no brainer for me. It returns control to the system by terminating the program. Prior to this exercise the only other use of a return statement I had encountered was a function returning a value to the program.

This left me with two possible interpretations. I explored both possibilities in my head and came to the same conclusion for both interpretations. At this point I had not opened a new project. I was still thinking out the results of line-by-line execution of the code in the book, playing computer before typing any code. Please put all of this in the context of my very shallow programming experience and the very early stages of the book. I am currently on page 158 of a 1231 page book.

My reasoning for one interpretation was that the braces containing the if block represented a complete instruction set. As such the behavior would be similar to a function executing and returning a value. This seemed to indicate that the return 0; statement would serve to exit the code block and continue with the next executable statement (not terminate the program). No matter what data is entered, it seemed to point to the eventual execution of the last WriteLine statement. I saw nothing to prevent this. Your previous statement
Nope, there are return 0; inside the if statements.
indicates that I had the wrong analysis.

The second interpretation led me through this thought process. If the other analysis is incorrect, what else could it mean? Does the return value represent a Boolean value? I "played computer" with the premise that this is a Boolean value. A Boolean false evaluation is zero in integer form.

I pondered the behavior of the if blocks. If the condition evaluates to false (0), the block is ignored and the next executable statement executes. I had a little trouble reconciling the results of the condition evaluating to true. Without a doubt the code inside the block would execute. What about the return 0; statement at the end of the block? Would that cause the false condition to execute also? In the unlikely event that both the false and the true options would execute, that seemed to point to the last WriteLine executing also.

At this point the only thing to do was to type the code, build the program, and run it to observe the results.
¿How did you observe the second problem (always "You did not enter a letter") if the window would not stay open?
I am in the habit of repeating whatever action causes a window to close prematurely in an attempt to read the text. On the first repeat, I noticed that there were two lines of text where there should be only one. After about a couple dozen repetitions, I had deciphered all the text.

As for executing Console::ReadLine(); twice for capital letter, small letter, and not a letter, I noticed that also. It is senseless to me. However, testing the program with multiple combinations of additions and deletions of the Console::ReadLine(); and return 0; statements left me with only one combination which exhibited the desired behavior for all three categories of data tested by the program. I provided input data for all three categories to test each modification of the code I tried.

The link you provided contains some functions I am not familiar with. I will test them to see if they work in the example code. System("pause") is not supported by C++/CLI as far as I can tell. There is a passage contained in the link which states that Windows is non-standard. When attempting to research some of the code in Visual C++ I find that to be true. In many cases I find that code examples and explanations only work in native C++. So far I have had no trouble with any of the examples using native C++. Several of the C++/CLI examples have required modification to run properly.

This book has the structure of a text book which begs for classroom lecture to supplement many of the topics. There appears to be too much information that requires previous knowledge or is left to research in order to clearly understand what is going on.

Anyway... I believe I have answered your questions. Ask more if you wish. I await your reply.

regards,
drpepper
Last edited on
drpepper
I'm in the same book/chap/exercise as what you posted, and had the same problem.

After much searching, as yourself, I stumbled upon a solution that's more "elegant" than yours, though I don't know why this exercise doesn't work like others, and I don't believe my solution is the best.

My solution was to write the "Console::ReadLine();" twice before each "return 0;"
I.E.:
Console::ReadLine();
Console::ReadLine();

P.S.

Go to Microsoft's DreamSpark (https://www.dreamspark.com/) website to download a full FREE copy of "Visual Studios 2010". You must be a student to do this, but to comply with this requirement you just need to go to your local Junior College and register for anything.

Cheers,
Ima767god
Last edited on
Topic archived. No new replies allowed.