GIDForums  

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

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 01-Nov-2004, 13:12
p_kumar81 p_kumar81 is offline
New Member
 
Join Date: Nov 2004
Posts: 1
p_kumar81 is on a distinguished road

Small Help Needed


Modify the following code to print - Welcome to My World of Computing -without changing the body of main().

CPP / C++ / C Code:
void main()
{
	cout <<”My World of Computing”;
}
Last edited by dsmith : 01-Nov-2004 at 13:30. Reason: Please use [c] & [/c] for syntax highlighting
  #2  
Old 01-Nov-2004, 15:11
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 p_kumar81
Modify the following code to print - Welcome to My World of Computing -without changing the body of main().

CPP / C++ / C Code:
void main()
{
	cout <<”My World of Computing”;
}

My answer is no. That is because my compiler (gnu c++) will not compile a C++ program that does not have a return value of int.

However, if you were to ask the same question of:

CPP / C++ / C Code:
int main()
{
  cout << "My World of Computing";

  return 0;
}

My answer would be yes. You need to do it with operator overloading. Look here for information on the basics of how this is done.

Be warned, this is not as straight forward as it may seem. I did get it to work, but the string literal was a tough thing for me to overcome. I am not a C++ expert, so I am calling out those of you that are C++ experts to see what you get. I will post my ugly answer after I see a few replies...
Last edited by dsmith : 01-Nov-2004 at 15:43.
  #3  
Old 01-Nov-2004, 16:31
cable_guy_67's Avatar
cable_guy_67 cable_guy_67 is offline
Senior Member
 
Join Date: Oct 2004
Location: Nescopeck, PA
Posts: 1,108
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
You need to pay attention to the simple stuff. If you use something like cout your compiler needs to know, therefor the include.

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

int main(){

    std::cout << "My World of Computing";

    return 0;
}

You could also do it this way.

CPP / C++ / C Code:
#include <iostream>
using namespace std;

int main(){

    cout << "My World of Computing";

    return 0;
}

or even this,

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

int main(){

     using namespace std;

     cout << "My World of Computing";

     return 0;
}

Listen to dsmith, use the code tags and pay attention. This is the most basic type of program. If you don't read your compiler's errors you can't fix them for yourself. Make sure to run in a shell that does not close so you can get all the info you need. It is there to help.
__________________
"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
  #4  
Old 01-Nov-2004, 16:44
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
hehe cable_guy_67. That is all good advice, but you should read his directions again :

Quote:
Welcome to My World of Computing -without changing the body of main()

Right now the program only prints "My World of Computing", but you need to make it print "Welcome to My World of Computing" without changing the main function!

