Wrox Programmer Forums
|
BOOK: Ivor Horton's Beginning Visual C++ 2005
This is the forum to discuss the Wrox book Ivor Horton's Beginning Visual C++ 2005 by Ivor Horton; ISBN: 9780764571978
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Ivor Horton's Beginning Visual C++ 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 11th, 2006, 02:27 PM
Registered User
 
Join Date: May 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default A caution for users at Chapter 5, Ex. 03

When doing the Try Out for Chapter 5, Ex 3 (Pass by Pointer), I ran into a problem that the prior chapters had not quite prepared me for.


When compiling and running the book's code, you're supposed to end up with results like this:

-----------------
Address Passed = 0012FF60
Address received = 0012FF60
incr10 (pnum) = 13
num = 13
----------------------------------


However, when I ran *my* code, I ended up with this:


-----------------
Address received = 0012FF60
Address Passed = 0012FF60
incr10 (pnum) = 13
num = 3
----------------------------------


I seemed to have two problems, the "Address received" and "Address Passed" lines were reversed, and the "num" variable was showing up as 3, when it should have been 13.

I triple checked my code, and substantially, it was exactly as it should be. My function prototype was correct, the function itself was correct, I couldn't figure out what the problem was.


I ran the debugger and saw that after the function call, "num" was in fact 13 - exactly as it should be. And yet, it was still being output as 3.

Eventually, I intuited what was causing the problem.


The book code specified the output statements as this:

-----------------
cout << endl
    << "Address Passed = " << pnum;

cout << endl
    << "incr10 (pnum) = " << incr10 (pnum);

cout << endl
    << "num = " << num;
----------------------------------


However, when I typed it in, I thought I would be more efficient by typing it in like this:


-----------------
cout << endl
    << "Address Passed = " << pnum
    << endl
    << "incr10 (pnum) = " << incr10 (pnum)
    << endl
    << "num = " << num;
----------------------------------


Apparently, having all of this in a single statement caused an interesting effect insofar as the order things were processed and output.


While the book went into some detail in earlier chapters to describe the order in which things are processed by the compiler, I was not prepared for this. Nothing indicated that a simple output statement would also have to be carefully crafted in order to avoid problems.


The way I understood it was that the program (in my original faulty version) would run like this:

1. Output new line
2. Output "Address Passed = "
3. Output value of "pnum"
4. Output new line
5. Output "incr10 (pnum) = "
6. Calculate "incr10 (pnum)"
7. Output result
8. Output new line
9. Output "num = "
10. Output value of "num"


Apparently, I was mistaken. :)


So, my posting this message is for three reasons:

1. Users teaching themselves using this book should be aware of this issue, and hopefully, this post will help you avoid a lot of head-scratching frustration as you try to figure out what went wrong when everything looks like it should work fine.

2. The author may wish to take this into account for the next revision/edition, so that he can address this issue in the book before readers encounter it the hard way.

3. Can someone clarify with some detail exactly what is happening with the faulty version of my code, and how things are processed in an output statement? I want to better understand how I need to code them in the future.



Thanks! And I hope this helps someone! :)

 
Old April 14th, 2008, 02:32 AM
Registered User
 
Join Date: Mar 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

cout << endl
    << "Address Passed = " << pnum
    << endl
    << "incr10 (pnum) = " << incr10 (pnum)
    << endl
    << "num = " << num;

As I understood, to start the output, the 'cout' needs the return of the function incr10(pnum), because the return of the function has to become immediately after the string "incr10(pnum) =".

So, first of all, the 'cout' calls the function. The function is starting and outputs first of all :

    cout << endl
         << "Address received = " << num;


and only after getting the return from the function, the 'cout' can start the output...

The output '<< "num = " << num;' results in 'num = 3' instead of 13 because the program already stored the value 3 in the variable num before calling the function. Only after restarting a new 'cout', the program stores the 'new' value (created by the function) in the variable.


I hope I answered a little bit your question...

rgds,

Marc







Similar Threads
Thread Thread Starter Forum Replies Last Post
Database Problem /Chapter 03 Wrox United Example dynamicarray BOOK: Beginning ASP.NET 2.0 BOOK VB ISBN: 978-0-7645-8850-1; C# ISBN: 978-0-470-04258-8 3 February 12th, 2007 09:24 AM
chapter 03 ..i am getting error messages realiest BOOK: Beginning ASP.NET 2.0 BOOK VB ISBN: 978-0-7645-8850-1; C# ISBN: 978-0-470-04258-8 1 July 17th, 2006 03:01 PM
chapter 03 ..i am getting error messages realiest BOOK: Beginning ASP.NET 2.0 BOOK VB ISBN: 978-0-7645-8850-1; C# ISBN: 978-0-470-04258-8 0 July 7th, 2006 08:17 AM
Outlook '03 automation from Access '03 error Derek_05 Access VBA 2 February 3rd, 2006 06:09 AM
Beginning Visual C# Exercises - Chapter 03 seblake C# 1 July 19th, 2004 09:16 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.