using:
ISBN-13: 978-0-470-50088-0
Visual C++ 2010 Express
On pages 157-158 under the heading
C++/CLI PROGRAMMING, the
TRY IT OUT sub-heading
A CLR Program Using Nested if Statements, the program presented problems, which I managed to resolve. I am unsure whether the problems I encountered are caused by the free version of Visual C++ excluding some of the features and capabilities of the paid version I am using or a problem in the code as presented in the book.
I am posting the code, problems, and new code which works as desired here in an attempt to find out if anyone else has encountered the same problem. The revised solution which I will include later in this post is ugly, but it works. I am also including my step-wise attempts to debug the program. Maybe someone can reply with insight regarding the cause of the problem and/or other recommended solutions. I intend to rewrite the program using
switch to see how it works. I am sure it will be prettier code.
Here is the code from the book.
Code:
// 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;
}
A note before I continue may help. I use the menu bar, not short cut keys, when building and running programs.
I had no problem with the program build. When it came time to run the program entering lowercase letters, uppercase letters, and random non-letter characters, the console window would not remain open. This all pointed me toward a logical error in the code to be debugged.
My first debugging step was to add
Console::ReadLine(); between the
Console::WriteLine(L" ..."); and
return 0; statements in all if statements. That did not resolve the issue.
Next I tried eliminating the
return 0; statement from all the if blocks while retaining the
ReadLine(); statement. Once again I did not obtain the desired results. The console window returned the
upper/lower case message
and the
not a character message for letters. Random non-letter character entries were greeted by the window not remaining open. On the up side, I had a partial improvement.
Now I just had to debug the non-letter character entries. After a few failed attempts to write if (and else) statements, I arrived at an if statement which contained a somewhat complex combination of logical operators. It works, but it is UGLY. The bottom line is that is
does work.
Here is the working code.
Code:
// 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(); //notice removal of return0;
//and addition of ReadLIne();
//throughout the remainder
//of the program
}
if(!(((letter >= 'A') && (letter <= 'Z')) ||
((letter >= 'a') && (letter <= 'z'))))
{
Console::WriteLine(L"You did not enter a letter.");
Console::ReadLine();
}
Console::ReadLine();
return 0;
}
Can any one provide an explanation why the book version did not work for me?
Are there any better solutions found by anyone having the same issue?
Is anyone interested in having me post problems and solutions to problems I encounter in the future?
regards,
drpepper