1) That is what a 'main loop' is for. Video games mostly use these. But the answer isn't putting it at the end, it is putting it at the beginning and testing it against a boolean value until the user wants to exit the application.
Sample Syntax:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
#include <conio.h>
#include <Windows.h>
int main()
{
bool endApp; // variable to test against ending the application
while (endApp != true)
{
char inputChar;
//application code here
cout << "Press Q to quit the application" << endl;
inputChar = getche();
if (inputChar == 'q' )
endApp == true; // Application will then terminate, else it will start over
}
return 0;
}
|
There are (of course) more complex and somewhat better ways to do this, but this is a start (and something I came up with on the spot ^_^).
2) Of course! This is programming! There are algorithms for doing everything! If you are learning from the book, it will teach you about user-defined functions. If you plan on being serious about programming, this will be an IMPORTANT chapter. Make sure you pay close attention.
Also, the book will not tell you this, but system("PAUSE") is good for simple applications like yours, but if you do this professionally, it is frowned upon because it is OS-specific and programming is about portability! Try using
1 2
|
cin.ignore();
return 0;
|
It does the same exact thing as system("PAUSE");
Hope I have helped you on your C++ learning quest!