Wrox Programmer Forums
| 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 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
  #1 (permalink)  
Old December 4th, 2007, 12:01 PM
Registered User
 
Join Date: Dec 2007
Location: , , .
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default Ex10_01 Identical code, different output

Main() prints to screen "The name is ", and then uses a function to
print "Ivor Horton".
Additionally, the function contains some debugging code, which outputs the text "Name::getName()called".


When I download the files from the internet, the output is:
    Name::getName()called
    The name is Ivor Horton


My own faithfully transcribed version yields a garbled version:
    The name is
    Name::getName()called Ivor Horton


Even when I delete all my own text and replace every bit of it with
the author's version, it still comes out in the wrong order. I'm
baffled.
I'm using C++express and this is a CLR blank project in both cases.
I can't find any difference, but I must be missing something.
Any suggestions welcome
Thanks tony

__________________________________________________ __
Here is part of my class main():
__________________________________________________ __


int main(int argc, char* argv[])
{
  Name myName("Ivor", "Horton");


  // Retrieve and store the name in a local char array
  char theName[12];
  cout << "\nThe name is " << myName.getName(theName);
. . .


}


__________________________________________________ _
Here is the function getName()
__________________________________________________ _

char* Name::getName(char* pName) const
{
  assert(pName != 0); // Verify non-null argument


#ifdef FUNCTION_TRACE
  // Trace function calls
  cout << "\nName::getName() called.";
#endif


  strcpy(pName, pFirstname);
  strcat(pName, " "); // Append a space
  return strcat(pName, pSurname); // Append second name and
return total




  #2 (permalink)  
Old December 4th, 2007, 03:41 PM
Authorized User
 
Join Date: Oct 2005
Location: Gateshead, Tyne and Wear, United Kingdom.
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The code for main should begin as follows:

int main(int argc, char* argv[])
{
    // Turn on free store debugging and leak-checking bits
    _CrtSetDbgFlag( _CRTDBG_LEAK_CHECK_DF|_CRTDBG_ALLOC_MEM_DF );
    // Direct warnings to stdout
    _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
    _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDOUT);

    Name myName("Ivor", "Horton"); // Try a single object
...

Secondly, your getName function returns something called "total". What is that? Omit this line. It needs to return the line before it.



Dan Crossley
  #3 (permalink)  
Old December 6th, 2007, 01:10 PM
Registered User
 
Join Date: Dec 2007
Location: , , .
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Dan,
"return total" was a word-wrapped comment. The line was too long for the window size in this forum. It should read
// Append second name and return total
The debugging code you added is way too advanced for me at the moment. I'll try it, but I don't think I'm ready to learn it yet.

I've written a simpler example of my problem:

I have two apparantly identical versions of the following project in vc++express.
One outputs "first second third",
The other, "second first third".

_________________________________________
#include <iostream>
using namespace std;


char* order(){
cout << "second ";
return "third ";
}

void main(){
/*cout<<"first "<<order(); */

}
_____________________________________


Thanks,
Tony


  #4 (permalink)  
Old December 7th, 2007, 04:53 AM
Authorized User
 
Join Date: Oct 2005
Location: Gateshead, Tyne and Wear, United Kingdom.
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Tony,

The debug information was inserted by the compiler. I didn't write it.

I don't suppose you need it anyway.

If you email me at dnt433@netscape.net, I can send you the zipped working solution for Ex10_1.

About your code, main() calls order(), which outputs "second" first, then it returns to main() and concatenates the return value of "third" with "first" in main() and outputs that next, hence "second first third".

I don't know why your other code produces a different output, as you haven't provided it.


Dan Crossley
  #5 (permalink)  
Old December 7th, 2007, 05:50 AM
Registered User
 
Join Date: Dec 2007
Location: , , .
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Dan,
Here is the other code. You'll see it is completely identical!!! but it produces the output "first second third".

I woke up at 4 am, and understood why both answers are possible (but not quite why my vc++ chooses between them randomly).


Consider this simpler program:
char* order(){
//<s>cout << "second ";</s>
return "third ";
}
void main(){
cout<<"first "<<order();
}

As always, the things in cout must be printed in left to right order, so it must be "first third".

But there is no rule about the order that they are calculated. So the program can calculate the return value of the function before or after figuring that "first " is a text string.
But it must print them in the correct order.

Unfortunately, the actual function that I used has an extra cout in the body. This will be executed when the calculations are done. And as I've said either order of calculation is legal.
...And the correct way of saying that my function has something going on in the body of it is "the function has a side effect"

I must credit two guys on usenet for helping me with this...
I didn't understand what they were saying, but my subconscious must've figured it out!
http://groups.google.co.uk/group/mic...3333e8712b5bb5


Here's my other code. You'll see it's no different
:
#include <iostream>
using namespace std;


char* order(){
cout << "second ";
return "third";
}

void main(){
cout<<"first "<<order();
}

I'll email you.

Tony






Similar Threads
Thread Thread Starter Forum Replies Last Post
Finding identical values voskoue Access VBA 7 January 30th, 2007 08:43 AM
Identical results in Drop Down List 5ilverado ASP.NET 1.0 and 1.1 Professional 0 July 3rd, 2006 12:26 AM
query for identical field and echo... flyguylol Beginning PHP 2 January 15th, 2006 05:48 PM
looping tree based on identical value of a child Kabe Classic ASP XML 0 March 17th, 2004 05:57 AM
multiple definitions with identical signatures Galina Crystal Reports 0 February 14th, 2004 05:39 PM





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