3 Replies - 4086 Views - Last Post: 09 March 2010 - 05:46 AM Rate Topic: -----

#1 Keevu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 13-September 09

Cout is an undeclared identifier error.

Posted 09 March 2010 - 05:32 AM

I am going through a tutorial in a book. At the end of the tutorial they give you some example source code. I copied the source code down word for word. Since I am using the visual c++ 2008 express edition compiler I added #include "stdafx.h".

When I try to compile the program I get the error Cout is an undeclared identifier. Anyone know how I can fix this?

I tried creating a new project without the precompileed header option checked but it still isnt working.

source
// Magic Number Game.cpp : Defines the entry point for the console application.
//

#include <iostream>
#include <cstdlib>
#include "stdafx.h"


int _tmain(int argc, _TCHAR* argv[])
{
	int magic;
	int guess;

	magic = rand();

	cout << "Enter your guess: ";
	cin >> guess;

	if (guess == magic) {
		cout << "** Right **\n";
		cout << magic << " is the right number.\n";
	}
	else {
		cout << "...Sorry, you're wrong.";

		//use  a nesed if statement
		if(guess > magic)
			cout <<" You're guess is too high.\n";
		else
			cout << " You're guess is too low.\n";
	}

	return 0;

}






errors
1>------ Build started: Project: magic number game 2, Configuration: Debug Win32 ------
1>Compiling...
1>stdafx.cpp
1>magic number game 2.cpp
1>(16) : error C2065: 'cout' : undeclared identifier
1>(17) : error C2065: 'cin' : undeclared identifier
1>(20) : error C2065: 'cout' : undeclared identifier
1>(21) : error C2065: 'cout' : undeclared identifier
1>(24) : error C2065: 'cout' : undeclared identifier
1>(28) : error C2065: 'cout' : undeclared identifier
1>(30) : error C2065: 'cout' : undeclared identifier
1>Generating Code...
1>magic number game 2 - 7 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Is This A Good Question/Topic? 0
  • +

Replies To: Cout is an undeclared identifier error.

#2 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Cout is an undeclared identifier error.

Posted 09 March 2010 - 05:43 AM

cout, cin, endl ... etc, are defined in a namespace. This namespace is called "std". So, how would you access the elements of a namespace called "std"? You put the "std::" prefix in front of every identifier. So: "cout" becomes "std::cout", etc ... To get rid of this prefix, use:
// Introduce the entire namespace in your program.
using namespace std;


Add this line right under the last #include.
If you don't want to include the entire namespace, you could only import the needed functions:
using std::endl;
using std::cout;
using std::cin;


This would be much more efficient. By the way, I consider "using namespace std;" a bad practice. Don't know why though ...

This post has been edited by sarmanu: 09 March 2010 - 05:46 AM

Was This Post Helpful? 1
  • +
  • -

#3 Keevu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 13-September 09

Re: Cout is an undeclared identifier error.

Posted 09 March 2010 - 05:44 AM

View Postsarmanu, on 09 March 2010 - 04:43 AM, said:

cout, cin, endl ... etc, are defined in a namespace. This namespace is called "std". So, how would you access the elements of a namespace called "std"? You put the "std::" prefix in front of every identifier. So: "cout" becomes "std::cout", etc ... To get rid of this prefix, use:
using namespace std;


Add this line right under the last #include.


Thanks that fixed it. I feel embarrassed now haha.
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6062
  • View blog
  • Posts: 23,514
  • Joined: 23-August 08

Re: Cout is an undeclared identifier error.

Posted 09 March 2010 - 05:46 AM

The standard C++ library "lives" in a namespace called std. You need to explicitly tell the compiler in what namespace the cout and cin identifiers are, so you can do that once of three ways.

The first to to prepend the namespace to each function identifier, like this:

std::cout << "Hello World" << std::endl;


OR, in your C++ file you can tell the compiler your using these specific identifiers:

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int main()
{
    cout << "Hello World" << endl;
    return 0;
}



OR you can just pull in the entire namespace itself:
#include <iostream>

using namespace std;

int main()
{
    cout << "Hello World" << endl;
    return 0;
}


EDIT: Ninja'd
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1