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++ 2005
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 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 Search this Thread Display Modes
  #1 (permalink)  
Old May 11th, 2006, 02:27 PM
Registered User
 
Join Date: May 2006
Location: , , .
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! :)

  #2 (permalink)  
Old April 14th, 2008, 02:32 AM
Registered User
 
Join Date: Mar 2008
Location: , , .
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


 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
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
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



All times are GMT -4. The time now is 10:42 PM.


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