473,395 Members | 1,870 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Problem with cout, please help, it's urgent

main(){
cout<<"Hello World";
}

This output will be : Hello World

But can we output "Hi! Hello World" without modifying the code in main()?

Can anyone help me out of this?
Apr 29 '07 #1
10 1473
ilikepython
844 Expert 512MB
main(){
cout<<"Hello World";
}

This output will be : Hello World

But can we output "Hi! Hello World" without modifying the code in main()?

Can anyone help me out of this?
No, whatever is in main that is what is going to be executed. If you want to output: "Hi! Hello World", just put "cout << "Hi! Hello World";. cout prints out anything after "<<". Is that what you need?
Apr 29 '07 #2
No, whatever is in main that is what is going to be executed. If you want to output: "Hi! Hello World", just put "cout << "Hi! Hello World";. cout prints out anything after "<<". Is that what you need?

Can't we write anything without disturbing the main() function ? Something outside main().
Apr 29 '07 #3
ilikepython
844 Expert 512MB
Can't we write anything without disturbing the main() function ? Something outside main().
Well you can create your own function that you call from main, but everything that happens in a program starts from main. That's where the program starts.
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. void myfunction(){
  6.     cout << "Hi! Hello World" << endl;
  7. }
  8. int main(){
  9.     myfunction();
  10.     return 0;
  11. }
  12. OUTPUT:
  13. "Hi! Hello World"
  14.  
Apr 29 '07 #4
Well you can create your own function that you call from main, but everything that happens in a program starts from main. That's where the program starts.
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. void myfunction(){
  6.     cout << "Hi! Hello World" << endl;
  7. }
  8. int main(){
  9.     myfunction();
  10.     return 0;
  11. }
  12. OUTPUT:
  13. "Hi! Hello World"
  14.  

But, again we are writing lines inside main() ! Can't we use any preprocessor directives like #define or #pragma ?
Apr 29 '07 #5
ilikepython
844 Expert 512MB
But, again we are writing lines inside main() ! Can't we use any preprocessor directives like #define or #pragma ?
I don't think so. The preprocessor doesn't know C or C++. It just alters the source file. Check out this page:
http://www.cplusplus.com/doc/tutorial/preprocessor.html
Apr 29 '07 #6
weaknessforcats
9,208 Expert Mod 8TB
If you need to chnage the display without changing main(), you could:
1) use a command line argument
2) code main() to display a variable
- have the user enter the contents from the keyboard
- or read a disc file with the correct message
Apr 29 '07 #7
main(){
cout<<"Hello World";
}

This output will be : Hello World

But can we output "Hi! Hello World" without modifying the code in main()?

Can anyone help me out of this?
i think its very easy, just try this,

class abc{
public:
abc() { cout <<"Hi !"; }
};
abc a;

main(){
cout<<"Hello World";
}
Apr 30 '07 #8
weaknessforcats
9,208 Expert Mod 8TB
This code:
class abc{
public:
abc() { cout <<"Hi !"; }
};
abc a;

main(){
cout<<"Hello World";
}
still requires that the file containing main() be changed in order ot change the message.

The only way to not change the code in order to change the message is to not have the message in the code in the first place.
Apr 30 '07 #9
JosAH
11,448 Expert 8TB
This code:


still requires that the file containing main() be changed in order ot change the message.

The only way to not change the code in order to change the message is to not have the message in the code in the first place.
But the requirements where not to change the main() method. Jesusdiehard's
solution didn't change that function. btw this is exactly the question as from
Stroustrup's book in one of his little exercises.

kind regards,

Jos
Apr 30 '07 #10
r035198x
13,262 8TB
But the requirements where not to change the main() method. Jesusdiehard's
solution didn't change that function. btw this is exactly the question as from
Stroustrup's book in one of his little exercises.

kind regards,

Jos
I thought I recognized that from somewhere.
Also, I think, the question has been asked here before.
Apr 30 '07 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: moi | last post by:
Can someone please help with this problem im having. i have to use the newton-raphson technique to find the root of a function, in this case X^2 - 1. basically, the program has to read in values...
12
by: 1111111111 | last post by:
I am writing a program that tells users to enter 2 sides of a rectangle. From that the program will output a drwaing of the rectangle using "*"'s... Here is my problem. Say I enter 5 and 10 for...
9
by: tomakated | last post by:
How do I get rid of the dashes next to the 3, 6, and the 9<?. Also the last row of "------" and the "|" on the last line so that the board will look like this<?. | | | | 1| 2| ...
5
by: trs | last post by:
thanks for reading this. problem: logical error is preventing array from sorting right code: #include <iostream> #include <cmath> using namespace std; struct coords { int x;
2
by: Dimitri | last post by:
PLEASE HELP,I HAVE A DATABSE WITH MULTIPLE RECORDS AS OUTLINED BELOW EMP NO LEVEL NEXTINCREASE WAGETYPE UNIT 1000 1 0 1000 1000 1 0 1002 ...
1
by: codergem | last post by:
Helo friends Could any of you please help me out with this problem. To me this program of heap sort seems logically perfect but still the output of this is not coming right, I dont want to follow...
4
by: cyborg81 | last post by:
when i compile the following code it gives the following error on the line indicated in bold: #include<iostream> #include<iomanip> #include<cstdlib> using std::cin; using std::cout;
14
by: audiokarate | last post by:
Hey everyone, Ok, this problem is giving me some trouble. I got it to iterate but I want it to generate an error message if a user types in an incorrect choice. I got the error message down however I...
3
by: newbprogrammer | last post by:
I just started programming in c++ and i tried to explore and do programs on my own... So i wanted to do a program that would help me in my daily school work ... a program to solve quadratic...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.