Greenhorn failing to compile first program.

I've been using Microsoft Visual C++ Express 2008. When I build the following code I get the following error report. I'm confused because I've checked and rechecked that according to the online beginners guide suggested by microsoft this is good code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
use namespace std;

int main()
{
	int length = 5;
	int width = 7;
	int area = length * width;

	cout << "The area of this rectangle is ";
	cout << area;

	return 0;
}


1>------ Rebuild All started: Project: Experiment1, Configuration: Debug Win32 ------
1>Deleting intermediate and output files for project 'Experiment1', configuration 'Debug|Win32'
1>Compiling...
1>stdafx.cpp
1>Compiling...
1>Experiment1.cpp
1>c:\documents and settings\wuzzzintme\my documents\visual studio 2008\projects\experiment1\experiment1\experiment1.cpp(1) : warning C4627: '#include <iostream>': skipped when looking for precompiled header use
1> Add directive to 'stdafx.h' or rebuild precompiled header
1>c:\documents and settings\wuzzzintme\my documents\visual studio 2008\projects\experiment1\experiment1\experiment1.cpp(3) : error C2143: syntax error : missing ';' before 'namespace'
1>c:\documents and settings\wuzzzintme\my documents\visual studio 2008\projects\experiment1\experiment1\experiment1.cpp(3) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\documents and settings\wuzzzintme\my documents\visual studio 2008\projects\experiment1\experiment1\experiment1.cpp(3) : error C2059: syntax error : ';'
1>c:\documents and settings\wuzzzintme\my documents\visual studio 2008\projects\experiment1\experiment1\experiment1.cpp(11) : error C2065: 'cout' : undeclared identifier
1>c:\documents and settings\wuzzzintme\my documents\visual studio 2008\projects\experiment1\experiment1\experiment1.cpp(12) : error C2065: 'cout' : undeclared identifier
1>Build log was saved at "file://c:\Documents and Settings\WuzzzintME\My Documents\Visual Studio 2008\Projects\Experiment1\Experiment1\Debug\BuildLog.htm"
1>Experiment1 - 5 error(s), 1 warning(s)
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========


I was guessing that I was using the compiler wrong and went to the command prompt compiler and found the same errors. Is the book wrong to say this is good code or am I missing something that would be needed for the microsoft compiler?
Last edited on
You have use namespace std; It's using namespace std;
well the code now looks as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
using namespace std;

int main()
{
	int length = 5;
	int width = 7;
	int area = length * width;

	cout << "The area of this rectangle is ";
	cout << area;

	return 0;
}


and the errors are now as follows

1>------ Rebuild All started: Project: Experiment1, Configuration: Debug Win32 ------
1>Deleting intermediate and output files for project 'Experiment1', configuration 'Debug|Win32'
1>Compiling...
1>stdafx.cpp
1>Compiling...
1>Experiment1.cpp
1>c:\documents and settings\wuzzzintme\my documents\visual studio 2008\projects\experiment1\experiment1\experiment1.cpp(1) : warning C4627: '#include <iostream>': skipped when looking for precompiled header use
1> Add directive to 'stdafx.h' or rebuild precompiled header
1>c:\documents and settings\wuzzzintme\my documents\visual studio 2008\projects\experiment1\experiment1\experiment1.cpp(15) : fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source?
1>Build log was saved at "file://c:\Documents and Settings\WuzzzintME\My Documents\Visual Studio 2008\Projects\Experiment1\Experiment1\Debug\BuildLog.htm"
1>Experiment1 - 1 error(s), 1 warning(s)
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========


over all it's a great improvement, thanks for pointing out what I missed!!!
The latest posted code compiles properly using Microsoft Visual Studio. It seems as though your compiler is asking you to include another header;

Add directive to 'stdafx.h' or rebuild precompiled header

I'm not familiar with your compiler, so I don't know if this will work, but perhaps try including that header:

#include <stdafx.h>

well I have modified the code to look like this;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <stdafx.h>
using namespace std;

int main()
{
	int length = 5;
	int width = 7;
	int area = length * width;

	cout << "The area of this rectangle is ";
	cout << area;

	return 0;
}


and get this;

1>------ Rebuild All started: Project: Experiment1, Configuration: Debug Win32 ------
1>Deleting intermediate and output files for project 'Experiment1', configuration 'Debug|Win32'
1>Compiling...
1>stdafx.cpp
1>Compiling...
1>Experiment1.cpp
1>c:\documents and settings\wuzzzintme\my documents\visual studio 2008\projects\experiment1\experiment1\experiment1.cpp(1) : warning C4627: '#include <iostream>': skipped when looking for precompiled header use
1> Add directive to 'stdafx.h' or rebuild precompiled header
1>c:\documents and settings\wuzzzintme\my documents\visual studio 2008\projects\experiment1\experiment1\experiment1.cpp(3) : error C2871: 'std' : a namespace with this name does not exist
1>c:\documents and settings\wuzzzintme\my documents\visual studio 2008\projects\experiment1\experiment1\experiment1.cpp(11) : error C2065: 'cout' : undeclared identifier
1>c:\documents and settings\wuzzzintme\my documents\visual studio 2008\projects\experiment1\experiment1\experiment1.cpp(12) : error C2065: 'cout' : undeclared identifier
1>Build log was saved at "file://c:\Documents and Settings\WuzzzintME\My Documents\Visual Studio 2008\Projects\Experiment1\Experiment1\Debug\BuildLog.htm"
1>Experiment1 - 3 error(s), 1 warning(s)
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========


which still doesn't work. I'm very confused because a few years back when I took a C++ course in high school we didn't use that header file, nor did we use namespaces, could it be that my compiler isn't working right? am I wrong to start a project as a win32 console?
better solution: Create a new project and choose "empty project" from the list so you don't start with that stdafx crap.
thank you very much guys that got it working!
You can also look at the first line, it tells you that you were compiling a

Debug Win32

project. A "Win32" project is a GUI program with fancy windows and buttons and the like. Whenever you start a console project, you must explicitly start a new console application from the menu -- the default is for a GUI application.

Glad you got it working.
Topic archived. No new replies allowed.