Default code in each new cpp files in Visual Studio 2015 - c++

I am currently studying C++ using Visual Studio and I don't want to use a new project for each and every small topic that I go through, instead, I have one project into which I Add and Remove Cpp files as needed.
I wonder if there is a possibility to set a default piece of code into each newly added Cpp file so that I don´t need to copy it every time. Some simple code like this
#include <iostream>
using namespace std;
int main()
{
cin.get()
return 0;
}

Related

Visual studio runs c++ in a white window rather than console

I want it to run just in the cmd but all I get is this white window. Any help?
Code:
//
// pch.cpp
// Include the standard header and generate the precompiled header.
//
#include "pch.h"
#include <iostream>
using namespace std;
int main() {
cout << "test";
return 0;
}
Visual Studio provides different kinds of project you can start with.
You are looking for "Win32 Console Application" which already provides the standard configuration that employs a console.
You can also use an empty C++ project, but then you must find all the required settings that enable the console environment.

cin and cout operator error C++

I am doing an assignment for school and at school we use codeblocks for our IDE, but I wanted to use visual studio at home. The problem is when I run my program on visual studio I continue to get an operator error on my cin and cout but only before a string. I tried #include and while the error lines went away when I build I get the same error before. I am new to C++ and rather confused any help is appreciated.
picture of code below
The #include <string> you tried would be correct.
Your main problem is the #include "stdafx.h". It's a Precompiled header. Visual studio puts it in by default although it isn't required. It has to be the first include if you use it:
Visual C++ will not compile anything before the #include "stdafx.h" in the source file, unless the compile option /Yu'stdafx.h' is unchecked (by default); it assumes all code in the source up to and including that line is already compiled.
Just put it as the first #include (or get rid of it completely) to fix your issue. Then the rest should start working normally.

How do I set up Visual Studio with default headers and comments?

