Thread: alternative to cin

  1. #1
    Unregistered
    Guest

    alternative to cin

    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/

  2. #2
    Sir Mister Insane Sako Klinerr1's Avatar
    Join Date
    May 2002
    Posts
    608
    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.
    Email: [email protected] || AIM: MisterSako || MSN: [email protected]

    -the shroom has spoken

  3. #3
    Unregistered
    Guest

    well...

    well, you know how cin waits for the user to enter something before the program continues on? im looking for an alternative to that

  4. #4
    Unregistered
    Guest
    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.

  5. #5
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    Originally 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.
    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 for

  6. #6
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    like a while loop:
    Code:
    while (1) {
    
    if  something, break;
    
    }

  7. #7
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    In ygf's post, if you turned off compiler optimization, it would probably be more effecient if you used goto.

    Code:
    LABEL_LOOP:
     if (something) goto OUT_OF_LOOP;
     goto LABEL_LOOP;
    
    OUT_OF_LOOP:
     //continue with your program
    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).

  8. #8
    Unregistered
    Guest
    Originally 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.
    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 prog

  9. #9
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    >>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.

  10. #10
    Registered User snowy101's Avatar
    Join Date
    May 2002
    Posts
    22

    The answer to your question is

    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

  11. #11
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    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.
    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.

    2) You mentioned unconditional jumps. What do you think going back to the beginning of the while loop is?

  12. #12
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    >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.

  13. #13
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    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.

    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
    It looks pretty similar to my code. Not much optimization can be done here.

  14. #14
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    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.

  15. #15
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cin problem
    By mikahell in forum C++ Programming
    Replies: 12
    Last Post: 08-22-2006, 11:14 AM
  2. getline(function)??
    By dac in forum C++ Programming
    Replies: 7
    Last Post: 03-10-2006, 12:42 PM
  3. cin not allowing input after first use of function
    By Peter5897 in forum C++ Programming
    Replies: 5
    Last Post: 01-31-2006, 06:29 PM
  4. Overriding Cin with Cout
    By Tainted in forum C++ Programming
    Replies: 5
    Last Post: 10-06-2005, 02:57 PM
  5. Simple Question relating to the IOStream Lib and Cin
    By XcomvB in forum C++ Programming
    Replies: 6
    Last Post: 06-12-2003, 02:17 AM