1
#include <iostream>
#include <math.h>
#include "stdafx.h"

using namespace std;

int main()
{
    float a, b;
    cout << "Enter The Number: ";
    cin >> a;
    b = sqrt(a);
    cout << "The Square Root of The Number Is: " << b;
    return 0;
}

The Compiler gives the Error:

consoleapplication1.cpp(10): error C2065: 'cout': undeclared identifier  
consoleapplication1.cpp(11): error C2065: 'cin': undeclared identifier
consoleapplication1.cpp(12): error C3861: 'sqrt': identifier not found
consoleapplication1.cpp(13): error C2065: 'cout': undeclared identifier

Please tell me the error and also why do I include "stdafx.h" and why is it in quotation marks?? Using Visual Studio 2015. Level: Beginner

Harshit Joshi
  • 260
  • 4
  • 15
  • 4
    You can read about `stdafx.h` [here](http://stackoverflow.com/questions/4726155/whats-the-use-for-stdafx-h-in-visual-studio) – Rakete1111 Aug 14 '16 at 16:48
  • 5
    TL;DR: move `#include "stdafx.h"` up, so it's the first (non-comment) line in the source file. – Igor Tandetnik Aug 14 '16 at 16:49
  • 2
    I am sure that the errors you show is not the only ones. You should have one before that, something about the precompiled header file `"stdafx.h"` not being included first. – Some programmer dude Aug 14 '16 at 16:50
  • The error you haven't got to is that the code doesn't check whether the input was successful: you should **always** verify that inputs were successful before using read values: `if (std::cin >> a) { ... }`. You should also avoid errors due to parameters being out of range and verify that `a` is non-negative. – Dietmar Kühl Aug 14 '16 at 16:53
  • Don't use precompiled headers or `stdafx.h` unless you have a huge number of include files. Not worth the hassle for small programs. – Thomas Matthews Aug 14 '16 at 18:05
  • You should append `'\n'` to the last line or use `std::endl`. – Thomas Matthews Aug 14 '16 at 18:08
  • Prefer not to use `using namespace std;`. Instead, list the functions and items individually: `using std::cout; using std::cin; using std::endl;`. This way the huge namespace of `std` is not brought into your source file. – Thomas Matthews Aug 14 '16 at 18:10

2 Answers2

3

Simple move #include "stdafx.h" at the top of the file and your code will compile.

stdafx.h contains precompiled header, if you want to remove it you have to disable it in project properties.

Better option is to disable it while creating new project in visual studio

File -> New Project -> Choose you're settings and type OK -> next -> here uncheck "Precompiled header" -> Finish

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • Why would a "better" option be to disable the precompiled header? Wouldn't it make more sense to learn how to use it? It isn't that difficult, and it saves a lot of time. – Cody Gray - on strike Aug 14 '16 at 17:43
  • Precompiled headers only save time if none of the precompiled headers change. The time is only significant if there are a plethora of include files. – Thomas Matthews Aug 14 '16 at 18:06
  • Right. And when you're building, say, a Windows application (as is common with Visual Studio), you're including all of the Windows header files and many of the C or C++ standard library header files, none of which ever change. Yet there are a whole bunch of them; a "plethora" if you will. – Cody Gray - on strike Aug 15 '16 at 14:40
1

Compiler will ignore anything before the #include "stdafx.h" line (when using pre complied headers).

I suggest you to actually use the precompiled header, so move the standard library headers includes to the stdafx.h file.

Niall
  • 30,036
  • 10
  • 99
  • 142
FrozenHeart
  • 19,844
  • 33
  • 126
  • 242