Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > BOOK: Ivor Horton's Beginning Visual C++ 2010
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Ivor Horton's Beginning Visual C++ 2010
This is the forum to discuss the Wrox book Ivor Horton's Beginning Visual C++ 2010 by Ivor Horton; ISBN: 9780470500880
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Ivor Horton's Beginning Visual C++ 2010 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Display Modes
  #1 (permalink)  
Old June 26th, 2012, 04:34 PM
Authorized User
Points: 267, Level: 5
Points: 267, Level: 5 Points: 267, Level: 5 Points: 267, Level: 5
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2012
Posts: 66
Thanks: 0
Thanked 4 Times in 4 Posts
Default resolving problems with Ex3_15

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

Last edited by drpepper; June 26th, 2012 at 06:55 PM. Reason: correct incorrect code entry
  #2 (permalink)  
Old October 25th, 2013, 09:19 PM
Registered User
Points: 12, Level: 1
Points: 12, Level: 1 Points: 12, Level: 1 Points: 12, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Ex3_16

The solution seems to be to insert "Console::ReadKey();" not "Console::ReadLine();" before each instance of "return 0;", but I don't know why.

Bob
 


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
resolving entities stolte XSLT 5 February 24th, 2011 12:31 PM
resolving localhost phin Internet Information Services 5 February 26th, 2007 11:34 AM
Resolving entities when reading XML dmorin C# 0 September 25th, 2006 09:19 AM
Resolving a URL using XSLT vibin_nair XSLT 1 October 21st, 2003 03:53 AM



All times are GMT -4. The time now is 02:24 AM.


Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.