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?
Related
Basically, I'm following a simple tutorial about files handling in C++.
I've been trying to create and write into a txt file at the same time, but any of the methods I've tried won't actually create a txt file in my executable location.
I should also say that, I print myfile.is_open() just to know if the file truly created and opened, but I get 0 everytime with every method.
What am I doing wrong ?
I mainly tried to create and write to a txt file like this:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream myfile;
myfile.open("example.txt", ios::out);
cout << myfile.is_open() << endl;
myfile << "Writing this to a file.\n";
myfile.close();
}
First, I bet you're using an IDE like Visual Studio. Most IDEs set your working directory somewhere other than your project directory. I don't use Visual Studio, but many of them put them in ../.
So your file is being produced, but not where you think you should find it.
If you compile and run this program without an IDE, you'll get your file where you expect it.
You may also be able to tell your IDE that the working directory should be your project directory.
Now, to keep you from making a few bad habits, I'm going to tell you two more things.
It's considered a mistake to do using namespace std. Instead, I do using statements only on those things I am going to use frequently. In your short code, I wouldn't have done any.
Next, if you're going to write out a file, it's better to use std::ofstream. It's otherwise the same code. But it's a bit clearer that you're only using the file for output.
So my version of your code:
#include <iostream>
#include <fstream>
int main()
{
std::ofstream myfile;
myfile.open("example.txt");
std::cout << myfile.is_open() << std::endl;
myfile << "Writing this to a file.\n";
myfile.close();
}
Yeah, those std:: everywhere can be annoying, so you could do this:
#include <iostream>
#include <fstream>
using std::ofstream;
using std::cout;
using std::endl;
int main()
{
ofstream myfile;
myfile.open("example.txt");
cout << myfile.is_open() << endl;
myfile << "Writing this to a file.\n";
myfile.close();
}
I actually have an include of CommonUsing.h that I put a few things I do almost everywhere.
#pragma once
#include <chrono>
#include <iostream>
#include <date/date.h>
//======================================================================
// The most common using statements I do in most of my code.
//======================================================================
using std::cout;
using std::cerr;
using std::endl;
using std::string;
using namespace std::chrono_literals;
using date::operator<<;
I have a small problem with the clear command in C++. The Clear function is supposed to clear everything at the end of the program but it clears too early. Does anyone know how to solve that problem and let the program clear it as the complete end? I want that at the end the console is empty.
#include <iostream>
#include <chrono>
#include <thread> // sleep_for, sleep_until
#include <ctime>
#include <thread>
using namespace std;
using namespace std::this_thread; // sleep_for, sleep_until
using namespace std::chrono;
int main() {
cout<<"\nTest message?";
sleep_for(nanoseconds(1500000000));
system("clear");
cout<<"\nThis message should be cleared";
sleep_for(nanoseconds(5000000000));
system("clear");
}
Console output:
This message should be clearedîș§
standard cout in c++ is buffered.
this means that the text is held before it is sent to the screen, this helps with performance.
this is causing the clear to be called before the buffer is sent to the screen.
to fix this you need to manually flush the buffer by either putting << std::flush or << std::endl at the end of the cout statement
#include <istream> //Includes the input/output library
using namespace std; // Makes std features available
// The main function of the program
// It outputs the greeting to the screen
int main() {
count <<"Hello World! I am C++ Program." <<endl;
return 0;
}
IntelliSense: no operator message Line 7, Column 8
error C2563:mismatch in formal parameter list Line 7, Column 1
Replace #include <istream> with the correct header, #include <iostream>.
Helpful mnemonic:
io = input/output
stream = "stream of data"
Additionally, the name of the standard output stream is std::cout or cout with the std:: namespace scope removed.
Helpful mnemonic:
std:: = Standard library's
cout = console output
The problems you are having with your simple block of code is simply: spelling errors.
Firstly, you have misspelled the input/output stream file in your include statement, so you need to rename the header file to:
#include <iostream>
There is no heade file named istream.
Secondly, you also misspelled the cout function to count. Change that line to:
cout << "Hello World! I am C++ Program." << endl;
Those lines should work now.
Also a recommendation for your future programs; avoid using the line
using namespace std;
Why? Because as you move on to more complex programming, you will undoubtedly learn and begin to define a data type or variable, and sometimes, that name may also be used by the standard library. As a result, you will have a hard time trying to differentiate the variables or data types you defined and the ones defined in the std library.
Therefore, try and attach std:: before every function that is a part of the standard library.
EDIT:
The code you posted in the comments box is pretty unreadable, so I just fixed it and have posted it below:
#include <iostream> //Includes the input/output library
using namespace std; // Makes std features available
// The main function of the program
// It outputs the greeting to the screen
int main()
{
cout <<"Hello World! I am C++ Program." <<endl;
return 0;
}
I've tried this in my IDE and fixed with the same and only recommendations from above. It works for me.
In my code below errors occur and the program will not run, I am required to make a Constructor that must open the file with the given filename. If the filename does not exist then it Prints an error message and terminates the program.
Below is the code that I have done so far in C++:
#include "ReadWords.h"
#include <iostream>
#include <cstdlib>
using namespace std;
ReadWords::ReadWords(const char filename[])
{
wordfile.open(filename);
if (!wordfile)
{
cout << "cannot make " << filename << endl;
exit(1);
}
}
void ReadWords::close()
{
wordfile.close();
}
Why dont you try including fstream to the top of your file and see if that works
I suppose wordfile is of type std::fstream. If your ReadWords.h #includes <fstream>, it should work (compiles and works as expected).
By the way, it's a bad practice to use using namespace std;.
Also, since you use C++, take a look at std::string. It's safer than using plain char* or char[].
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.