So I've been having fun coding for the past few days and I notice that after I run my program and it does what it's supposed to do, it closes. How do I prevent it from closing and do the task again?
Additional questions:
1. what does #include <iostream> do? and is there a substitute for it?
2. I often see return 0; at the end of a source cods. What does it do and what's the difference if I use return 1; instead?
How do I prevent it from closing and do the task again?
You can run your code in a loop- this is an easy example:
1 2 3 4 5 6
while(1){
cout << "Enter A Number :";
int number = 0;
cin >> number;
cout << "Your Number Is 0x" << std::hex << number;
}
This code stays open after it runs a task, and runs the same task.
#include <iostream> includes the iostream header file in your project, that header file has a bunch of functions and definitions that you can use (for instance, iostream defines cout and cin to interact with the console) you can't really substitute a header file if it includes essential functionality, but you can include that header from other headers. You can also create your own functions that perform the same functionality.
Your main is actually a function, return 0 returns exit status success to the console. Not really super important in the early stages of programming, but it's good to be consistent with it.
Return 1 would simply return an exit code 1 to the console. Generally it really doesn't matter.
This code stays open after it runs a task, and runs the same task.
It also creates an infinite loop, the program will never progress out of that loop and reach the end of the program. A do/while, or game loop is best used if you want the user to be able to repeat the program http://www.cplusplus.com/doc/tutorial/control/
If you want to have any kind of interaction with a user, include iostream. Trying to explain the other questions is like explaining a second derivative to someone just starting algebra. In other words, do not worry about it, just take it at face value at this point. By the time you have learned enough to understand it, you will not need an explanation.