Results 1 to 9 of 9

Thread: cin.get not working?

  1. #1
    Ultimate Member nothing's Avatar
    Join Date
    Oct 2002
    Location
    Brazil
    Posts
    1,413

    cin.get not working?

    When I compile and execute this C++ program (by double clicking the executable file), after the program executes, the DOS window closes and I can't see the output.
    cin.get() used to work for me but somehow it is not working now. Does anybody see anything that might be causing this problem? Thanks.

  2. #2
    Member
    Join Date
    Jul 2002
    Location
    Sweden
    Posts
    79
    I can't see your code, but one solution is usually to introduce an extra variable in main (say char garbage), in a cin >> which forces the program to stop. It won't continue to execute until you press a key.

    Cheers,

    Nini-Veh

  3. #3
    Ultimate Member nothing's Avatar
    Join Date
    Oct 2002
    Location
    Brazil
    Posts
    1,413
    Here...
    Code:
    #include <iostream> 
    using std::cout; 
    using std::cin; 
    using std::endl; 
    
    int main() 
    { 
       const int buffer = 10; 
       char book[] = "The Little Prince"; 
       char author[buffer]; 
    
       cout << "One of my favorite books is " << book << endl; 
    
       cout << "\nSpelled backwards that's"; 
    
       for(int i = sizeof(book) - 1; i >= 0; i--) 
          cout << book[i]; 
    
       cout << "\n\nPlease enter the author's last name: "; 
       cin >> author; 
    
       cout << "\nWhat a coincidence!\n" 
            << author << " is my last name.\n\n"; 
    
       cout << "Press enter to exit."; 
    
       cin.get(); 
    
       return 0; 
    }

  4. #4
    Banned the_mac's Avatar
    Join Date
    Feb 2004
    Location
    peeping in your window
    Posts
    47
    compile errors pleaes

  5. #5
    Ultimate Member nothing's Avatar
    Join Date
    Oct 2002
    Location
    Brazil
    Posts
    1,413
    There is no compile errors. The program compiles just fine.

  6. #6
    Banned the_mac's Avatar
    Join Date
    Feb 2004
    Location
    peeping in your window
    Posts
    47
    i hate it when people do this to me, but you might be using it wrong, im not sure, ive never really used cin.get().

    youll probably learn something new by reading this anyways

    http://cplusplus.com/ref/#libs

  7. #7
    Hired Geek fishybawb's Avatar
    Join Date
    Jun 2002
    Location
    York, UK
    Posts
    3,371
    You need to clear the input stream - your cin.get is reading the key in from the last input. Use:

    Code:
    cin.sync();
    cin.get();
    
    return 0;
    at the end instead

  8. #8
    Ultimate Member nothing's Avatar
    Join Date
    Oct 2002
    Location
    Brazil
    Posts
    1,413
    Thanks fishy. What is the difference between cin.clear() and cin.sync()? I tried using cin.clear() once you said that I need to clear the input stream but it didn't work. cin.sync() worked just fine though.

  9. #9
    Hired Geek fishybawb's Avatar
    Join Date
    Jun 2002
    Location
    York, UK
    Posts
    3,371
    cin.clear() "fixes" a stream if a previous cin operation returned an error (bad input), whereas cin.sync() clears the input buffer. That's my understanding anyway - console programming's not been my thing for a while

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •