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.
Related
enter image description here
1.After executing a.exe is not showing output in visual studio terminal and also in powershell admin mode.It is also not giving any error.
but when running hello world program it gives output.
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> g1;
for (int i = 1; i <= 5; i++)
g1.push_back(i);
cout << "Output of begin and end: ";
for (auto i = g1.begin(); i != g1.end(); ++i)
cout << *i << " ";
return 0;
}
Your code is showing but it just exits out so fast you can't tell what it returned. Advise checking this out: How to stop C++ console application from exiting immediately?
Have you set the program to the console program? Property -> Linker -> System -> Subsystem -> "Console(/SUBSYSTEM:CONSOLE)"
I've recently tried cpp, in the thing I'm making I'm trying to make it so that a variable with the value of 20 is subtracted by 1 every second, but I also need the machine to be waiting for an input from the user. I tried using for loops but they won't proceed until the input is placed or until the variable runs out. I looked at clock but they don't seem to fit my need, or maybe I just misunderstood their purpose.
Any suggestions?
As has already been suggested in the comments, threading is one way to do this. There is a nice self-contained example here (which I've borrowed from in the code below).
In the code below an asynchronous function is launched. Details on these here. This returns a future object which will contain the result once the job has finished.
In this case the job is listening to cin (typically the terminal input) and will return when some data is entered (i.e. when enter is pressed).
In the meantime the while loop will be running which keeps a track of how much time has passed, decrements the counter, and also returns if the asynchronous job finishes. It wasn't clear from your question if this is exactly the behaviour you want but it gives you the idea. It will print out value of decremented variable, but user can enter text, and it will print that out once user presses enter.
#include <iostream>
#include <thread>
#include <future>
#include <time.h>
int main() {
// Enable standard literals as 2s and ""s.
using namespace std::literals;
// Execute lambda asyncronously (waiting for user input)
auto f = std::async(std::launch::async, [] {
auto s = ""s;
if (std::cin >> s) return s;
});
// Continue execution in main thread, run countdown and timer:
int countdown = 20;
int countdownPrev = 0;
std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();
std::chrono::steady_clock::time_point end;
double elapsed;
while((f.wait_for(5ms) != std::future_status::ready) && countdown >= 0) {
end = std::chrono::steady_clock::now();
elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - begin).count();
countdown = 20 - (int) (elapsed/1000);
if (countdown != countdownPrev) {
std::cout << "Counter now: " << std::fixed << countdown << std::endl;
countdownPrev = countdown;
}
}
if (countdown == -1) {
std::cout << "Countdown elapsed" << std::endl;
return -1;
} else {
std::cout << "Input was: " << f.get() << std::endl;
return 0;
}
}
P.S. to get this to work on my compiler I have to compile it with g++ -pthread -std=c++14 file_name.cpp to correctly link the threading library and allow use of c++14 features.
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 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.
#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.