C++ For loop loops only 299 times - c++

I'm having this weird problem. My code is simple:
#include <iostream>
using namespace std;
int main() {
int num;
cout << "number: ";
cin >> num;
for (int i=0;num>i;i++) {
cout << i <<"\n";
}
system ("Pause");
return 0;
}
If the input for example is 1000, the output contains numbers from 701-999.
Any idea?
I'm using Dev-C++ IDE on Parallels.

Actually it prints all of them, from 0 to 999, but your console's buffer is not large enough. So you see only the last part. if you print into a file, not the console, you'll see :)

The loop ends when num>i is no longer true. This occurs when i is 1000, so the last loop executed will be with value 999. As for not seeing lower than 701, maybe your screen buffer is too small.

It will start with 0-999. Also, it appears to you that it starts with 701 because of your console screen settings. If you want to see it for yourself, change the newline into a space:
cout << i <<" ";

Did 0-700 scroll off the screen? Run your exe like this
your_program > out.txt
Then look at out.txt in an editor.

Works absolutely fine for me. I'd suggest your IDE might be playing tricks on you. Could you redirect output into a file and check that?

Regarding #JoshD answer,
You will need to:
for (int i=0;num>=i;i++) {
cout << i <<"\n";
}

Related

How to print page by page with cout in C++?

Imagine I have this code:
for (int i=0; i<1000; i++) {
cout << i << endl;
}
So in the console, we see the result is printed once until 999. We can not see the first numbers anymore (let say from 0 to 10 or 20), even we scroll up.
How can I print output page by page? I mean, for example if there are 40 lines per page, then in the console we see 0->39, and when we press Enter, the next 40 numbers will be shown (40->79), and so on, until the last number.
While the previously answers are technically correct, you can only break output at a fixed number of lines, i.e. at least with standard C++ library only, you have no way to know how many lines you can print before filling the entire screen. You will need to resort to specific OS APIs to know that.
UNIX people may tell you that, in fact, you shouldn't bother about that in your own program. If you want to paginate, you should just pipe the output to commands such as less or more, or redirect it to a file and then look at the file with a text editor.
You could use the % (remainder) operator:
for (int i=0; i<1000; i++) {
std::cout << i << '\n';
if(i % 40 == 39) {
std::cout << "Press return>";
std::getchar();
}
}
This will print lines 0-39, then 40-79 etc.
If you need something that figures out how many lines the terminal has and adapts to that, you could use curses, ncurses or pdcurses. For windows, (I think) pdcurses is what you should go for.
#include <curses.h>
#include <iostream>
int main() {
initscr(); // init curses
int Lines = LINES; // fetch number of lines in the terminal
endwin(); // end curses
for(int i = 0; i < 1000; i++) {
std::cout << i << '\n';
if(i % Lines == Lines - 1) {
std::cout << "Press return>";
std::getchar();
}
}
}
This only initializes curses, fetches the number of lines in the terminal and then deinit curses. If you want, you could stay in curses-mode and use the curses functions to read and write to the terminal instead. That would give you control over the cursor position and attributes of what you print on screen, like colors and underlining etc.

[C++]Eclipse ignores console input during debugging

During debugging Eclipse doesn't "see" INPUT from built-in console, just ignores it. Simple example:
#include <iostream>
using namespace std;
int main() {
int a;
cin >> a;
cout << a << endl;
cin >> a;
cout << a << endl;
return 0;
}
works perfectly fine when just run, but when I try to debug it, first "data on input"(?) is always a number around 40, and only zeros next, no matter what i will write to console.
So program executes, first variable is set to ~40 and all next to zeros.
Output works fine, values are written to console, only input doesn't work.
I work on Windows 10 and use MinGW.
Thanks in advance.
#EDIT
Everything works, when I use native Windows console
(.gdbinit file with set new-console on line)

Why doesn't the cout command print a message?

