-2

im having trouble running my console app with the following format.I've looked through dozens of websites and stack overflow questions and non of them were useful.please help me figure this out:

#include "stdafx.h"
#include <iostream
using namespace std;

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


}
  1. cannot open source file "stdafx.h"

  2. identifier "_TCHAR" is undefine.

  3. it's a console project and it does'nt create those stdafx.h and stdafx.cpp files.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Alin
  • 85
  • 1
  • 8
  • Does this answer your question? [What is the difference between \_tmain() and main() in C++?](https://stackoverflow.com/questions/895827/what-is-the-difference-between-tmain-and-main-in-c) – Krishna Kanth Yenumula Dec 22 '20 at 14:31
  • 1
    You need to `#include `. If you create a console project with the standard Visual Studio template, it will compile just fine. – Cody Gray - on strike Dec 22 '20 at 14:32
  • @CodyGray I cannot believe how bad that advice is. Have you seen the hundreds of posts on this site by newbies who get into trouble with bizarre mixes of char, WCHAR, TCHAR etc.If you what to advise using non-standard C++ then at least advise to use WCHAR. – john Dec 22 '20 at 14:40
  • Hundreds of newbies make programming errors writing C++ code, too. Should we start recommending that they all write in BASIC instead? That's a weird argument. They have a code snippet, which they want to make compile, but are getting an error. The way to fix that error is to include the appropriate header file. Telling them to do something completely different is also viable, but somewhat out of scope, by my estimate. – Cody Gray - on strike Dec 22 '20 at 14:44
  • 1
    I get the feeling that `#include ` is missing. Also, `#include `. – Ted Lyngmo Dec 22 '20 at 14:57
  • `stdafx.h` is for precompiled headers. In modern versions of Visual Studio `pch.h` is used instead. – drescherjm Dec 22 '20 at 15:20

2 Answers2

0

I have found the answer to this problem on my own. from the beginning the problem was the version of visual studio . so apart from the unreadable and unclear form of debugging the vs2012 does,the thing is in vs2012 this form of "int _tmain(int argc, _TCHAR* argv[])" is accepted but in vs2019 this is completely prohibited so as "#include "stdafx.h". So the key is to know what form you should use for c++ programming in different Visual Studio versions.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Alin
  • 85
  • 1
  • 8
-3

Replace _TCHAR with char, and _tmain with main.

_TCHAR is not part of the C++ language, it's an obsolete feature of the Windows API that should not be used today.

You can also remove #include "stdafx.h", it's a common convention when using the Microsoft Visual Studio compiler but it's not required. You may also need to turn off the precompiled headers option in your project.

john
  • 85,011
  • 4
  • 57
  • 81
  • 1
    On Windows, you should be using `wchar_t`, not `char`. – Cody Gray - on strike Dec 22 '20 at 14:31
  • @CodyGray Or, if you are using the Windows SDK, the platform type `WCHAR` – Govind Parmar Dec 22 '20 at 14:32
  • @CodyGray If you want to use standard C++ then you should use `char`. – john Dec 22 '20 at 14:32
  • 2
    TCHAR is not obsolete. In Windows, you should always be using wchar_t, not char. Turning off precompiled headers is a very bad idea, nowadays anything bigger than a hello world will benefit. – Michael Chourdakis Dec 22 '20 at 14:33
  • If `TCHAR` is not obsolete then why do you say 'You should always be using wchar_t'? The only standard C++ entry points are` `int main()` and `int main(int, char**)` using anything else is non-standard. I haven't compiled with precompiled headers for years but that's a decision the OP can make when they understand what it means. For simpicity I would advise turning them off. – john Dec 22 '20 at 14:36
  • @MichaelChourdakis Re 'TCHAR is not obsolete' see discussion at https://stackoverflow.com/questions/234365/is-tchar-still-relevant – Govind Parmar Dec 22 '20 at 14:40
  • 2
    Why do you operate under the assumption that the asker is intending to write "standard" C++? The question is fairly clear that they're attempting to develop a Windows console application. – Cody Gray - on strike Dec 22 '20 at 14:45
  • 1
    The phrase 'windows console application' appears nowhere in the post (only 'console application'). It's a reasonable working assumption, unless you have a particular need for Windows specific code (which is unlikely in a total beginner) it's better to stick to standard C++. – john Dec 22 '20 at 14:52