clrscr() is not a standard function either. Actually, I believe it is specific to Borland C++. Visual C++ doesn't have it, gcc doesn't have it, most don't, because it's not standard.
Also, you are using a member of the std namespace (cout), but you haven't told the compiler where it is from... There are several ways to do that, I'd suggest replacing
cout
with
std::cout
or declare the preprocessor directive
using std::cout;
. You could also use
using namespace std;
, but that can cause namespace conflicts, as it tells the compiler to use members from the std namespace for ALL the code, even if they are really not.
I'll admit I find it much easier to just declare the entire namespace, rather than to write "std::" in front of every member from std or stick in multiple using directives, and I haven't had any problems (so far), but it's bad practice anyway. I also accept the risk of having conflicts, and the fact that it'll probably take time to track it down to a namespace conflict.
As for pausing the program when it's done... This shouldn't be done in releases or production code... Not really a problem if you're just writing programs for yourself, but if you plan on distributing it, it's generally considered "invasive" to the user, as any console program should leave as much control to the console as possible. Pausing removes control from the console, and thus from the user as well. You can probably see why this is undesirable. If you are releasing a console application, it should be assumed that the user will run it from a command line, instead of double-clicking it, in which case there is no need to pause because the user can still see the output of the program. There's nothing wrong with it if it actually
is necessary, but it's usually not.
If you do need to use a pause in a release program, then you should use something portable, that is not constrained to one single platform and compiler/library. Use this:
1 2 3 4
|
/* Cross-Platform 'Pause' implementation -
* Takes 'Enter' key to continue, but most users press Enter anyway. */
std::cout << "Press 'Enter' to continue ... ";
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
|
As far as programs just for you, or you don't plan on anyone else seeing the code or compiling it, I see no problem using restricted functions or headers... Just keep it to yourself.
I would advise against getting in the habit of clearing the console screen.... This is very invasive behavior for a program to be doing... I would even call it evil... Only exceptions would be if you are making a program with a full UI, that HAS to clear the console to display the UI... But you may as well make a graphical program instead of console. It wouldn't be much more effort, and would be much better.
Also, you can declare variables on the same line, i.e.
int x, y;
You should also declare them locally unless you
need to make them global... meaning keep them within your functions, like main(). Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
#include<iostream.h>
using namespace std;
int main() {
int x, y;
cout<<"what is your height?"<<"\nyour feet: ";
cin>>x;
cout<<"your inches: ";
cin>>y;
cout<<"your height is: "<<x<<"'"<<" and "<<y<<"''";
cout<<"\nYou are tall!";
return 0;
}
|
And if they are being used in a loop, like the For loop for example, then you should only declare them in the loop and use them within the loop. In other words, keep their scope as minimal as possible to avoid conflicts and other problems.
And there is much, much more to learn, Have fun!
Hope I helped a bit!