I'm using eclipse and I'm building a simple program, but I get an error saying function sleep could not be resolved
#include <time.h>
#include <stdio.h>
#include <conio.h>
#include <iostream>
using namespace std;
int main() {
cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
printf("ciao");
sleep(20);
return 0;
}
I don't know if I need other libraries or something else.
MinGW should be installed properly, so I have no idea
The sleep() function is defined by POSIX, not by the C++ standard.
If you're on a Unix-like system, you need
#include <unistd.h>
If you're not, then the sleep() function might not even be available.
Oh, and mixing cout << ... and printf() is probably not a good idea, and you don't need the #include <conio.h>.
If you are using MinGW as stated, then you may need to include windows.h. The sleep implementation I think uses the Win API Sleep().
For example:
#include <windows.h>
#include <iostream>
int main() {
std::cout << "!!!Hello World!!!" << std::endl;
Sleep(20000);
std::cout << "Text Will Appear After 2 Sec.." << std::endl;
return 0;
}
When developing portable code that must run on unix and windows, I've always defined a sleep() macro for windows that calls the windows Sleep() that looks like this:
#define sleep(a) Sleep(a * 1000)
It's simple enough to do.
Related
I was trying to make a basic program that would print out the words "hello" and then "press any key to continue" but whenever I try to run the code, this pops up
enter image description here
This is my code btw,
#include <iostream>
int main() {
std::cout << 'hello\n';
system('pause');
}
Does anyone know how to fix this error?
Your code is wrong. Try fixing that first not to fail into compilation error.
#include <cstdlib> is missing while using system()
A implementation-defined integer is passed to system() instead of a pointer to string, which system() requires. You should use "", not '', to express string literals.
#include <iostream>
#include <cstdlib>
int main() {
std::cout << "hello\n";
system("pause");
}
I tried using unistd.h sleep(x) on Linux and if the stream isn't explicitly flushed, it'd hang up for the defined time and then output all cout statements at once. On other hand, when using Windows.h header file on my Windows OS, it'd actually wait that amount of time and output each cout statement as if they were individually flushed.
#include <iostream>
#include <Windows.h>
using namespace std;
int main()
{
cout << "Test";
Sleep(2000);
cout << "Test";
}
So, is it implemented or am I wrong about this?
I'm trying to run the following code, but eclipse is giving the errors "type thread cannot be resolved", "thread was not declared in this scope", "expected ; before t1".
#include <iostream>
#include "Tetris.h"
#include <Windows.h> //Sleep
#include <conio.h> //getch()
#include <thread>
#define SLEEP_TIME 1000
using namespace std;
void call_from_thread() {
cout << "hello!" << endl;
}
int main(){
/* some code... */
thread t1(call_from_thread);
/* some more code... */
return 0;
}
So far i've tried the following:
I've also tried the -thread flag as I've seen someone suggesting in an answer here.
I've been looking on google and stackoverflow for hours without being able to solve the problem. "thread" still cannot be resolved and therefore I can't compile and generate a binary.
Any help would be very welcome
thanks in advance
I know we can use
perror()
in C to print errors. I was just wondering if there is a C++ alternative to this, or whether I have to include this (and therefore stdio.h) in my program. I am trying to avoid as many C functions as possible.
You could do something like:
std::cerr << strerror(errno) << std::endl;
That still ends up calling strerror, so you're really just substituting one C function for another. OTOH, it does let you write via streams, instead of mixing C and C++ output, which is generally a good thing. At least AFAIK, C++ doesn't add anything to the library to act as a substitute for strerror (other than generating an std::string, I'm not sure what it would change from strerror anyway).
You could use the boost::system_error::error_code class.
#include <boost/system/system_error.hpp>
#include <cerrno>
#include <iostream>
void
PrintError(
const std::string& message,
int error
)
{
std::cerr << message << ": " <<
boost::system::error_code(
error,
boost::system::get_system_category()
).message()
<< std::endl;
}
int
main()
{
PrintError( "something went wrong!", EINVAL );
return 0;
}
it's a tad verbose, and somewhat overkill if you aren't already using the boost_system library.
With C++11, we have the <system_error> header, so you should be able to use:
std::error_code{errno, std::generic_category()}.message();
Example program:
#include <system_error>
#include <iostream>
int main() {
std::cout << std::error_code{errno, std::generic_category()}.message() << '\n';
}
This prints Success.
See also:
How to convert errno to exception using <system_error>
<system_error> categories and standard/system error codes (regarding whether to use generic_category or system_category)
Here is some code that used to work with my code, but is having a problem now:
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstring>
using namespace std;
int main()
{
stringstream out;
out << 100;
cout << out.str();
}
I get just blank output. I just changed to snow leopard with Xcode 3.2.
Get this exact same issue under the same conditions Snow Leopard 64-Bit XCode 3.2 Base SDK 10.6 and the switch to Base SDK 10.5 resolves it.
Apparently it's a SDK 10.6 issue.
and the correct workaround is to remove the preprocessor macros:
_GLIBCXX_DEBUG=1
_GLIBCXX_DEBUG_PEDANTIC=1
From the preprocessor settings (or else fall back to SDK 10.5 as above).
Apple Discussion Link
it works for me. if there's a problem, it should be your gcc's.
btw, maybe you have to add fflush(stdout); after the cout << sometime the problem is stdout buffer
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstring>
using namespace std;
int main()
{
stringstream out;
out << 100;
cout << out.str();
fflush(stdout);
}
Another idea is that you have a .o file left over from before you upgraded that's somehow messing things up. Mixing .o files from two different versions of the C++ compiler can cause all kinds of strange problems. I also do not discount the header file issue as well, though sstream should be including string.
Shouldn't you add the end of string before converting to string?
cout << out.str() << sdt::ends;