Microsoft visual studio 2012 c++ - c++

I am learning c++ from scratch and I was trying to make hello world program with this code:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World" << endl;
return 0;
}
but I always end with "this project is out of date"
and when I try to build it I have this error message:
unable to start program 'c:\users\User\documents\visualstudio2012\projects\Consoleapplication3\Debug\ConsoleApplication3.exe'.
the system cannot find the file specified

Your program should fail if you do not include your default #include <iostream>, which implies that your code should be something like this:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World" << endl;
return 0;
}
These said, to fix your problem, look in the solution explorer box to the left. Make sure that there is actually a .cpp file there. You can do the same by looking the .cpp file where the .sln file for the project is stored. If there is not one, then you will get that error.
When adding a cpp file you want to use the "add new item" icon. (top left with a gold star on it, hover over it to see the name) For some reason Ctrl+N does not actually add a .cpp file to the project.
Sources : System cannot find specified file

Related

Console doesn't show output from vector

I have a weird problem when trying to print out the content of a vector.
I'm using Visual Studio Code with the CMake extension.
I can print out simple text using cout
#include <iostream>
#include <vector>
using namespace std;
int main() {
cout << "test" << endl;
return 0;
}
But I can't print out the vectors content
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> test = {1,2,3};
cout << "test" << endl;
cout << test[1] << endl;
return 0;
}
I've never really worked with C++ vectors, so I'm probably missing something fairly obvious but I followed a C++ vector tutorial step by step and for them, the output works fine.
Cheers,
Luca
I'm new to C++ and have exactly the same problem.
Temporary workaround as posted by Luckylone here
would be to copy libstdc++-6.dll from your mingw64\bin folder into your project folder.
Something is messing up our link with the resources even though folders are properly added to system PATH.
EDIT: After an afternoon of troubleshooting, I've solved my problem by uninstalling compilers (originally provided through WinLibs) and reinstalling them using MSYS2.
Delete the existing Mingw64 folder, remove the PATH variables, then carefully follow these instructions. Keep in mind that I had to add mingw64/bin to both user and system Path, and restart VS Code before damn std::vector finally started to print.

When the program built with C::B doesn't work

I made a small c++ project and it was fully compiled and built it.
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world!" << endl;
return 0;
}
When I run through c::b it was okay
However, it says next warnings when I run program outside of the c::b.
"programdir/bin/Debug/program.exe"
So I got "libgcc_s_dw2-1.dll" from sourceforge, pasted it next to my .exe file,
and I got 0xc000007b error with the error "cannot start the program"
What should I do in this case of problem?

Visual Studio 2017 "Unable to start program. The system cannot find the file specified"

I'm following this tutorial to create a C++ Hello World app for Visual Studio.
https://tutorials.visualstudio.com/cpp-console/install
I've installed the software, selected "Windows Console Application" and copy/pasted the Hello World program from the tutorial:
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
cout << "Hello, world!\n";
return 0;
}
However, when I try to run the Local Windows Debugger, I get this error:
Unable to open program.
\repos\HelloWorld\Debug\HelloWorld.exe
The system cannot find the file specified.
How would I include an .exe file?
Also, I have an error "cannot open stdafx.h", but I assume that's connected to this.
EDIT:
I removed "#include "stdafx.h"" and got the same error. It also said there was an unexpected end of file and suggested I #include "pch.h", so I did.
#include <iostream>
#include "pch.h"
using namespace std;
int main()
{
cout << "Hello, world!\n";
return 0;
}
I'm still getting the error that it can't find .exe ...also 'cout' is an undeclared identifier.

Header file is not being read?

