0

Firstly I wanted to create a program that would count the time from the user input time to zero, this was the code -

#include <iostream>
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <dos.h>
#include <windows.h>
#include <thread>
using namespace std;

//  sleep(5000);
int runTimer = 1;
int seconds;
int hoursLeft;
int minutesLeft;
int secondsCount=0;
void timeLeft ()
{
    hoursLeft = seconds/3600;
    minutesLeft = seconds/60 - hoursLeft*60;
}
void timer ()
{
    while(runTimer=1)
    {
         if (secondsCount == 60)
    {
    timeLeft();
    cout << "The Amount of time left is: " << hoursLeft << " hours and " << minutesLeft << " minutes left." << endl;
    secondsCount=0;
    }
    secondsCount++;
    seconds--;
    Sleep(1000);
    timer();
    }

}
int main()
{
    // introduction and time picking
    cout << "Welcome to my Timer - Please set the amount of hours and than minutes you want the timer to run" << endl;
    double requestedHours, requestedMinutes;
    cin >> requestedHours;
    cin >> requestedMinutes;
    double requestedSeconds = requestedHours*3600 + requestedMinutes*60;
    seconds = requestedSeconds;
    cout << "Timer Started";
    timer();
}

But than I wanted to add a function in which the user could type a word or a letter to pause the program, (and after a bit of reearching I found out about threading) - but when I added #include <thread> I got this massage -

"#error This file requires compiler and library support for the \
ISO C++ 2011 standard. This support is currently experimental, and must be \
enabled with the -std=c++11 or -std=gnu++11 compiler options.
#endif" 

How to fix this?

mazhar islam
  • 5,561
  • 3
  • 20
  • 41
Tomer8009
  • 189
  • 2
  • 3
  • 11
  • 7
    Your error message is giving you the solution... – Eric Martinez Jun 15 '15 at 03:50
  • 2
    You installed a CodeBlocks that supports an older version of C++. To upgrade, Google "codeblocks C++ 11". For example: [StackOverflow: How can I add C++11 support to code::blocks compiler?](http://stackoverflow.com/questions/18174988/how-can-i-add-c11-support-to-codeblocks-compiler) or [YouTube: How to add C++11 support to code::blocks compiler](https://www.youtube.com/watch?v=SNLZEhWZ1og) – paulsm4 Jun 15 '15 at 03:51
  • @paulsm4 CodeBlocks is not a compiler, and if this message occurs in `libstdc++`, he is using a version of `gcc` that *does* support C++11 since they are shipped together. – o11c Jun 15 '15 at 03:55
  • @o11c: I *know* Codeblocks is an IDE (I much prefer Eclipse/CDT, thank you). The point is, he needs to Google for an update procedure *specific to Codeblocks* . Which may or may not differ from a procedure to update Eclipse, or update MinGW ... or, for that matter, update a command-line gcc/g++ toolchain. – paulsm4 Jun 15 '15 at 03:59
  • 1
    @paulsm4 Except that absolutely nothing needs to be upgraded. Only a project setting needs to be changed. – o11c Jun 15 '15 at 04:00
  • Separate from the thread aspect, your code reads from `int seconds` without initilising it: that's undefined behaviour. And, your condition `while(runTimer=1)` is flawed... you need `==` to do a comparison: as it is you assign `1` to `runTimer` every time through the loop, and `1` (being non-zero) is considered `true`, so the loop keeps executing. Which won't start to matter until you stop your function being recursive: as is, it calls itself and only stops when there's a stack overflow, but that'd take a long time at one call per second, so you're probably Control-Cing out first. – Tony Delroy Jun 15 '15 at 05:42

1 Answers1

4

You seem to be using g++ in Windows so I assume it is a flavour of MinGW that came with Code::Blocks.

GNU glibc doesn't support Windows threads (its dev team doesn't care about Windows) so you have to either use a MinGW pthread build, or use an add-on.

Firstly you should add -std=c++11 to your build options.

However, your error message suggests you have installed quite an old version of g++ so I would recommend upgrading to Mingw-w64 (an actively-maintained fork of Mingw). See here for installation help.

For more info about thread support in various versions of MinGW see this thread. I use Mingw-w64 with Win32 threads and the meganz addon in Code::Blocks successfully.

Community
  • 1
  • 1
M.M
  • 138,810
  • 21
  • 208
  • 365