Cin.get()

This is a discussion on Cin.get() within the C++ Programming forums, part of the General Programming Boards category; In my program, I have a cout statement I want peopl to be able to rea, so I use cin.get() ...

  1. #1
    Hi ay_okay's Avatar
    Join Date
    Dec 2004
    Location
    Here
    Posts
    69

    Cin.get()

    In my program, I have a cout statement I want peopl to be able to rea, so I use cin.get() to pause. For some reason, it skips that and ends. I think its my cpu speed or keyboard I unno. Can someone help?

  2. #2
    Registered User
    Join Date
    Dec 2004
    Posts
    32
    Since you did not post any code I will guess and say add "cin.ignore()".
    This is a common question. A search of the board would probably give more of a explanation. But here are some examples.
    This will work.
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	cout << "Enter hours worked" << endl;
    	cin.get();
    	return 0;
    }
    But this will not.
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	cout << "Enter hours worked" << endl;
    	int var1 =0;
    	cin >> var1;
    	cout << "Hours worked = " << var1 << endl;
    	cout << "Press the Enter to continue" << endl;
    	cin.get();
    	return 0;
    }
    This will.
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	cout << "Enter hours worked" << endl;
    	int var1 =0;
    	cin >> var1;
    	cout << "Hours worked = " << var1 << endl;
    	cout << "Press the Enter to continue" << endl;
    	cin.ignore();
    	cin.get();
    	return 0;
    }
    Long story short you need to get rid of the '\n' that was left in the stream. If the search does not yield good results please post back with any questions.

  3. #3
    Hi ay_okay's Avatar
    Join Date
    Dec 2004
    Location
    Here
    Posts
    69
    Your cin.ignore() worked, but first I had to switch cin>> with cin.getline() because it missed the spaces and skipped the cin.get(). Thanks for the help!!!

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    but first I had to switch cin>> with cin.getline()
    ...then you don't need cin.ignore().

  5. #5
    dra
    dra is offline
    Weak. dra's Avatar
    Join Date
    Apr 2005
    Posts
    166
    just out of preference, i use getch(). lol. i don't know why.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cin.get(); not working with my switch statement
    By tenor_jazz13 in forum C++ Programming
    Replies: 2
    Last Post: 01-12-2008, 10:33 PM
  2. cin.get() problem
    By Cilius in forum C++ Programming
    Replies: 20
    Last Post: 07-28-2005, 06:32 PM
  3. Confused about cin.get(); and classes.
    By RaccoonKing in forum C++ Programming
    Replies: 6
    Last Post: 07-17-2005, 12:44 PM
  4. cin.get();
    By swgh in forum C++ Programming
    Replies: 2
    Last Post: 06-29-2005, 08:51 AM
  5. curiosity about cin.get() and cin.getline()
    By ssjnamek in forum C++ Programming
    Replies: 18
    Last Post: 11-30-2003, 01:26 AM

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