Hi, im looking for an alternative to cin. Is there a library with an input stream that doesn't wait for the user to enter something? I'm trying to make a program that runs in a loop even when the user doesn't enter anything. Thanks/
Hi, im looking for an alternative to cin. Is there a library with an input stream that doesn't wait for the user to enter something? I'm trying to make a program that runs in a loop even when the user doesn't enter anything. Thanks/
u mean like to run something than have it start over? just use the goto command! i think ur the unreigstered user who said goto is a qbasic thing. it is also used in c++. sorta. there might be something in a tuoial in cprogramming.com on how to use it. i dont feel like explaining.
well, you know how cin waits for the user to enter something before the program continues on? im looking for an alternative to that
If you want to do other stuff until the user presses a key to start entering something use kbhit(). You need to include conio.h
<code>
#include <iostream.h>
#include <conio.h>
int main()
{
char blah[20];
while(!kbhit())
{
//do stuff here
}
cin>>blah;
cout<<blah;
return 0;
}
</code>
This will do the stuff in the loop until the user presses a key, then that key will be the first one entered in the cin part.
Dont use goto unless its absolutely necessary - which is very rare. It is a bad habit to get into and there are always better ways of achieving anything that a goto is used forOriginally posted by Klinerr1
u mean like to run something than have it start over? just use the goto command! i think ur the unreigstered user who said goto is a qbasic thing. it is also used in c++. sorta. there might be something in a tuoial in cprogramming.com on how to use it. i dont feel like explaining.
like a while loop:
Code:while (1) { if something, break; }
In ygf's post, if you turned off compiler optimization, it would probably be more effecient if you used goto.
Although goto is shunned, it can be used efficiently in some situations. In this example, the need to check the value of the while loop's argument is eliminated. However, something like this probably wouldn't be needed (unless your program is really going slow).Code:LABEL_LOOP: if (something) goto OUT_OF_LOOP; goto LABEL_LOOP; OUT_OF_LOOP: //continue with your program
and no he isnt the onbe who told you not to use goto... that would be me and sure buddy keep using goto lol... have fun when you have to code anything remotly above a cin cout progOriginally posted by Klinerr1
u mean like to run something than have it start over? just use the goto command! i think ur the unreigstered user who said goto is a qbasic thing. it is also used in c++. sorta. there might be something in a tuoial in cprogramming.com on how to use it. i dont feel like explaining.
>>In ygf's post, if you turned off compiler optimization<<
Why would you do that?
>>Although goto is shunned, it can be used efficiently in some situations.<<
Not this one. If you really wanted to instruction count then carrying out the logical test in the while conditional would probably result in equally efficient code. I'm pretty sure the only place a goto has the potential to be more efficient in unoptimised code is in it's use in unconditional jumps.
ust have #include <conio.h> and to make it stop use getch(); or getchar(); depends on the version you use hope it helps because that will pause the program until the user enters a key like enter or space bar. tell if it helps
___________________________
when candy is made so is a smile
1) Let's see, optimization comes off in Dev-C++ unless you turn it on. Also, I know some people that keep it off just to make sure that things they don't want done aren't done.Originally posted by Sorensen
>>In ygf's post, if you turned off compiler optimization<<
Why would you do that?
>>Although goto is shunned, it can be used efficiently in some situations.<<
Not this one. If you really wanted to instruction count then carrying out the logical test in the while conditional would probably result in equally efficient code. I'm pretty sure the only place a goto has the potential to be more efficient in unoptimised code is in it's use in unconditional jumps.
2) You mentioned unconditional jumps. What do you think going back to the beginning of the while loop is?
>1) Let's see, optimization comes off in Dev-C++ unless you turn it on. Also, I know some people that keep it off just to make sure that things they don't want done aren't done. <
Then don't be suprised if your code efficiency is not what it could be. You probably don't have enough control in C++ to manually do all the optimisations a compiler would.
> You mentioned unconditional jumps. What do you think going back to the beginning of the while loop is?<
What code do you think is generated in the normal C++ while loop? I was talking about your use of a conditional goto.
By all means challenge conventional wisdom, but perhaps those people that claim you shouldn't use goto unless you know what you're doing are correct.
Sorensen, do u actually know any asm? If so, could you show me a way that the compiler could optimize my code to anything better? The code I listed almost directly translates to asm (besides the if statement which would be a cmp). For example, here's an asm implementation.
It looks pretty similar to my code. Not much optimization can be done here.Code:INLOOP: ;beginning of loop cmp eax, 0 ;sees if eax=0 jz OUTOFLOOP ;jumps to OUTOFLOOP if eax=0 jmp INLOOP ;jumps back to INLOOP OUTOFLOOP: ;do whatever
Well, I'm sure you probably didn't mean it but that could be optimised to one instruction quite easily if it could be established that it resulted in an infinite loop.
However, there probably isn't much optimisation that can be done assuming it actually did something. It'd be no more efficient than a correctly constructed while loop (assuming efficiency was a concern). However C++ contains a few more features than basic loops that do nothing. Try to outperform your optimser with a program that contains a few functions, classes and uses some of the C/C++ standard library.
When did I ever mention anything but the basic loop I made? I never referred to anything else. All that I was trying to show is that in unoptimized code, my use of the infamous goto would be more effecient. You were the only one that referred to something different.