#include <iostream>
using namespace std;
int main()
{
int x = 42;
cout << x; // This line doesn't print! Why?
return 0;
}
Screenshot of Visual C++: http://bildr.no/image/ZlVBV0k0.jpeg
This code gives me nothing but a black console window that flashes by when I click on debug. Isn't the number 42 supposed to be printed in the console window? This is my first application in C++. I have experience in C# from high school.
EDIT:
Now I have tried this code:
// Primtallsgenerator.cpp : Defines the entry point for the console application.
//
#include <iostream>
using namespace std;
int main()
{
int x = 42;
cout << x << endl; // This line doesn't print! Why?
cin >> x;
return 0;
}
It still doesn't work. Screenshot of the code here: http://bildr.no/image/ODNRc3lG.jpeg
The black windows still just flashes by...
Two things to note:
First, you are not forcing the buffer to flush, so there is no guarantee the output is being sent to the screen before the program ends. Change your cout statement to:
cout << x << endl;
Second, Visual Studio will close the console when it ends (in Debugging mode). If you do not debug it (Ctrl-F5 by default), it will keep the console open until you press a key. This will allow you to see the output. Alternatively, you can add a cin.get() before your return statement which will force the program to wait for a character to be in the input stream before the program is allowed to exit.
It did print the message, it was just too fast for you to see.
add this command:
cin >> x;
or this one
while(true) {}
before the return statement.
Yes, it will print the number. Then the program ends, and the console window is closed. Run it in the debugger, and put a breakpoint on the return 0; line. Then you'll see it.
This code should work fine:
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
int x = 42;
cout << x;
getchar();
return 0;
}
Also check this documentation about getchar().
I recommend to use system pause at the end before the "return 0" statement like this:
system("PAUSE");
This is cleaner and much more effective.
If you are working with a console application in Visual Studio, you have to go to your project's linker properties and set your SubSystem setting to CONSOLE.
And get a habit of running your code without a debugger (Ctrl+F5 by default) when you don't need the debugger. That way the console window will not flash and disappear by itself.

Cout in While loop Strange behaviour

My code look like below
int i=0;
while(i<10){
cout<<"Hello";
sleep(1);
i++
}
In Windows the code prints on each loop but in Linux it prints everything after exiting while loop . And also if I put an endl at the last of cout then it prints on each loop. Why this happening ?. Can anyone explain this behavior?.
Try to use cout.flush(); maybe the two OS has different policy in term of buffering the stdout.
For efficiency reasons, sometimes the standard streams will be implemented with a buffer. Making lots of tiny writes can be slow, so it will store up your writes until it gets a certain amount of data before writing it all out at once.
Endl forces it to write out the current buffer, so you'll see the output immediately.
#include <iostream>
using namespace std;
int main()
{
int i = 0;
while(i < 10){
cout << "Hello" << endl;
sleep(1);
++i;
}
}

c++, sleep, and loops

Ok, this is just out of curiousity, but why does the sleep function NOT work in a loop, or how can I Get it to work in a loop?
for(int i = 0; i < 5; i++) {
cout << i << endl;
sleep(2);
}
cout is buffered, meaning its contents aren't always written to the console right away. Try adding cout.flush() right before sleep(2);
If that isn't working for you you could try this code:
#include <iostream>
#include <windows.h>
...
for(int i = 0; i < 5; i++) {
cout << i << endl;
Sleep(2000);
}
Have you tried unrolling the loop to see if that behaves the same way?
cout << 1 << endl;
sleep(2);
cout << 2 << endl;
sleep(2);
// Etc.
Assuming that behaves the same way, even though std::endl is supposed to flush the buffer, it really does look like dave.kilian has the right idea that cout isn't getting flushed until the program (presumably) terminates.
In that case, try doing the std::flush and see if that helps - it's possible you have a bug (missed feature?) in your standard library.
Another thing to try is to redirect the output to a file while watching it with tail -f in another window. See if the same delay occurs when redirected.
Finally try playing with the compiler's optimization level and see if that changes the results.
In my humble opinion, this program should work correctly. Maybe your std::cout is redirected somewhere else? You don't call the correct sleep() function (but no header provided)? Or other problem? But it should work.
Dave has already given you the answer, so I won't touch on that. However, if its purely for debugging or prototype code, you could also pipe the output to std::cout's sibling, std::cerr, which is unbuffered to begin with. This means you do not need to call flush explicitly and you do not need to add an endl to your output.
Try Sleep() instead of sleep()