I am fairly new to using Visual Studio. Currently I am using Visual Studio 2015 version. Currently when I set up a new C++ console application project (with Precompiled header off or on) it displays the following:
// Name of project.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
int main()
{
return 0;
}
Every time I create a new project I have to waste my time entering in my header information and comments. I would like it to look like this every time I create a new C++ console application project.
// *********************************************************************
// Name:
// Description:
// Date:
// Class Section:
// Title:
// *********************************************************************
//Heading information
#include <iostream>
#include "stdlib.h"
#include "stdafx.h"
using namespace std;
//Main function
int main()
{
//Pause system and end main function
system("pause");
return 0;
}
Is it possible to set up Visual Studio 2015 to display the code above every time I create a new project? Thanks
Create a new console application.
Edit the file to be as you want it in the new one.
Select "Export Template..." in the "File" menu.
Walk through the wizard (choose a name for your new template, etc.)
Be sure the "Automatically import the template into Visual Studio" is checked.
Restart VS
Select "New Project"
Select "Visual C++" in the tree control on the left
Select your new template in the list in the middle
The usual new-project stuff (name and location for the project, etc.)
[Note: As I'm writing this, I'm looking at it in VS 2013. I don't recall it's changing in VS 2015, but it's possible.]

C++ Unit Test in Visual Studio 2012

I am working with Microsoft Visual Studio 2012 Ultimate to write C++ applications. I got that version from my MSDNAA access. My Problem is that I want to create unit tests for the C++ classes I wrote.
Please Note:
It's standard conform C++, nothing mixed, no C#, it's just C++ that can also be compiled with the g++.
Under file -> new -> project -> Visual C++ exists something like a "managed testproject":
However when I create such a project I cannot manage it to add references e.g. to "MyClass.h" and to compile. And I cannot find a simple tutorial for that.
Can anyone help me by showing how to set up a simple C++ Unit Test with Visual Studio 2012?
You have two choices for C++ unit tests Manage Test Project and Native Unit Test Project. You should select the native one, and then just add the includes you want and write the tests.
Here is a dummy example where I include a "foo.h" header, instantiate a foo and call one of its methods.
#include "stdafx.h"
#include "..\foo.h" // <- my header
#include "CppUnitTest.h"
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace UnitTest1
{
TEST_CLASS(UnitTest1)
{
public:
TEST_METHOD(TestMethod1)
{
foo f;
Assert::AreEqual(f.run(), true);
}
};
}
See Unit testing existing C++ applications with Test Explorer for more.

Visual C++ can't open include file 'iostream'

I am new to C++. I just started! I tried a code on Visual C++ 2010 Express version, but I got the following code error message.
------ Build started: Project: abc, Configuration: Debug Win32 ------
ugo.cpp
c:\users\castle\documents\visual studio 2010\projects\abc\abc\ugo.cpp(3): fatal error C1083: Cannot open include file: 'iostream': No such file or directory
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
This is the code:
// first.cpp -- displays a message
#include <iostream> // A PREPROCESSOR directive
int main(void) // Function header
{ // Start of a function body
using namespace std;
cout << "Come up and C++ me sometime.\n"; // Message
// Start a new line
cout << "Here is the total: 1000.00\n";
cout << "Here we go!\n";
return 0;
}
Replace
#include <iostream.h>
with
using namespace std;
#include <iostream>
Some things that you should check:
Check the include folder in your version of Visual Studio (in "C:\Program Files\Microsoft Visual Studio xx.x\VC\include", check for the file which you are including, iostream, make sure it's there).
Check your projects Include Directories in <Project Name> → Properties → Configuration Properties → VC++ Directories → Include Directories (it should look like this: $(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSdkDir)include;$(FrameworkSDKDir)\include;)
Make sure that you selected the correct project for this code
(menu File → New → Project → Visual C++ → Win32 Console Application)
Make sure that you don't have <iostream.h> anywhere in your code files, Visual Studio doesn't support that (in the same project, check your other code files, .cpp and .h files for <iostream.h> and remove it).
Make sure that you don't have more than one main() function in your project code files (*in the same project, check your other code files, .cpp and .h files for the* main()` function and remove it or replace it with another name).
Some things you could try building with:
Exclude using namespace std; from your main() function and put it after the include directive.
Use std::cout without using namespace std;.
I had this exact same problem in Visual Studio 2015. It looks like as of Visual Studio 2010 and later you need to include #include "stdafx.h" in all your projects.
#include "stdafx.h"
#include <iostream>
using namespace std;
The above worked for me. The below did not:
#include <iostream>
using namespace std;
This also failed:
#include <iostream>
using namespace std;
#include "stdafx.h"
You are more than likely missing $(IncludePath) within Properties → VC++ Directories → Include Directories.
Adding this should make iostream and others visible again. You probably deleted it by mistake while setting up your program.
If your include directories are referenced correctly in the VC++ project property sheet → Configuration Properties → VC++ directories → Include directories, the path is referenced in the macro $(VC_IncludePath).
In my Visual Studio 2015 this evaluates to:
"C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include"
using namespace std;
#include <iostream>
That did it for me.
It is possible that your compiler and the resources installed around it were somehow incomplete. I recommend re-installing your compiler: it should work after that.
I got this error when I created an 'Empty' console application in Visual Studio 2015. I recreated the application, leaving the 'Empty' box unchecked. It added all of the necessary libraries.
Make sure you have Desktop Development with C++ installed.
I was experiencing the same problem, because I only had Universal Windows Platform Development installed.
Microsoft Visual Studio is funny. When you're using the installer, you must checkbox a lot of options to bypass the .NET framework (somewhat) to make more C++ instead of C# applications, such as the CLR options under desktop development... in the Visual Studio installer.... the difference is the C++ Win32 console project or a C++ CLR console project.
So what’s the difference? Well, I'm not going to list all of the files CLR includes, but since most good C++ kernels are in Linux... So CLR allows you to bypass a lot of the Windows .NET framework because Visual Studio was really meant for you to make applications in C#.
Here’s a C++ Win32 console project!
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
cout << "Hello, World!" << endl;
return 0;
}
Now here’s a C++ CLR console project!
#include "stdafx.h"
using namespace System;
int main(array<System::String ^> ^args)
{
Console::WriteLine("Hello, World!");
return 0;
}
Both programs do the same thing .... the CLR just looks more frameworked class overloading methodology, so Microsoft can great its own vast library you should familiarize yourself with if so inclined.
Keywords (C++)
Other things you'll learn from debugging to add for error avoidance:
#ifdef _MRC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
If you created an environment variable with the name IncludePath, try renaming it to something else.
This name will override $(IncludePath) inside project properties.
Quick fix for small programs:
Add: #include <cstdlib>
In my case, my Visual Studio 2015 installed without selecting C++ package, and Visual Studio 2017 is installed with the C++ package. If I use Visual Studio 2015, opening a C++ project will show this error, and using Visual Studio 2017 will be no error.
I had this problem too. I used this code (before main();) in Visual Studio 2022, and it turned OK:
#include "pch.h"
#include <iostream>
using namespace std;
using namespace winrt;
using namespace Windows::Foundation;
In my case, the error occurred when I created a file in VS Code, without giving the .cpp extension. It resolved when I renamed it with the .cpp.
// first.cpp -- displays a message
#include <iostream> // a PREPROCESSOR directive
using namesapce std;
int main() // function header
{ // start of a function body
///using namespace std;
cout << "Come up and C++ me sometime.\n"; // message
// start a new line
cout << "Here is the total: 1000.00\n";
cout << "Here we go!\n";
return 0;
}