Wrox Programmer Forums

Need to download code?

View our list of code downloads.

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 September 28th, 2013, 12:27 AM
Registered User
Points: 5, Level: 1
Points: 5, Level: 1 Points: 5, Level: 1 Points: 5, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2013
Location: Plano, TX
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Chap 3 Exercise 2

I'm new to both this forum and Visual C++ so let me know if I pull a forum no-no.

I try all the exercises as I go through the book and sometimes embellish them to improve my understanding of various C++ features.
Chap 3 Exercise 2 asks you to:
Write an ISO/IEC C++ program to read characters from the keyboard and count the vowels. Stop counting when a Q (or a q) is encountered. Use a combination of an indefinite loop to get the characters, and a switch statement to count them.

Then Exercise 5 asks you to write a CLR version of the program.

Here's the CLR version of my program, the ISO/IEC program worked without a problem.
Code:
// Ch3_Exercise_2.cpp : main project file.

#include "stdafx.h"

using namespace System;

int main(array<System::String ^> ^args)
{
int vowels(0);
int consonants(0);
wchar_t letter;

	Console::Write("This program keeps a running total of vowels and consonants that you input.\n");
	Console::Write("It stops counting when you enter a q or Q.\n\nEnter a letter at the ? prompt.\n\n");
	do
	{
	start: Console::Write(L"? ");			// Prompt for input
		letter = Console::Read();			// Read input character
		letter = Char::ToUpper(letter);		// Convert to a capital letter
		if(letter == 'Q')				// Check for input of q or Q
				{
					Console::WriteLine(L"You have entered a total of {0} vowels and {1} consonants.", vowels, consonants);	// Output number of vowels and consonants entered
					Console::ReadLine();	// Added to halt so you can read the statement directly above.  Ignored if line 19 is ommited.
					return 0;				//Terminate if q or Q
				}
					if(!((letter >= 'A') && (letter <= 'Z'))) // Test for non-letter
					{
						Console::WriteLine(L"That was not a letter. Please try again. Letter = 0x{0:x}", safe_cast<int>(letter)); // Output not a letter and output Hex value of letter
						goto start;			// Return to inputting characters
					}
						switch(letter*((letter >= 'A') && (letter <= 'Z')))				//Setup switch to parse for vowels / consonants
						{
							case 'A': case 'E': case 'I': case 'O': case 'U': 
									++vowels;											//Increment vowels for case A,E,I,O,U
									Console::WriteLine(L" Added one to vowels");
									break;
							default: ++consonants;										//Increment consonants for case letter != A,E,I,O,U
									Console::WriteLine(L" Added one to consonants");
									break;
						}
	}
	while(true);		// Continuous loop
return 0;
}
For which I get an output of:

This program keeps a running total of vowels and consonants that you input.
It stops counting when you enter a q or Q.

Enter a letter at the ? prompt.

? a
Added one to vowels
? That was not a letter. Please try again. Letter = 0xd
? That was not a letter. Please try again. Letter = 0xa
? s
Added one to consonants
? That was not a letter. Please try again. Letter = 0xd
? That was not a letter. Please try again. Letter = 0xa
? 1
That was not a letter. Please try again. Letter = 0x31
? That was not a letter. Please try again. Letter = 0xd
? That was not a letter. Please try again. Letter = 0xa
? q
You have entered a total of 1 vowels and 1 consonants.

QUESTIONS:

1) Where did the ? That was not a letter. Please try again. Letter = 0xd
and ? That was not a letter. Please try again. Letter = 0xa come from?
2) 0xd and 0xa are not hex representations while the 0x31 is correct. What's going on here?
3) I thought that the {0:X} format automatically added the 0x to the output without you having to add it.
4) The last line of output flashes by in milliseconds because the Console::ReadLine() is ignored.

In a totally serendipitous action I put a Console::ReadLine() statement after the letter = Console::Read() line.
VOILA!!!
The program works properly. Even the Console::ReadLine() at the end now works.

The code is now:

Code:
// Ch3_Exercise_2.cpp : main project file.

#include "stdafx.h"

using namespace System;

int main(array<System::String ^> ^args)
{
int vowels(0);
int consonants(0);
wchar_t letter;

	Console::Write("This program keeps a running total of vowels and consonants that you input.\n");
	Console::Write("It stops counting when you enter a q or Q.\n\nEnter a letter at the ? prompt.\n\n");
	do
	{
	start: Console::Write(L"? ");			// Prompt for input
		letter = Console::Read();			// Read input character
		Console::ReadLine();			// ********The mystery line that makes it work*********
		letter = Char::ToUpper(letter);		// Convert to a capital letter
		if(letter == 'Q')				// Check for input of q or Q
				{
					Console::WriteLine(L"You have entered a total of {0} vowels and {1} consonants.", vowels, consonants);	// Output number of vowels and consonants entered
					Console::ReadLine();	// Added to halt so you can read the statement directly above.  Ignored if line 19 is ommited.
					return 0;				//Terminate if q or Q
				}
					if(!((letter >= 'A') && (letter <= 'Z'))) // Test for non-letter
					{
						Console::WriteLine(L"That was not a letter. Please try again. Letter = 0x{0:x}", safe_cast<int>(letter)); // Output not a letter and output Hex value of letter
						goto start;			// Return to inputting characters
					}
						switch(letter*((letter >= 'A') && (letter <= 'Z')))				//Setup switch to parse for vowels / consonants
						{
							case 'A': case 'E': case 'I': case 'O': case 'U': 
									++vowels;											//Increment vowels for case A,E,I,O,U
									Console::WriteLine(L" Added one to vowels");
									break;
							default: ++consonants;										//Increment consonants for case letter != A,E,I,O,U
									Console::WriteLine(L" Added one to consonants");
									break;
						}
	}
	while(true);		// Continuous loop
return 0;
}
With an output of:

This program keeps a running total of vowels and consonants that you input.
It stops counting when you enter a q or Q.

Enter a letter at the ? prompt.

? a
Added one to vowels
? A
Added one to vowels
? s
Added one to consonants
? S
Added one to consonants
? 1
That was not a letter. Please try again. Letter = 0x31
? @
That was not a letter. Please try again. Letter = 0x40
? q
You have entered a total of 2 vowels and 2 consonants.


Which is what is expected.

SO! Why is the added Console::ReadLine() needed and whats going on without it?

The book says to avoid using goto statements. Any suggestions on how to modify my program to avoid this?

Any insight or other comments would be greatly appreciated.

Also is there a way to copy command line text rather than just re-typing it?

Thanks.

Last edited by mikenoth; September 30th, 2013 at 09:53 PM.
 


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
Chap 7 Menu exercise - Menu only Appearing in DarkGrey theme WHixson BOOK: Beginning ASP.NET 4 : in C# and VB 1 April 19th, 2011 12:11 PM
Chap 4 exercise 4 shaman BOOK: Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer 1 April 17th, 2011 09:40 AM
chap 2 exercise problem 6 iramwebtech BOOK: Beginning PHP5, Apache, and MySQL Web Development ISBN: 978-0-7645-7966-0 1 April 16th, 2009 04:14 PM
Chap. 14 Exercise 2 C#Book stoverje BOOK: Beginning ASP.NET 2.0 BOOK VB ISBN: 978-0-7645-8850-1; C# ISBN: 978-0-470-04258-8 0 July 1st, 2008 09:41 AM



All times are GMT -4. The time now is 01:56 AM.


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