Sign in to follow this  

little question

This topic is 3871 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi,I have little problem. I wrote a little "hello world" code and it all went fine (afcourse :P ) But now my question; if I want to write an other source code like this one here: #include <iostream.h> int main() { int x,y; cout <<"vul het eerste getal in:"; cin >> x; cout <<"\nvul het tweede getal in:"; cin >> y; cout << "\n" << x+y; return 0; } Do i have to put this code in an other (new) file?

Share this post


Link to post
Share on other sites
Each program should have at most one main function. It doesn't matter which file it's in, as long as there's only one.

If you want to try several little programs, just put them each in a seperate .cpp file and compile each one as needed.

So HelloWorld.cpp compiles to HelloWorld.exe (assuming win32 here) and TryInput.cpp results in TryInput.exe

Another option might be replacing the contents of your first main function with the new code, or replacing the entire file, as blueappel suggested.

hth,
CipherCraft

Share this post


Link to post
Share on other sites
A couple tips:

1. In future, when posting source code, either surround it with [code] and [/code] tags (for short snippets of code) or [source] and [/source] tags (for longer pieces of code. The code tags gives it a fixed width font and maintains the formatting; the source tags do this, in addition to placing the code in a nice scrolling box (to stop the code from potentially distorting the page) with source colouring.

2. That code, as is, shouldn't compile -- at least, as far as I'm aware. You need to put the line using namespace std; after your #include directive or preface cout and cin with std::. I would normally recommend doing the latter, but if you're only at the 'hello world' stage, the former is absolutely the easier option until you encounter namespaces.

This is, of course, in addition to everything above. :-)

EDIT: Messed up escaping one of the tags!

Share this post


Link to post
Share on other sites

This topic is 3871 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this