Return to Level1Techs.com

Cout, endl, and cin undeclared identifier error


#1

I'm using Visual Studio 2017 and got the error undeclared identifier for cout, cin, and endl. After looking through the internet a bit I couldn't get it to work so here I am. Heres the code

#include <iostream>
#include "stdafx.h"
using namespace std;
int main()
{
	int number;
	bool even;
	int result;
	int number_remainder;
	int calculationAmount = 0;
    cout << "Enter a number." << endl;
cin >> number;

number_remainder = number % 2;

if(number_remainder == 0){
	even = true;
}
else {
	even = false;
}

while (number != 1) {

	number_remainder = number % 2;

	if (number_remainder == 0) {
		even = true;
	}
	else {
		even = false;
	}


	if (even == true) {
		number /= 2;
		
	}
	if (even == false) {
		number *= 3;
		number += 1;
	}

	calculationAmount += 1;
}

cout << "The amount of calculations it took to get to 1 is " << calculationAmount << endl;

return 0;
}

EDIT: everything is indented properly, not sure why its showing the indents wrong there


#2

Not sure about ms compilers, butusing namespace is somewhat frowned upon in large codebases

std::cout would be what I'd use even with gcc/clang

edit: also, decide on your curly brace / new line style, and declare your variables closer to point of use , ideally in the same line


#3

I've tried that, but to no avail, I get the error cout/cin/endl is not a member of std


#4

Can you get rid of stdafx?


#5

The code compiles fine with GCC. Only change I made was to remove the inclusion of "stdafx.h" as that appears to be a Microsoft thing.

It is possible that said header #undefs stdin/stdout. Try removing the header if you don't need it or import "stdafx" before "iostream".


#6

putting stdafx before iostream worked, thanks


#7

Make sure to add a comment which explains that/why this has to be done. Some IDEs/people like to clean up #includes and may be very much surprised to find the code no longer working. May save you a headache later on :smiley:


#8

thanks for the idea, will do


#9

Edit: Look at this answer. https://stackoverflow.com/a/4726838
Seems weird, but maybe you need to disable precompiled headers?


Sounds like a precompiler error.

I don't use visual studio, but is there somewhere you configure the precompiler? It could be that the system libraries path is set up wrong, or that it doesn't precompile at all.

Make a new file and just put in the below and see if it compiles. If it doesn't, you should look at Visual Studio's precompiler settings. Maybe it has a predefined header file?

#define FOO 1

int main()
{
    int table[FOO];
    return 0;
}

#10

If that was the case the preprocessor would make that clear, with a message like

ct.cpp:1:10: fatal error: foobar: No such file or directory
#include <foobar>

Also your code example doesn't actually test the system header path :confused:

Anyway, no need to speculate as the problem has been solved already.


#11

I find this solution interesting as I had this issue on a test in my C++ 2.

I use GCC normally, but for the exam were were able to take the test in the computer lab and they were Win7 boxes.

I could not get my program to compile to dave my life and I re rote it 3 times with the same issue. I wonder if it was a mater of this issue as the error was bery similar.

The instructor could not figure out what was wrong and just graded it by hand. I got a 98 on it because she did not like my commenting style.

Is it common to have these issues with the MS compiler where one include statement may interfere with another? It seems GCC handles this with no issue.


#12

The issue is not directly related to the MS compiler. The error only occurred under Visual Studio, because it tries to be helpful by creating that weird header in the first place, but using the same header under GCC would probably result in similar problems. (Though I wouldn't trust that GCC would actually be able to compile the header, as it likely containts MS-specific constructs.)

Conflicts between headers are not common but do happen. I've had issues with SDL, assertions and CPython in the past. In fact the CPython documentation even states that it's header needs to be included first:

Since Python may define some pre-processor definitions which affect the standard headers on some systems, you must include Python.h before any standard headers are included.

So this is not an issue of whether a compiler can resolve conflicts. The C++ standard clearly states how the preprocessor has to behave. It just so happens that one can write stupid header files :smiley:


#13

I have run into this issue as well in the past, before building the solution, have you tried cleaning the solution? (in the same menu)
It fixed some code for me a few times but in some cases had to cut my loses and just make a new project and paste it into a new source file.

Also your "stdafx.h" may also need
#include<iostream>
using namespace std;


#14

I'm sorry for the late answer, the problem is solved but maybe it will make the issue clear for someone.
Actually "stdafx.h" (Standard Application Framework eXtensions*) is a header file needed for precompiled headers in VS, it helps to optimize compile time when you use all the MS stuff (for ex. win API) since it doesn't compile all included files every time you build your project, but for simple console applications it is not needed (compile time is pretty short anyway) so you can disable "precompiled headers" option in your Project Properties. When you use "empty project" at start the option is probably (I'm not sure) disabled by default.

*Standard Application Framework eXtensions - early (old) name for MFC (Microsoft Foundation Class Library)


#15

Your compiler is not explicitly able to resolve cout and endl. cout is ofstream and you have got the correct header . Still this resolves std and cout is part of std. endl is a manipulator part of std. Looks like from this compiler you have to use std to use cout. ie

std::cout << “String to display” << std::endl;

That should work!