I have a problem with C + +.

This is a discussion on I have a problem with C + +. within the C++ Programming forums, part of the General Programming Boards category; Hello .. I have a problem with C + +. Look at this small program ... Code: #include <iostream> #include ...

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    3

    I have a problem with C + +.

    Hello ..


    I have a problem with C + +.

    Look at this small program ...

    Code:
    #include <iostream>
    #include <windows.h>
    
    using namespace std; 
    
    
    main ()
    {
         
    for ( int i = 0 ; i < 50 ; i ++ )
    {
    
    cout << i << endl ;
    
    Sleep(1000);
    
    }
    
    cout << "Done! ";
    
    getchar ();
    getchar ();
    
    }
    How to Make this program when enter Character 's' exit from loop .

    without using cin or break . ??

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Can you use other functions, like getline()?
    You don't need break. You can do something like
    Code:
    i < 50 && c != 's'

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by OOP_C++ View Post
    How to Make this program when enter Character 's' exit from loop. without using cin or break . ??
    The real question is: Why do you have such superficial requirements?

  4. #4
    Registered User
    Join Date
    Jan 2010
    Posts
    3
    I mean, when I press the letter S at any time directly out of the loop .. I hope you write the program to understand

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by OOP_C++ View Post
    I mean, when I press the letter S at any time directly out of the loop .. I hope you write the program to understand
    Right. So, again, why exactly can't 'cin' nor 'break' be used? Just a whim, or what? Anyway, post #2 basically answers your question, I think.

  6. #6
    Grey Wizard C_Sparky's Avatar
    Join Date
    Sep 2009
    Posts
    50
    Work with something like this using GetAsyncKeyState();

    Code:
    #include <iostream>
    #include <windows.h>
    
    using namespace std;
    
    int main()
    {
         int i = 0;
         while(!GetAsyncKeyState(0x53))
         {
               ++i;
               cout << i << endl;
               Sleep(1000);
         }
         return 0;
    }

  7. #7
    Registered User
    Join Date
    Jan 2010
    Posts
    3
    Quote Originally Posted by C_Sparky View Post
    Work with something like this using GetAsyncKeyState();

    Code:
    #include <iostream>
    #include <windows.h>
    
    using namespace std;
    
    int main()
    {
         int i = 0;
         while(!GetAsyncKeyState(0x53))
         {
               ++i;
               cout << i << endl;
               Sleep(1000);
         }
         return 0;
    }

    it is good .

    Thank you very much

    but , Can you explain to me this function in detail . ((GetAsyncKeyState(0x53))

    And how to used in for loop ??

  8. #8
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    Look GetAsyncKeyState up on msdn, it's a WinAPI function and should be well documented there. Cin is better though, because it is portable, anything in WinAPI is not.

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    You are confused. "break" is exactly what you want to get out of a loop immediately.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  10. #10
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    cin is not asynchronous though
    There are other ways past GetAsyncKeyState(), but it seems like a good solution here.
    The other way to go is to use signals. When 's' is pressed it will send a signal. The signal handler will automatically called and it will result on breaking the loop.

    This can be done by throwing an exception maybe. Or setting a flag and each line in the loop being if(flag) else break. Or more drastically killing the program.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21