GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #21  
Old 04-Nov-2004, 14:47
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,335
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by davekw7x
Well, I used to offer my "two cents worth" from time to time, but with the current exhange rate, I'll go for euros.

Dave
(Left Coast, USA)
I'll buy that for a dollar ten!
__________________

During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence?
  #22  
Old 07-Nov-2004, 19:21
cable_guy_67's Avatar
cable_guy_67 cable_guy_67 is offline
Senior Member
 
Join Date: Oct 2004
Location: Nescopeck, PA
Posts: 1,109
cable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the rough
Now that the local brain-trust has allocated resources and applied inverse kinetics to this proposed problem I for one am curious how the COMMITTEE for SOLUTIONS has ruled. I have a feeling that there are more than one 'correct' solutions and all authors will be appropriately awarded their accolades.



Just my long winded way of saying, "Please show us a solution." It is a curious problem for those of us that are new. In fact, a seemingly impossible task. After all, how would it even be possible to change the output from outside the main() involving only an output director and a string literal?

The only thing that I could come up with was possibly creating a version of cout or the << that sent the output desired instead of allowing the iostream version to work. The only way I can describe what I mean (at least in terms that I understand) is something similar (but more likely more complex) to this simple example:

CPP / C++ / C Code:
void lets_go(int){
    cout << "Exit 1";
    exit(1);
}
void lets_go(int, int){
    cout << "Exit 2";
    exit(2);
}

Not that I have a clue how to solve the original problem, I am just speculating on possible solution theories. In my simple example I can control (at least I think so) my exit value by passing either one int or two and having the compiler send me to the function with the proper signature. If it is possible to overload (or overwelm as I sometimes think of it ) << in some way so that before it's normal task it always sends to the console output "Welcome to My " and then << for the rest of the literal left.

Inconceivable, I am more likely to avenge my father's death by finding a six fingered man. :-)
__________________
"Opportunity is missed by most people because it comes dressed in overalls and looks like work."
--Thomas Alva Edison
"Those who would give up essential liberty to purchase a little temporary safety, deserve neither liberty nor safety."
--Benjamin Franklin
"A happy person is not a person in a certain set of circumstances, but rather a person with a certain set of attitudes."
--Hugh Downs
  #23  
Old 07-Nov-2004, 20:17
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Well, I was waiting for a few more responses and then, to my chagrin, I deleted Dave's responses!

Dave wins by default since he is the only one that responded. Dave if you have your answers, I would appreciate your posting them...

Funny enough, Dave's C answer is closer to my C++ answer and vice-a-versa.

My C++:
CPP / C++ / C Code:
#include <iostream>
#include <string>

using namespace std;

ostream& operator << (ostream& os, const char* s)
{
	string temp = "Welcome to " + string(s) + "\n";

	return os << temp;
}


int main()
{
	cout << "My world of Computing";

	return 0;
}

What makes this a pain in the a**, is that once you overload << with a string literal, you can't use it in your overload function or it keeps calling itself!

My C solution is much simpler:
CPP / C++ / C Code:
#include <stdio.h>
#define printf(string) printf("Welcome to %s\n",string);

int main(){
	printf("My World of Computing");

	return 0;
}

The C solution kind of cheats (IMO) because it uses a pre-compiler directive, so the main code is "changed" before it is compiled.
  #24  
Old 07-Nov-2004, 22:24
cable_guy_67's Avatar
cable_guy_67 cable_guy_67 is offline
Senior Member
 
Join Date: Oct 2004
Location: Nescopeck, PA
Posts: 1,109
cable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the rough
Quote:
Originally Posted by dsmith
Well, I was waiting for a few more responses and then, to my chagrin, I deleted Dave's responses!

Dave wins by default since he is the only one that responded. Dave if you have your answers, I would appreciate your posting them...

Hooray for Dave. 8-)

Quote:
Originally Posted by dsmith_solution_cpp
ostream& operator << (ostream& os, const char* s)
{
string temp = "Welcome to " + string(s) + "\n";

return os << temp;
}

Ok, thanks D. I was on the right track. You 'stole' the call that would normally end up here,
Quote:
Originally Posted by <ostream>line130-137
CPP / C++ / C Code:
      __ostream_type&
      operator<<(__ostream_type& (*__pf)(__ostream_type&));
      
      __ostream_type&
      operator<<(__ios_type& (*__pf)(__ios_type&));
      
      __ostream_type&
      operator<<(ios_base& (*__pf) (ios_base&));

Quote:
Originally Posted by dsmith
What makes this a pain in the a**, is that once you overload << with a string literal, you can't use it in your overload function or it keeps calling itself!

I know this doesn't follow the original question but adding the line to the original code would have bad results as well because of the additional <<.
CPP / C++ / C Code:
void main()
{
  cout << "My World of Computing" << endl;
}

