Remember this is not a manned 24/7 forum. People pop in and out at their whim and answer questions at their whim.
How to do what you are asking depends on what libraries you are using, if you are writing a console application of a windows application, whether or not your application is multi-threaded.
Now that the lecture is over ...
This is one way to do it:
include <stdio.h>
int main (void){
int i = 0;
for (int i = 0; i < 10; i++){
printf("Loop index is %d\n", i);
}
printf ("Press the ENTER key to quit\n");
while (getchar() != '\n');
return 0;
}
NOTE: This code is untested but expected to more or less work.
rr
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
None of these worked as intended. The second produces a compile error and rr's works alone, but for some reason not where I put it, it skips right over it.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Keep in mind that the second code example is C++ code, so you have to compile it as such. If you try to compile it as C code, you will get compile errors.
Wayne
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
If my example seems to work as a standalone example, but not in your context, then check to see that the code flows as expected.
As well, my example is a but simple. It does not flush the input stream before reading from it. So if you had been typing previously, then it is possible that some characters remain in the input stream buffer. So as soon as you hit the while(getchar()) it consumes teh characters in the characters in the stream and seems to not work.
rr
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I am surprised that cin.sync() works to flush the input buffer. But I get all the input routines at some lower level all use the same stdin stream.
I would have expected you to use fflush(stdin).
The reason the while loop is emty is because all it is doing is consuming input characters until you hit ENTER.
while ( getchar() != '\n');
is equivalent to (among others).
bool done = false;
while ( !done ){
if (getchar() == '\n'){
done = true;
}
}
The C and C++ languages allow for a rich number of expressions and shortcuts. Which is not always good. But the usage I employed is fairly standard for the C world.
rr
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
The input buffer tends to get in the way when using input functions. There is often something remaining in it at the time a "pause" function is called, so it has to be flushed somehow before trying to read the ENTER key.
Unfortunately, the fflush function is only defined for output so it should not be used on stdin. Even though some compilers also use it on input, this is unreliable and is not portable either.
The cin.sync() thing seems to work in C++ but is not available in C. I don't quite understand what it does to the buffer in C++ either (the documentation I have it not clear on that) so even though it I has worked for me I won't use it until I understand precisely what it does.
What I have found the last time I came across this topic is something that I understand and that applies to both C and C++, even though it does not explicitly "flush" anything. You can just send the pointer to the end of the input stream just before calling the input function. This simply skips over whatever may be left in the buffer. In C it looks like this:
printf("Press ENTER to continue...");
fseek(stdin, 0, SEEK_END);
getchar();
The same thing in C++ might look this way:
cout << "Press ENTER to continue...";
cin.seekg(0, ios::end);
cin.get();
I have not tested this thoroughly under all conditions to see how stable this is since I have not done any console-based programming for a while. It worked on short tests though, and it seems understandable enough...
qWake
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Thanks for that clarification, I simply went by what I quickly picked off the web. A more detailed search at MSDN (of all places) on fflush() reveals the following:
fflush( stdin ); // fflush on input stream is an extension to the C standard
I didn't know that. Well ... I initially thought that fflush was only for output streams, but a quick google seemed to indicate that it worked on stdin as well.
rr
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
How can I make a command similar to the Windows PAUSE command? I want the user to just press enter and the program flow to continue.
So... I take it it's not possible?
Don't be silly! Of course it is possible.
Remember this is not a manned 24/7 forum. People pop in and out at their whim and answer questions at their whim.
How to do what you are asking depends on what libraries you are using, if you are writing a console application of a windows application, whether or not your application is multi-threaded.
Now that the lecture is over ...
This is one way to do it:
include <stdio.h>
int main (void){
int i = 0;
for (int i = 0; i < 10; i++){
printf("Loop index is %d\n", i);
}
printf ("Press the ENTER key to quit\n");
while (getchar() != '\n');
return 0;
}
NOTE: This code is untested but expected to more or less work.
rr
include <iostream>
using namespace std;
int main()
{
cin.ignore(cin.rdbuf()->in_avail() + 1);
return 0;
}
don't know if this is what you are looking for,
but it might help
None of these worked as intended. The second produces a compile error and rr's works alone, but for some reason not where I put it, it skips right over it.
The second code compiles fine here. Dev version 4.9.9.0, Windows XP Pro:
Executing g++.exe...
g++.exe "C:\mycstuff\testit.cpp" -o "C:\mycstuff\testit.exe" -I"C:\Dev-Cpp\include\c++\3.3.1" -I"C:\Dev-Cpp\include\c++\3.3.1\mingw32" -I"C:\Dev-Cpp\include\c++\3.3.1\backward" -I"C:\Dev-Cpp\lib\gcc-lib\mingw32\3.3.1\include" -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib"
Execution terminated
Compilation successful
What does your full log look like (not just the error message)?
Wayne
Keep in mind that the second code example is C++ code, so you have to compile it as such. If you try to compile it as C code, you will get compile errors.
Wayne
If my example seems to work as a standalone example, but not in your context, then check to see that the code flows as expected.
As well, my example is a but simple. It does not flush the input stream before reading from it. So if you had been typing previously, then it is possible that some characters remain in the input stream buffer. So as soon as you hit the while(getchar()) it consumes teh characters in the characters in the stream and seems to not work.
rr
That would be the problem... I flush it using std::cin.snyc() right?
OK, now it works, but could you explain it to me? what is getchar() and why do you have an empty while loop?
also another one is cin.get(); , you might have to use two of them... it just waits for you to press anykey...
You might find this useful:
http://www.cplusplus.com/ref/cstdio/getchar.html
Wayne
getchar is a macro that simply defines
getc(stdin)
I am surprised that cin.sync() works to flush the input buffer. But I get all the input routines at some lower level all use the same stdin stream.
I would have expected you to use fflush(stdin).
The reason the while loop is emty is because all it is doing is consuming input characters until you hit ENTER.
while ( getchar() != '\n');
is equivalent to (among others).
bool done = false;
while ( !done ){
if (getchar() == '\n'){
done = true;
}
}
The C and C++ languages allow for a rich number of expressions and shortcuts. Which is not always good. But the usage I employed is fairly standard for the C world.
rr
The input buffer tends to get in the way when using input functions. There is often something remaining in it at the time a "pause" function is called, so it has to be flushed somehow before trying to read the ENTER key.
Unfortunately, the fflush function is only defined for output so it should not be used on stdin. Even though some compilers also use it on input, this is unreliable and is not portable either.
The cin.sync() thing seems to work in C++ but is not available in C. I don't quite understand what it does to the buffer in C++ either (the documentation I have it not clear on that) so even though it I has worked for me I won't use it until I understand precisely what it does.
What I have found the last time I came across this topic is something that I understand and that applies to both C and C++, even though it does not explicitly "flush" anything. You can just send the pointer to the end of the input stream just before calling the input function. This simply skips over whatever may be left in the buffer. In C it looks like this:
printf("Press ENTER to continue...");
fseek(stdin, 0, SEEK_END);
getchar();
The same thing in C++ might look this way:
cout << "Press ENTER to continue...";
cin.seekg(0, ios::end);
cin.get();
I have not tested this thoroughly under all conditions to see how stable this is since I have not done any console-based programming for a while. It worked on short tests though, and it seems understandable enough...
qWake
Thanks for that clarification, I simply went by what I quickly picked off the web. A more detailed search at MSDN (of all places) on fflush() reveals the following:
fflush( stdin ); // fflush on input stream is an extension to the C standard
I didn't know that. Well ... I initially thought that fflush was only for output streams, but a quick google seemed to indicate that it worked on stdin as well.
rr
Mouaaaaaaaa !!!! you are all "nul !"
Write that in your code:
system("PAUSE>nul") for not write "Press ENTER to continue..." and make a pause.
BY THE BIG_BOSS !!!!
i didnt read it but i used this similar to the system("PAUSE")
cout << "Press Enter to Continue" << endl;
getch();
Getch() is a useful function but im sure using the escape sequence /r which also stands for the key enter you can sort it out.