Here are some things that I noticed that may help you improve your code.
Strive for portability where practical
Sometimes it's necessary to write non-portable code that is specific to a particular operating system or environment, but this is not one of those times. By doing two simple things -- omitting "stdafx.h" and using main() instead of non-standard _tmain() your code will still compile and run correctly under Windows, but also under any other operating system which supports C++. Since it costs essentially nothing and gains a great deal (not least because more Code Review reviewers will be able to try your code) it makes sense to use portable constructs where you can.
Only #include required headers
This program uses std::cout and so it needs <iostream> but makes no use of std::string and so it does not need <string>. Make sure you know the difference between a plain old C-style string constant such as "*" and a std::string.
Eliminate return 0 at the end of main
When a C++ program reaches the end of main the compiler will automatically generate code to return 0, so there is no reason to put return 0; explicitly at the end of main. This has been the case in C++ since the 1990 standard and in C since the 1999 standard.
Don't abuse using namespace std
Putting using namespace std at the top of every program is a bad habit that you'd do well to avoid. It isn't necessarily wrong to use, but be aware of when you absolutely shouldn't do it (such as in header files).
Learn and use C++ idioms
In C++ one would usually write --h rather than h=h-1. Similarly instead of a conditional expression h != 0 one would typically write simply h since it's functionally identical in C++ or C.
Prefer using for rather than while
The loop that you have is more idiomatically expressed as a for loop rather than as a while loop.
for (int h = 20, bl = 1; h; --h, bl+=2) { for (int i = 0; i != h; i++) { cout << " "; } for (int i = 0; i != bl; i++) { cout << "*"; } cout << endl; }
We do this because then it is clear that h and bl are only defined within the for loop and because it is now clear that they are altered each iteration.
Prefer to decrement to zero
On many machine architectures, zero is treated specially. There are often shorter instructions for loading or testing for zero, so for many machines, it's more efficient (in terms of both code space and execution time) to decrement rather than increment through a loop:
for (int i = h; i; --i) { cout << " "; }
This is often (somewhat derisively) called a micro-optimization, but if you get into the habit of writing loops this way, by the time you are working on large, major projects in which the cumulative effects add up to "software that's too slow", you'll not have to relearn how to code simple loops.
Know the difference between --i and i--
In a short simple program such as this one, it makes no difference, but over your programming career, you will use both --i and i--. The difference is that the pre-decrement version (--i) decrements and then returns the value, while the post-decrement version (i--) saves the original value, decrements and then returns the original undecremented value. Modern compilers are smart enough to understand when you're not actually saving the value and will optimize them both to the same exact code (as with this case), but again, it's useful to know the difference.
Omit argc and argv unless you use them
This code doesn't actually use argc or argv so it would be better to omit them. This gives a very good clue to the reader of your program's source code that it takes no command-line parameters.
Use named constants where useful
The only constants here are 20 and 1 so for such a simple program it doesn't make much difference but it makes sense to get into the habit of naming constants. That way, in a larger program, when you have five constants, all with different uses but with the value 20 it will be possible to easily change one constant without having to hunt through the program for all instances of 20 and wondering which constant was meant.
Applying these tips
Here's a version of the code with those tips applied:
#include <iostream> int main() { const int PYRAMID_HEIGHT = 20; const int PYRAMID_TOP_WIDTH = 1; for (int h = PYRAMID_HEIGHT, bl = PYRAMID_TOP_WIDTH; h; --h, bl+=2) { for (int i = h; i ; --i) { std::cout << " "; } for (int i = bl; i ; --i) { std::cout << "*"; } std::cout << std::endl; } }