Would output:

Welcome to My World of Computing
<-from << after cout
Welcome to <-from << before endl
<-from endl

If I am correct is there a useful application to this other than forcing people like me to delve into the depths of header files. Where by the way there are gems like this to find.
Quote:
Originally Posted by <cmath>
CPP / C++ / C Code:
// Get rid of those macros defined in <math.h> in lieu of real functions.
#undef abs
#undef div

// ...

#undef tanh

// ...and in the darkness bind them...
namespace __gnu_cxx
{
  namespace  __c99_binding

Hmm.. compiling the example with the additional << and endl changes nothing. But, adding the line,
CPP / C++ / C Code:
void main()
{
  cout <<”My World of Computing” ;
  cout <<"What the ...";
}
Outputs:

Welcome to My World of Computing

Welcome to What the ...

So it cout that is being overloaded. What would this be good for? Appending something like "<computer says>" then the actual output. Interesting stuff. Thanks to all.

Thanks D, my brain is now a little more fullerer. :-)
__________________
"Opportunity is missed by most people because it comes dressed in overalls and looks like work."
--Thomas Alva Edison
"Those who would give up essential liberty to purchase a little temporary safety, deserve neither liberty nor safety."
--Benjamin Franklin
"A happy person is not a person in a certain set of circumstances, but rather a person with a certain set of attitudes."
--Hugh Downs
  #25  
Old 08-Nov-2004, 00:08
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,335
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
My C solution is nearly identical to D's:

CPP / C++ / C Code:
#include <stdio.h>

#define printf(xx)  fputs("Welcome to ", stdout);fputs(xx, stdout);

int main()
{
    printf("My World of Computing");
        
    return 0;  
}
__________________

During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence?
  #26  
Old 08-Nov-2004, 02:37
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 1,032
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough
Thanks, guys, I was really curious to see what you will come up with for the C part!
I never thought about the precompiler directive, I have just "overriden" the printf; I'm not sure that it works on every compiler, so please be merciful about my code

CPP / C++ / C Code:
// Gid.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "stdio.h"
#include "conio.h"

void printf(char * textToPrint);

int main(int argc, char* argv[])
{

	printf("The world of computing");
	getchar();
	return 0;
}

void printf(char * textToPrint)
{
	fputs("Welcome to ", stdout); 
	fputs(textToPrint, stdout);

}

I originally used puts for printing the text; this added a newline, so I STOLE the fputs from Walt

As for the C++ thing, I didn't give it much thought since I read something similar in Stroustrup's "The C++ Programming Language" - "Operator Overloading"; there is also a very interesting test here; so, it's not like I could take any credit for that; d actually came up with a very good solution, with a great adding from Walt

Can't wait to see Dave's code!

Best regards,
Luci
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
  #27  
Old 08-Nov-2004, 03:36
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 1,032
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough
Nope, it doesn't work on .NET
Guess the #define is the solution!
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
  #28  
Old 08-Nov-2004, 08:06
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Quote:
Originally Posted by cable_guy_67
So it cout that is being overloaded. What would this be good for? Appending something like "<computer says>" then the actual output. Interesting stuff. Thanks to all.

My opinion? This doesn't really serve much in and of itself, except to have a simple, straight forward example of how operator overloading is done. The real power of operator overloading is when you can overload the operator based upon a class that you have defined. Crystalattice has posted this sample here that shows the real use of operator overloading.

The C sample? It can be very convenient to use #define such as Walt and I did. However, it does not seem to be very smart in this case as it replaces the printf function in all cases for this program. Again, a good simple sample showing how it is done, but useful, as it is implemented, for not much
  #29  
Old 08-Nov-2004, 09:45
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by dsmith
My opinion? This doesn't really serve much in and of itself,

...

it does not seem to be very smart in this case as it replaces the printf function in all cases for this program.

First of all, this is not an exercise in Good Things to do in your program. It's a puzzle. Even though I would never do any of these things in a "real" program, I feel that it is somewhat instructive to see what you can do within the rules of standard C and C++.

In particular, I think it shows the dangers of macros and operator overloading. Both are very powerful tools but can lead to obfuscated code that is very difficult to debug.

Well, I have two examples in C and one in C++. The first one in C is similar to the ones posted, except I used fprintf() instead of fputs().

CPP / C++ / C Code:
/* 
 *
 * This code is released to public domain: no copyright,
 * copyleft or anything else.
 *
 * Dave Evans
 * Santa Rosa, CA
 * November 2, 2004
 *
 * Here's the original code:
 *
 *************************************************************
 * #include <stdio.h>
 *
 * int main()
 * {
 *   printf("My World of Computing");
 *   return 0;
 * }
 *************************************************************
 *
 * The challenge was, without changing the body of main(),
 * to make it print the following:
 *
 *   Welcome to My World of Computing
 *
 * Now, I have discovered a wonderful way to do this, but the margins
 * of my notebook are too small to include it here (Just kidding, folks).
 *
 * Here's one way to do it:
 */
