How to let user press enter to stop? Can printf be used to print out variable?

1
2
3
4
for(;;){
(...)
if (???) break;
}


What is needed to be filled in to let user press enter to leave the infinite loop?


Also,
1
2
3
for(int i=1 ; i<=5 ; i++){
printf(i);
}


The above does not work like "cout".
But I have to use "printf" because it can help me change the textcolor.
What's wrong?


Thx.
What is needed to be filled in to let user press enter to leave the infinite loop?

You will need a non-blocking input function. There is no such function in standard c++. If your compiler supports <conio.h>, then you may find something there. Otherwise you can try some system specific thing, or ncurses.

Can printf be used to print out variable?
printf("%d", i);
For more information : http://cplusplus.com/reference/clibrary/cstdio/printf/
dude paste your full code, we may not know the single small error that clogs your program

You need something like iskeypressed()

Windows: http://www.cplusplus.com/forum/beginner/5619/#msg25047
POSIX: http://www.cplusplus.com/forum/general/5304/#msg23940

Hope this helps.
Thx, R0mai.

Duoas,
I am using Windows.
Is lines 7-13 needed (the bool part)?
Also, the iskeypressed() restricts the time.
Does it really work for breaking the loop?
And also how can I fix the iskeypressed() to be responded only by pressing ENTER?
Or by pressing any key is also okay.

Thx.
Last edited on
[Are] lines 7-13 needed ... ?
Yes, you need the function in order to use it.

Also, the iskeypressed() restricts the time.
That is right. The function only returns after the given number of milliseconds have passed (hence, it is a timeout). If you want to forcibly wait only until a key is pressed, then use iskeypressed( INFINITE ). If you only want to check to see if a key is pressed (or rather, waiting to be read) right now, then the timeout should be zero (the default).

However, you usually want to be able to moderate the speed of your loop, like the example I gave you does, so you can specify how many ms to wait per iteration.

Does it really work for breaking the loop?
Did the example code not work? (Answer: yes, of course it works. You might want to try actually compiling and running the example I gave you so you better understand what it does and how it works.)

And also how can I fix the iskeypressed() to be responded only by pressing ENTER?
You cannot. You first need to know that a key is waiting to be read. Once it is, read the key. If the key is the Enter key, then break the loop. Otherwise continue as if nothing had happened.

To read a single key on Windows, see:
http://www.cplusplus.com/forum/beginner/3523/#msg15435
Keep in mind that this is a pretty simple function, and may not work properly for any key the user presses...

Good luck!
Sorry that in the bool part,
error found.
error C2601: 'iskeypressed' : local function definitions are illegal
You pasted the function definition inside another function. Don't do that.
No, I haven't.
I just put them under the declaration.
Sorry that problems are not thoroughly solved.
Can anyone help me solve the error?
Thx.
For your first question here's the code:

1
2
3
4
5
char ch;
do
{
   ch = getch();
}while( ch != char(10) )

10 is newline in ASCII codes. ( 'Enter key' is equal to newline (\n) )
for using getch(), conio.h in needed.

for second question :

if you want to change text color in console you must use cprintf function. there's no other choice. (I think)
if your mean is why it creates error, printf() prototype is like below:
int printf(const char *format[ , arguments . . .]);
it means that first argument must be a string or array of characters.
other arguments can be in other types.
for using printf with an int parameter you should do this:
 
printf("%d", i);

% is control character (like \ in cout )
d means that an integer must be printed.

I hope it helps :-)
From your answer,
I got inspiration and wrote the below codes:
1
2
3
4
5
6
char ch;
for(;;){
     (...);
     ch = getch();
     if ( ch = char(10) ) break;
}

and,
1
2
3
4
5
6
7
8
char ch;
for(;;){
     (...);
     do{
          ch = getch();
     }while( ch != char (10));
     if ( ch = char(10) ) break;
}


Both of them will stop at ch=getch();.
And waiting for the user to key in the ENTER.

How can I solve the problem?

Thx.
Excuse me. I made a mistake.
Newline is 13 in ASCII code.
so my code change to :

1
2
3
4
5
char ch;
do
{
   ch = getch();
}while( ch != char(13) )


In both of your codes, you made a mistake. in if statement, you should use == instead of =
if( ch == char(13) )

I don't understand why you nest two loops in your second code.
Thx for telling me the mistake.
However,
the code ch = getch() will stop the infinite for-loop.
but I want it to work until pressing ENTER.
How can I do that?

Thx.
Why dont you just compare ch to '\n'...?
1
2
3
4
5
char ch;
do
{
   ch = getch();
}while( ch != '\n') 
Last edited on
Actually, my code is like this:
1
2
3
4
5
for(;;){
     cout << "X\bY\b";  // I want to keep showing "X" and "Y" interchanging until pressing ENTER.
     ch = getch();         // This stops the infinite for-loop.
     if ( ch == '\n') break;
}


How can I solve this problem?
Thx.
Can't you do it with multi-threading? Create a separate thread to wait for input and loop and print in the main thread. The main thread can check a shared variable before each output operation to determine if it should continue. It has been pointed out numerous times that there isn't a non-blocking input mechanism. getch() will block the the thread until the user inputs something. Therefore you need to have two concurrent threads running. I suggest that you search the windows forum for ideas on how to start a new worker thread.
RE: Skillless
Why don't you just compare ch to '\n'...?

It doesn't work.
'\n' is not equal to Enter Key. You can examine it to understand.

RE:horace5563333
Here's the code:

1
2
3
4
5
6
7
8
9
10
        char ch;
	int i = 0;
	for(;; i %= 2)
	{
		cout << ( i % 2 == 0 ? "\bX" : "\bY" );
		i++;
		ch = getch();
		if(ch == 13)     // <-- Don't compare ch with '\n'
			break;
	}


I hope it helped ;-)
Majidkamali,
your code will show the following output:
Y/*enter*/X/*enter*/Y/*enter*/X/*enter*/Y/*enter*/X/*enter*/Y...

Maybe I have not explained clearly.
My X and Y will replace each other without pressing ENTER.
After pressing ENTER,
the infinite for-loop will break and the X and Y will not replace each other anymore.

How can I make this?
Thx.
kempofighter,
thx for your advice.

But how can create multithread?
Can just MS Visual C++ 6.0 do this?
What softwares are needed?

Thx.
Topic archived. No new replies allowed.