I am not a very good C++ programmer. (I dare say this can not be done in C at all though - I'm calling you out, Walt!). It took me a while to figure it out. If you get a chance, you should try it. It was p***ing me off pretty severely! This can be done with operator overloading and at first looks pretty straight forward.

Give it a try! Hint: the problem has a lot to do with the string literal.
  #5  
Old 01-Nov-2004, 16:53
cable_guy_67's Avatar
cable_guy_67 cable_guy_67 is offline
Senior Member
 
Join Date: Oct 2004
Location: Nescopeck, PA
Posts: 1,108
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
hehe cable_guy_67. That is all good advice, but you should read his directions again :

WHAT A MONKEY I AM!!! I am not worthy, I am not worthy, I spent all afternoon on a different problem.

Sorry 'bout that. I should learn how to follow directions. I will have to think about this.


Walt! Save us from ourselves, or at the very least me from me.

I must apologize for my haste, I'm usually not this cranky.
__________________
"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
  #6  
Old 01-Nov-2004, 18:02
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 dsmith
(I dare say this can not be done in C at all though - I'm calling you out, Walt!).

Well, I proved myself wrong! It is actually simpler in C (IMO). Here is the C challenge. Take this & without changing the main function, make it print "Welcome to My World of Computing"

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

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

	return 0;
}

I am actually kind of embarrased about saying that it couldn't be done in C!
  #7  
Old 01-Nov-2004, 23:32
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,234
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 dsmith
hehe cable_guy_67. That is all good advice, but you should read his directions again :



Right now the program only prints "My World of Computing", but you need to make it print "Welcome to My World of Computing" without changing the main function!

I am not a very good C++ programmer. (I dare say this can not be done in C at all though - I'm calling you out, Walt!). It took me a while to figure it out. If you get a chance, you should try it. It was p***ing me off pretty severely! This can be done with operator overloading and at first looks pretty straight forward.

Give it a try! Hint: the problem has a lot to do with the string literal.
Calling me out, eh. Well,
CPP / C++ / C Code:
void main()
{
  cout <<”My World of Computing”;
}
cannot be done in C AFAICT (as far as I can tell). The << is the main problem.

But
CPP / C++ / C Code:
void main()
{
  printf(”My World of Computing”);
}
can be done as you've figured out. Let's see if anyone else gets any ideas (don't post solutions, just indicate whether you've figured out a solution).

Maybe you can PM your answers to me or D (don't yet, please) and we can compile a list of solutions... Whatcha think, D?
__________________

Cow: You're a lawyer too?
Mooseblood (mosquito): Ma'am, I was already a bloodsucking parasite. All I needed was a briefcase!
  #8  
Old 02-Nov-2004, 06:41
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 WaltP
Maybe you can PM your answers to me or D (don't yet, please) and we can compile a list of solutions... Whatcha think, D?

Good idea. If everyone wants to PM me their answers, we will see who comes up with the best solutions and you will get a prize beyond your wildest dreams: Our respect and admiration! (nah....)

Remember, there are two riddles, the original C++ one and the C one. I've got my answers!
  #9  
Old 02-Nov-2004, 10:23
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 889
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
Quote:
Originally Posted by cable_guy_67
You need to pay attention to the simple stuff. If you use something like cout your compiler needs to know, therefor the include.

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

int main(){

    std::cout << "My World of Computing";

    return 0;
}

You could also do it this way.

CPP / C++ / C Code:
#include <iostream>
using namespace std;

int main(){

    cout << "My World of Computing";

    return 0;
}

or even this,

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

int main(){

     using namespace std;

     cout << "My World of Computing";

     return 0;
}


You should be carefull about your statements. Some people don't use the same compiler as you do (I am guessing .NET?); in VC++ 6, for example, cout has nothing to do with std.

As for your little "competition", I onestly found the "problem" easy to solve; I don't mean to be haughty or anything....If you really want too, i could send you my solution; actually, I am really curious about the C version; I mean I'm not sure if someone didn't find a nicer solution than mine; maybe we could just post it here, since this is still a forum

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

"A person who never made a mistake never tried anything new."
Einstein
  #10  
Old 02-Nov-2004, 10:29
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 LuciWiz
As for your little "competition", I onestly found the "problem" easy to solve; I don't mean to be haughty or anything....If you really want too, i could send you my solution; actually, I am really curious about the C version; I mean I'm not sure if someone didn't find a nicer solution than mine; maybe we could just post it here, since this is still a forum

Best regards,
Luci

Just PM for now. I think others may want to do this and I don't want to give the answers out.

I am glad that you got it easily, because it drove me nuts for about 20 minutes! The C was really easy though once I thought about it.

I've already got answers from Dave, which are completely different than mine but valid. It will be interesting to see how many ways this cat can be skinned. So I say for now, don't post your answers here. I will post all of the solutions after a bit.
 

Recent GIDBlogPrepping for deployment 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 05:00
Some small things Allowee GIDSearch™ 6 14-Jul-2004 01:40
This is a small snippet from a much larger piece. Tang_Quester Open Discussion Forum 1 19-Mar-2004 01:17
Free 1st month / Free setup / No credit card needed...Plans start at 4.95 LarryIsaac Web Hosting Advertisements & Offers 0 11-Oct-2003 14:03
Script needed for letting user input a few days of data for tracking and analysis. tradertt MySQL / PHP Forum 3 06-Mar-2003 02:54

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

All times are GMT -6. The time now is 16:14.


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