So here is my code:
Main
#include<iostream>
#include "Tclass.h"
#include "Tclass.cpp"
using namespace std;
int main(){
int x = 7;
Tclass object;
cout << object.OFunction();
return 0;
}
Header file
#ifndef TCLASS_H
#define TCLASS_H
class Tclass
{
public:
Tclass();
int OFunction();
};
#endif // TCLASS_H
C++ style sheet(i believe that is what it is called)
#include "Tclass.h"
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
Tclass::Tclass()
{
cout << "An object for this class has been created \n";
}
int Tclass::OFunction(){
srand(time(0));
return 1+(rand()%6);
}
My issue is that i heard that i didn't need to include the #include"Tclass.cpp" in my code but without it my code won't run. Also i remove the the header file from the main it still works. I trust this person to be correct but i simply can not get this to work. The IDE i am using is code::blocks.
You're not supposed to include the cpp file (and it's not called a style sheet, it's a source file) You compile main.cpp and Tclass.cpp separately into object files and then link the object file into one binary.
Oh and don't use using namespace std, don't copy code from text books, code in text books is very different from actual production code, code in text books is meant to be terse and only show the specific topic at hand.
Correct, you should not include the .cpp files in your main program. Instead, you should ADD the file to the project (I haven't used Code::Blocks for a long time, but I'm sure there is a pretty obvious "Add file to project" option somewhere in the menus - either "File" or "Project" would be the first places to look).
[And no, it's called "Source file" - a style sheet is something you use in web-development]

error C2065: 'cout' : undeclared identifier

I am working on the 'driver' part of my programing assignment and i keep getting this absurd error:
error C2065: 'cout' : undeclared identifier
I have even tried using the std::cout but I get another error that says:
IntelliSense: namespace "std" has no member "cout"
When I have declared using namespace std, included iostream and I even tried to use ostream
#include <iostream>
using namespace std;
int main () {
cout << "hey" << endl;
return 0;
}
I'm using Visual Studio 2010 and running Windows 7. All of the .h files have using namespace std and include iostream and ostream.
In Visual Studio you must #include "stdafx.h" and be the first include of the cpp file. For instance:
These will not work.
#include <iostream>
using namespace std;
int main () {
cout << "hey" << endl;
return 0;
}
#include <iostream>
#include "stdafx.h"
using namespace std;
int main () {
cout << "hey" << endl;
return 0;
}
This will do.
#include "stdafx.h"
#include <iostream>
using namespace std;
int main () {
cout << "hey" << endl;
return 0;
}
Here is a great answer on what the stdafx.h header does.
write this code, it works perfectly..
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
cout<<"Hello World!";
return 0;
}
I had same problem on Visual Studio C++ 2010. It's easy to fix. Above the main() function just replace the standard include lines with this below but with the pound symbol in front of the includes.
# include "stdafx.h"
# include <iostream>
using namespace std;
The include "stdafx.h" is ok
But you can't use cout unless you have included using namespace std
If you have not included namespace std you have to write std::cout instead of simple cout
If the only file you include is iostream and it still says undefined, then maybe iostream doesn't contain what it's supposed to. Is it possible that you have an empty file coincidentally named "iostream" in your project?
I have seen that if you use
#include <iostream.h>
then you will get the problem.
If you use
#include <iostream>
(notice - without the .h)
then you will not get the problem you mentioned.
If you started a project requiring the #include "stdafx.h" line, put it first.
I've seen similar things happen when I was using the .c file extension with C++ code. Other than that, I'd have to agree with everyone about a buggy installation. Does it work if you try to compile the project with an earlier release of VS? Try VC++ Express 2008. Its free on msdn.
Such a silly solution in my case:
// Example a
#include <iostream>
#include "stdafx.h"
The above was odered as per example a, when I changed it to resemble example b below...
// Example b
#include "stdafx.h"
#include <iostream>
My code compiled like a charm. Try it, guaranteed to work.
The code below compiles and runs properly for me using gcc. Try copy/pasting this and see if it works.
#include <iostream>
using namespace std;
int bob (int a) { cout << "hey" << endl; return 0; };
int main () {
int a = 1;
bob(a);
return 0;
}
I have VS2010, Beta 1 and Beta 2 (one on my work machine and one at home), and I've used std plenty without issues. Try typing:
std::
And see if Intellisense gives you anything. If it gives you the usual stuff (abort, abs, acos, etc.), except for cout, well then, that is quite a puzzler. Definitely look into your C++ headers in that case.
Beyond that, I would just add to make sure you're running a regular, empty project (not CLR, where Intellisense is crippled), and that you've actually attempted to build the project at least once. As I mentioned in a comment, VS2010 parses files once you've added an include; it could be that something stuck the parser and it didn't "find" cout right away. (In which case, try restarting VS maybe?)
Take the code
#include <iostream>
using namespace std;
out of your .cpp file, create a header file and put this in the .h file. Then add
#include "whatever your header file is named.h"
at the top of your .cpp code. Then run it again.
I had the same issue when starting a ms c++ 2010 project from scratch - I removed all of the header files generated by ms and but used:
#include "stdafx.h"
#include <iostream>
using namespace std;
int main() {
cout << "hey" << endl;
return 0;
}
I had to include stdafx.h as it caused an error not having it in.
Try it, it will work. I checked it in Windows XP, Visual Studio 2010 Express.
#include "stdafx.h"
#include <iostream>
using namespace std;
void main( )
{
int i = 0;
cout << "Enter a number: ";
cin >> i;
}
before you begin this program get rid of all the code and do a simple hello world inside of main. Only include iostream and using namespace std;.
Little by little add to it to find your issue.
cout << "hi" << endl;
Are you sure it's compiling as C++? Check your file name (it should end in .cpp). Check your project settings.
There's simply nothing wrong with your program, and cout is in namespace std. Your installation of VS 2010 Beta 2 is defective, and I don't think it's just your installation.
I don't think VS 2010 is ready for C++ yet. The standard "Hello, World" program didn't work on Beta 1. I just tried creating a test Win32 console application, and the generated test.cpp file didn't have a main() function.
I've got a really, really bad feeling about VS 2010.
When you created your project, you did not set 'use precompiled headers' correctly. Change it in properties->C/C++->precompiled headers.
In Visual studio use all your header filer below "stdafx.h".
Just use printf!
Include stdio.h in your stdafx.h header file for printf.
Include the std library by inserting the following line at the top of your code:
using namespace std;
is normally stored in the C:\Program Files\Microsoft Visual Studio 8\VC\include folder. First check if it is still there. Then choose Tools + Options, Projects and Solutions, VC++ Directories, choose "Include files" in the "Show Directories for" combobox and double-check that $(VCInstallDir)include is on top of the list.
I ran across this error after just having installed vs 2010 and just trying to get a nearly identical program to work.
I've done vanilla C coding on unix-style boxes before, decided I'd play with this a bit myself.
The first program I tried was:
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
cout << "Hello World!";
return 0;
}
The big thing to notice here... if you've EVER done any C coding,
int _tmain(int argc, _TCHAR* argv[])
Looks weird. it should be:
int main( int argc, char ** argv )
In my case I just changed the program to:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world from VS 2010!\n";
return 0;
}
And it worked fine.
Note: Use CTRL + F5 so that the console window sticks around so you can see the results.
I came here because I had the same problem, but when I did #include "stdafx.h" it said it did not find that file.
What did the trick for me was: #include <algorithm>.
I use Microsoft Visual Studio 2008.
These are the things that you can use then, incl. 'count': Link
Had this problem, when header files declared "using namespace std;", seems to be confusing for GNU compiler;
anyway is bad style!
Solution was providing std::cout ... in headers and moving "using namespace std" to the implementation file.
In VS2017, stdafx.h seems to be replaced by pch.h see this article,
so use:
#include "pch.h"
#include <iostream>
using namespace std;
int main() {
cout << "Enter 2 numbers:" << endl;
It was the compiler - I'm now using Eclipse Galileo and the program works like a wonder