Thread: cin.getline not pausing function so user can enter input???

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    30

    cin.getline not pausing function so user can enter input???

    when i ask user to input name number 1 it pauses like it is supposed to and allows the user to input their name. when it asks the user to input name #2 it skips right into the loop and displays "enter test score 1" before the user has a chance to enter their name. why isnt cin.getline pausing the program like its supposed to and like it did with name 1?


    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	char names[5][50];
    	char grades[5];
    	double set1[4];
    	double set2[4];
    	double set3[4];
    	double set4[4];
    	double set5[4];
    	double total1 = 0;
    	double total2 = 0;
    	double total3 = 0;
    	double total4 = 0;
    	double total5 = 0;
    
    
    		cout << "Enter student #1 name: ";
    		cin.getline(names[0], 50);
    
    		for (int j = 0; j < 4; j++)
    		{
    			cout << "Enter test score #" << j+1 << ": ";
    			cin >> set1[j];
    			total1 += set1[j];
    		}
    
    		cout << "Enter student #2 name: ";
    		cin.getline(names[1], 50);
    
    		for (int j = 0; j < 4; j++)
    		{
    			cout << "Enter test score #" << j+1 << ": ";
    			cin >> set2[j];
    			total2 += set2[j];
    		}
    
    		cout << "Enter student #3 name: ";
    		cin.getline(names[2], 50);
    
    		for (int j = 0; j < 4; j++)
    		{
    			cout << "Enter test score #" << j+1 << ": ";
    			cin >> set3[j];
    			total3 += set3[j];
    		}
    
    		cout << "Enter student #4 name: ";
    		cin.getline(names[3], 50);
    
    		for (int j = 0; j < 4; j++)
    		{
    			cout << "Enter test score #" << j+1 << ": ";
    			cin >> set4[j];
    			total4 += set4[j];
    		}
    
    		cout << "Enter student #5 name: ";
    		cin.getline(names[4], 50);
    
    		for (int j = 0; j < 4; j++)
    		{
    			cout << "Enter test score #" << j+1 << ": ";
    			cin >> set5[j];
    			total5 += set5[j];
    		}
    
    
    	return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    See Hirosh's thread, just a few lines down, for enlightenment.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    There is a common issue when mixing cin >> with getline. When you request input with cin >>, it reads until it fills the variable and stops. The user types in a test score and hits <enter>. After the last test score is read, the newline character from the <enter> is still in the input stream. The next call to getline reads until it finds a newline, but the newline from the previous call to cin >> is still there so it stops immediately.

    The simple solution is to add cin.ignore() after all calls to cin >> to ignore the trailing newline. Do not put cin.ignore() after a call to getline because getline automatically ignores that newline.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    30
    ahhh thanks so much guys. never new newline stays in input stream after cin>>. nice to know.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It would be better to use std::string and std::getline instead.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 02:20 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  4. Replies: 5
    Last Post: 07-05-2005, 12:37 PM
  5. SSH Hacker Activity!! AAHHH!!
    By Kleid-0 in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 03-06-2005, 03:53 PM