I am 21 years old and i am new in c++

I'm new in c++
I only start learning this month, july..
and this is my first work.. I just want to post my 1st work..
I want to be good in c++, unfortunately I don't start to
learn this program earlier in my age.. I wish it would not be
too late for me.. thank you.. =)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<iostream.h>
#include<conio.h>
int x;
int y;
main()
{
clrscr();
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!";

getch();
return 0;
}
It's never too late, but you need to get yourself a better book (you could try "Thinking in C++", it's freely available on the web).
The iostream.h header has been called iostream for 12 years already and a modern compiler should not even accept iostream.h. Neither is it allowed to omit the return type, be it main() or any other function. Replace it by int main().
I suggest installing the Code::Blocks/MinGW bundle to get a decent IDE with a modern compiler.

conio.h is not a standard header, so it's better not to use it without a good reason. getch() is not a standard function either, so just remove the call. Code::Blocks will keep the terminal window open after the program has terminated.
Last edited on
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!
Last edited on
thanks Athar and RyanCaywood..

ahm, the codes I use was just teach by our instructor, in college..
maybe their program c++ was old.. it was turbo c++ I think..
maybe its out of date..

anyway, thanks again and I will gonna practice what you have teach me..

and oh, by the way.. is this possible, to give the meaning of the letter we input..
example, type a letter: a
and it will say, a is for apple
or if b, b is for ball..

what code shall i use?
Sure, just about anything is possible in C++! You could do something like this:
(note that you can omit the braces {}, if you only have one line in the IF, or even put it on the same line, though making it a one-liner it may reduce readability...)
1
2
3
4
5
6
7
char input;
if (std::cin >> input && input == 'a')
    std::cout << "Apple\n";
else if (input == 'b') std:cout << "Ball\n";
else
    std::cout << "Not Apple or Ball (A or B)\n";
// note that the else could be one line too, but it's easier to read as an indented two-liner. 


You could also use a switch statement, though you should stick to if's for small things. Kind of like a point of diminishing return... a switch is too complex if you only need to check a couple things, but you should use a switch instead of 10 or 20 IF statements, etc.

Here is what you'd do using a switch (again, this really isn't necessary for something so small):

1
2
3
4
5
6
7
8
9
10
11
12
char input;
std::cin >> input;
switch (input) {
    case 'a':
        std::cout << "Apple\n";
        break;
    case 'b':
        std::cout << "Ball\n";
        break;
    default:
        std::cout << "Not Apple or Ball (A or B)\n";
}


Case statements can be singled lined too... actually mostly anything can be. I tend to do it often to make my code more compact, but I'm the only one who reads it, and I know what to look for, since I wrote it... If you intend others to read the code, you probably want to keep it more readable than compact... There are other standard practices that you should read up on... I won't go into the details though, unless you want to. Mainly though, if you're the only one reading it, then you can probably ignore general formatting standards... Make sure you can understand your own code though, that's the most important thing.
Last edited on
Topic archived. No new replies allowed.