#include <iostream>
using namespace std;
int main() {
bool x = true;
bool y = false;
if(x) {
cout << "if works";
}
if(y==false) {
cout << "else works";
}
int z;
cin >> z;
}
This is a small code that I compiled using Microsoft Visual C++ 2010 Express Edition. When I compile I get a message saying that Your project is out of date.
Why do I get this message ?
If the above code is really out of date, I will be thankful to any suggestion?
You probably hit F5. Which is: run in debug, NOT compile (at least not in C++/VS terms).
It detects that your code is different from the one used to compile your binaries.
If you have this code:
int main()
{
cout << "test";
return 0;
}
And you compile, that's version 1 of your exe.
Then you change the code in
int main()
{
cout << "test1";
cout << "test2";
return 0;
}
Now you hit F5, you are essentially still trying to debug version 1 of your exe since you have not compiled version 2 of your source code into version 2 of your assembly.
That's why it gives you the warning. If I recall correctly you can set a checkbox on that popup to always rebuild. (not sure!)
Projects are out of date dialog will pop up when the time stamp of input files(source code) are newer than output files(binaries).
It has nothing to do with the source code being out of date(If you are thinking in that direction).
Delete all the Debug folder, recompile and then run the project and it should work.
Related
I try to compile this code
#include <iostream>
int main()
{
std::cout << "Hello World" << std::endl;
}
But it outputs
nothing in the terminal
Atom was just broken for me, had to restart my PC and Visual Studio Code started working.
I recently noticed strange behavior in Visual Studio 2017 Community Edition. The output speed of the following VC++ code (made in an "empty project"), when run in or out of VS dramatically increases in speed when the execution reaches the 2nd while loop.
Curious, I ran some benchmarks using std::chrono::system_clock::now(). The execution time of the first while loop took over 7 seconds; The execution time of the second while loop took under 3.
I then ran some more tests, and noticed the strange behavior vanished when I removed both of the std::endl s; They both executed in ~1.5-2 seconds.
What is causing this behavior?
#include <iostream>
int main() {
int i = 0;
while (i <= 9999) {
std::cout << i << std::endl;
i++;
}
while (i <= 19999) {
std::cout << i << std::endl;
i++;
}
}
EDIT: I should have clarified I'm using a release build.
I am new to C++, and now learning it using code blocks (version: codeblocks-16.01mingw-setup.exe). My test codes are as follows:
#include<iostream>
#include<stdlib.h>
int main()
{
int sum = 0, val = 1;
// keep executing the until val is greater than 10
while (val <=10 ) {
sum += val; // short-cut assignment
++val; // add 1 to val
}
std::cout << "Sum of 1 to 10 inclusive is "
<< sum << std::endl;
system("pause");
return 0;
}
These codes are written in an empty file named ex1.cpp. Then I tested by click "Build and run". As a result, another file main.cpp (I did not write this) pops up:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world!" << endl;
return 0;
}
Screenshot attached for your better checking:
The reason why you are getting this error is because your compiler settings is not correct.You need to make sure that you use GNU GCC MinGW Compailer.Go To Settings-->Compiler and make sure every thing is same as on the screen shot.
Solving common codeblocks problems :Link
I really did something wrong about coding:
when I create an empty file in the project, it will result in two main functions in the that project one of which is that "hello world" file automatically generated, which is not allowed by C++.
To build it successfully, what I did is to overwrite the codes in the main.cpp.
I am starting with C++ (Visual Studio 2015 and Windows 8.1), with this simple code:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world" << endl;
return 0;
}
But, the output screen shows nothing!, what shall I do?
Thanks in advance.
In Visual Studio, start the program with Ctrl-F5 and it will run and pause automagically for you. No additional code needed.
Your code is perfectly fine but the program currently only prints and exits right after, because this can happen very fast you might not be able to even see it,try pausing it :
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world" << endl;
cin.get();
return 0;
}
Also, make sure your Anti Virus isn't blocking Visual Studio.
Your code is just fine, however, if you execute it as a cmd program, the program window will close immediately, you might not be able to even see the output. You can write extra code to solve this problem by "pausing" the program:
#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
cout << "Hello world" << endl;
system("PAUSE");
return 0;
}
if you don't like include a windows.h file every time you type, you can add a "cin.get();" in the end of the code. But to be honest, since you are just a beginner, the coolest way I think you should try, is not to use Visual Studio to learn C/C++ but to install CodeBlocks(a simple but effective IDE) to write some codes that are not so long. You know, VS is for huge and complex projects and some practical program developing.
Another solution, platform dependent. My answer is for those of you who just need test pause for debugging purposes. It's not recommended release solution!
windows
#include <iostream>
int main()
{
std::cout << "Hello world" << endl;
system("pause");
return 0;
}
linux (and many alternatives)
#include <iostream>
int main()
{
std::cout << "Hello world" << endl;
system("read -rsp $'Press enter to continue...\n'");
return 0;
}
Detecting paltform
I used to do this on programming homework assignments, ensuring this only happens on windows:
#include <iostream>
int main()
{
std::cout << "Hello world" << endl;
#ifdef _WIN32
system("pause");
return 0;
}
Here's a good cheatsheet for ifdef macros and operating systems: http://sourceforge.net/p/predef/wiki/OperatingSystems/
The program exits on return 0; and window closes. Before this, you must pause the program. E.g you can wait for an input.
Here is a snippet from my code to do this. It works in both windows and linux.
#include <iostream>
using std::cout;
using std::cin;
// Clear and pause methods
#ifdef _WIN32
// For windows
void waitForAnyKey() {
system("pause");
}
#elif __linux__
// For linux
void waitForAnyKey() {
cout << "Press any key to continue...";
system("read -s -N 1"); // Continues when pressed a key like windows
}
#endif
int main() {
cout << "Hello World!\n";
waitForAnyKey();
return 0;
}
I was writing a code for exception handling on Visual C++ 2010 .Here is the code
#include <iostream>
using namespace std;
// Localize a try/catch to a function.
void Xhandler(int test)
{
try{
if(test) throw test;
}
catch(int i) {
cout << "Caught Exception #: " << i << '\n';
}
}
int main()
{
cout << "Start\n";
Xhandler(1);
Xhandler(2);
Xhandler(0);
Xhandler(3);
cout << "End";
return 0;
}
The Program executed properly and the output was the as expected.But when I pressed the close button for closing the console then an error came that cmd has stopped working
.Then I ran my previous code that executed properly ,they also gave the same error
.
Can anybody tell why it is happening?Is it a problem with the Visual c++ 2010 or the code
I think your problem is not with your code. The problem is within your compiler tool chain. You probably are using Qt, and the tool chain has a problem causing this. Google the message you get when you crash with your IDE.
Here's a simple experiment to prove what I'm saying: just run this code:
int main()
{
cout << "Start\n";
cout << "End";
return 0;
}
And your program will crash, which means you have no problems with exceptions or anything else in your code, but with your tool chain.