Need Help on a program.

Im using an online resource to learn C++. This was a program that they had for loops. I am trying to run it but it keeps failing.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "stdafx.h"
#include <iostream>

using namespace System;

int main()
{
  //declaration of variables
int sum, number;
//Initialization of the variables
sum = 0;
number = 1;
// using the while loop to find out the sum of first 1000 integers starting from 1
while(number <= 1000)
{
// Adding the integer to the contents of sum
sum = sum + number;
// Generate the next integer by adding 1 to the integer
number = number + 1;
}
cout << "The sum of first 1000 integers starting from 1 is " << sum;
return 0;
}

and i am getting an error message which is
1>------ Build started: Project: More Training, Configuration: Debug Win32 ------
1> More Training.cpp
1>More Training.cpp(23): error C2065: 'cout' : undeclared identifier
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
please help i don't understand what this error message means.
Thank you very much
cout is in the std namespace not in System
1. Don't use VC++'s Console Project, make an Empty Project instead (it doesn't have that evil "stdafx.h" header)
2. The standard libraries put stuff into the std namespace, not the System namespace.
cout lives in namespace std

Your options are to do one of the following:

1) stick using namesapce std; at the top of your code where right now you have a completely useless using namespace System; which sounds to me like something you'd get in a .NET language of some kind,

2) instead of cout write std::cout

3) stick using std::cout; in your code before you use cout. Again, at the top in place of using namespace System; would work.
Last edited on
Topic archived. No new replies allowed.