#include <stdio.h>

#define printf(x) fprintf(stdout,"Welcome to %s", x)

int main()
{
  printf("My World of Computing");
  return 0;
}

Here's another in C, that uses a local version of printf(). Note that in C the only special function name is main(). You had better not have anything in your namespace called main. Everything else is fair game (except, of course reserved keywords like if, for, etc.)

[edit on November 8, 2004]

I probably should have used fputs(st, stdout) instead of fputs(st) in the following, since puts() appends a newline, and, therefore changes the output. (Now, I say that my program as shown here does print out "Welcome to My World of Computing", as required in the rules, and I always follow the rules. However, some have said that it was implied that the original program's output should be unchanged except for prepending "Welcome to ". Oh, well...)

Dave

[/edit]

CPP / C++ / C Code:
/* 
 *
 * This code is released to public domain: no copyright,
 * copyleft or anything else.
 *
 * Dave Evans
 * Santa Rosa, CA
 * November 2, 2004
 *
 * Here's the original code:
 *
 *************************************************************
 * #include <stdio.h>
 *
 * int main()
 * {
 *   printf("My World of Computing");
 *   return 0;
 * }
 *************************************************************
 *
 * The challenge was, without changing the body of main(),
 * to make it print the following:
 *
 *   Welcome to My World of Computing
 *
 * Now, I have discovered a wonderful way to do this, but the margins
 * of my notebook are too small to include it here (Just kidding, folks).
 *
 * Here's one way to do it:
 */
#include <stdio.h>
#include <string.h>            /* first line of new stuff */
int printf(const char *s,...)
{
  char st[100] = "Welcome to ";
  strcat(st, s);
  puts(st);
  return 1;
}                              /* last line of new stuff */

int main()
{
  printf("My World of Computing");
  return 0;
}

Finally, the C++ routine, also using a macro. (Maybe this is why Bjarne Stroustroup has said that he would like to eliminate macros from the face of the earth, or something like that.)

CPP / C++ / C Code:
/* 
 *
 * This code is released to public domain: no copyright,
 * copyleft or anything else.
 *
 * Dave Evans
 * Santa Rosa, CA
 * November 2, 2004
 *
 * Here's the original code:
 *
 *************************************************************
 * #include <iostream>
 * using namespace std;
 * 
 * int main(){
 * 
 *    cout << "My World of Computing";
 *
 *    return 0;
 *}
 *************************************************************
 *
 * The challenge was, without changing the body of main(),
 * to make it print the following:
 *
 *   Welcome to My World of Computing
 *
 *
 * Now, I have discovered a wonderful way to do this, but the margins
 * of my notebook are too small to include it here (Just kidding, folks).
 *
 * Here's one way to do it:
 */

#include <iostream>
using namespace std;

#define cout std::cout<<"Welcome to " /* new line inserted here */

int main()
{
  cout << "My World of Computing";

  return 0;
}

Regards,

Dave
Last edited by davekw7x : 08-Nov-2004 at 10:54.
  #30  
Old 08-Nov-2004, 09:59
cable_guy_67's Avatar
cable_guy_67 cable_guy_67 is offline
Senior Member
 
Join Date: Oct 2004
Location: Nescopeck, PA
Posts: 1,109
cable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the rough
Thanks for the replies everyone. I just wanted to make sure. (never hurts to ask especially here) It is very interesting and I do have a much better understanding of the raw power of overloading (for good and evil). Just because you can do something doesn't mean you should.

Well, on to new things for me, thanks for the lesson.
__________________
"Opportunity is missed by most people because it comes dressed in overalls and looks like work."
--Thomas Alva Edison
"Those who would give up essential liberty to purchase a little temporary safety, deserve neither liberty nor safety."
--Benjamin Franklin
"A happy person is not a person in a certain set of circumstances, but rather a person with a certain set of attitudes."
--Hugh Downs
 
 

Recent GIDBlogProblems with the Navy (Chiefs) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
help with coding expectation maximization needed. thanks edge C Programming Language 8 08-Nov-2005 06:00
Some small things Allowee GIDSearch™ 6 14-Jul-2004 02:40
This is a small snippet from a much larger piece. Tang_Quester Open Discussion Forum 1 19-Mar-2004 02:17
Free 1st month / Free setup / No credit card needed...Plans start at 4.95 LarryIsaac Web Hosting Advertisements & Offers 0 11-Oct-2003 15:03
Script needed for letting user input a few days of data for tracking and analysis. tradertt MySQL / PHP Forum 3 06-Mar-2003 03:54

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 00:21.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.