There are a number of things that aren't right here, but don't worry just starting out it is very hard to get the proper syntax and format down. I'll go down the list on what I noticed.
1)
1 2
|
#include <iostream>
#include <string>
|
These are #include statements. They tell you program that you are going to be using code from different header files on your system. I won't go into the details right now because that is a bit farther down the road for you but you will learn it later.
What I will say is that these ALWAYS should be at the top of your files. In your case these should be right after
#include "stdafx.h"
and before
int main()
.
2) Same thing with your using declarations.
1 2 3
|
using namespace std;
using std::cout;
using std::cin;
|
You need to have these declared before anything that uses them. So they should come right after your #include's and before
int main()
. These I also won't go into detail on but you will learn more about them later.
3) To simplify things you can use just
int main()
instead of having
int _tmain(int argc, _TCHAR* argv[])
(Which is not right I don't think).
Anyways int main() is where you program starts. It is the first thing that is called in most programs so think of it as the heart of your program. Everything that you want to run gets called in main().
Now notice that you don't have anything in main except
return 0;
. Basically right now your program does nothing, it just opens up and then closes again because there is nothing inside main().
4) There was a bunch of things wrong with what you had after int main() so I just edited your code to how I believe you wanted it to look and added some comments for you.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
#include <iostream>
#include <string> // You don't need this either since you don't use a string
using namespace std;
// using std::cout; You didn't need both of these because of using namespace std;
// using std::cin; Which include both of these.
int main()
{
/* I moved everything inside of main since this is where it should be
* and is where I believe that you wanted it. Wasn't sure what you were
* doing before but this is how you want it.
*/
int n; // You forgot to put a ; after int n.
cout << "How many Xs?" << endl;
cin >> n;
int c = 0; // This is how to initialize a variable when you declare it.
while (c<n)
{
cout <<"x";
c++;
}
return 0;
}
|
Hope this helps a bit and also next time please use
codetags (Hint: The <> off to your right, just highlight all your code that you pasted and click that button) it makes it so much easier to read your code on the forums.
Good luck with your programming and keep with it. It gets a easier and much more fun after you get through the basics.