-4

I have a problem about my c++ code. When ı write in my code include "stdafx.h" ı have errors. Why is the reason? I have 2 pics to better explain my problem. Can anyone help me ?

https://i.stack.imgur.com/XylsB.jpg

https://i.stack.imgur.com/zhgkr.jpg

There is my code:

#include <iostream>
#include "stdafx.h"
using namespace std;
int main()
{
int x, y,toplam=0;

cout << "1. Sayiyi Giriniz:";
cin >> x;

cout << "2. Sayiyi Giriniz:";
cin >> y;

toplam = x + y;

cout << "Sayilarin toplami:" << toplam << endl;
system("PAUSE");
return 0;
}

Error C1083 Unable to open include file: 'stdafx.h': No such file or directory

Error (active) E1696 Source file "stdafx.h" cant open

root_roxox
  • 179
  • 1
  • 9
  • There is no `stdafx.h` in your project. – tkausl Aug 02 '19 at 18:01
  • 3
    stdafx.h is a Microsoft thing. You don't need it. Just leave out the `#include "stdafx.h"`. – Pete Becker Aug 02 '19 at 18:05
  • 1
    Note that IF you were using Precompiled Headers through ``stdafx.h``, then the above code would behave strangely because it would never include ````. Basically the compiler skips everything up to the pch header, so you'd have to make it: ``#include "stdafx.h"`` and then ``#include `` ` – Chuck Walbourn Aug 02 '19 at 18:12
  • 1
    If you do want to use precompiled headers, it needs to be the first thing in your file. I've heard that anything before that is ignored. Also, the default name has changed to `pch.h`. – eesiraed Aug 02 '19 at 18:13
  • You may want to read [Microsoft Docs](https://learn.microsoft.com/en-us/cpp/build/creating-precompiled-header-files?view=vs-2019) to learn more about them... – Chuck Walbourn Aug 02 '19 at 18:14
  • Thanks for help mate. But ıf ı want to use stdafx.h how can ı use it what ı should to do? – root_roxox Aug 02 '19 at 18:29
  • This is a classic XY question. There's something you want to do. You think you can accomplish it by including "stdafx.h". But that doesn't work. So you ask us to fix it. But you never told us what it was you wanted to do. So that makes it very hard for us to help you. *Never* ask about the way you think a problem should be solved without first making sure you clearly explain what the actual problem is. See [this page](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) for more information about how to avoid this. – David Schwartz Aug 02 '19 at 18:51
  • You can try to use `#include "pch."`. If it works your IDE is too new. You can download and install an older version of visual studio: https://learn.microsoft.com/en-us/cpp/build/creating-precompiled-header-files?view=vs-2019 – Thomas Sablik Aug 02 '19 at 23:50

2 Answers2

2

Microsoft Visual Studio and its C++ compiler by default creates projects that use something called precompiled headers. Earlier versions used to name the header file for that "stdafx.h".

The file have changed name in the latest versions, and precompiled headers in general are not commonly used for other environments.

If the compiler says that it cannot find it, then just remove the #include directive.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
1

Your project does not have stdafx.h - its added by visual studio. Remove it from source code.

Then go to project settings -> c++ -> precompiled headers then select Not using precompiled headers from the drop down list.

Deb S
  • 509
  • 5
  • 16