Thread: Problems with getline

  1. #1
    Village id10t
    Join Date
    May 2008
    Posts
    57

    Problems with getline

    I have a problem using getline in a loop. The first time that I have to enter a Address, it works. but from there on it just skips the cin for the rest of the gardens..

    Code:
    void inputInformation(GardenInfo gardensP[])
    {
    for(int i=0;i<NR_OF_GARDENS;i++)
    {
      cout<<"Please enter the address of garden "<<i+1<<":"<<endl;
      getline(cin,gardensP[i].Address,'\n');
      cout<<"Please enter the weekday that we will work at garden "<<i+1<<":"<<endl;
      getline(cin,gardensP[i].Day,'\n');
      cout<<"Please enter the number of workers for garden "<<i+1<<":"<<endl;
      cin>>gardensP[i].Workers;
      do{
      cout<<"Please indicate of the garden has a pool"<<endl;
      cin>>gardensP[i].Pool;
      }while (gardensP[i].Pool!='Y' && gardensP[i].Pool!='N');
    }  
    }
    Output looks something like this:

    Please enter the address of garden 1:
    Long Street 12
    Please enter the weekday that we will work at garden 1:
    Monday
    Please enter the number of workers for garden 1:
    4
    Please indicate of the garden has a pool
    Y
    Please enter the address of garden 2:
    Please enter the weekday that we will work at garden 2:
    Monday
    Please enter the number of workers for garden 2:
    6
    Please indicate of the garden has a pool
    N
    Please enter the address of garden 3:
    Please enter the weekday that we will work at garden 3:

    It just skips the Address question after the first one... What am i doing wrong?

    Thanks in advance

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    I could be wrong, but I think you need a cin.ignore() after you get the Y/N for a pool.

  3. #3
    Village id10t
    Join Date
    May 2008
    Posts
    57
    Thanks for the reply MacGyver, but that did not solve the problem... any other idea?

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    It probably didn't solve the problem because you didn't do it, or at least didn't do it properly.

    Case in point:

    Code:
    #include <iostream>
    
    int main()
    {
    	std::string s1, s2;
    	int num;
    	char yn;
    	
    	for(int i=0;i<3;i++)
    	{
    		std::cout << "Enter string 1: ";
    		getline(std::cin, s1, '\n');
    		std::cout << "Enter string 2: ";
    		getline(std::cin, s2, '\n');
    		std::cout << "Enter int 1: ";
    		std::cin >> num;
    		do
    		{
    			std::cout << "Enter [Y/N]: ";
    			std::cin >> yn;
    			std::cin.ignore();  /* Important */
    		}
    		while((yn != 'Y') && (yn != 'N'));
    	}
    	
    	return 0;
    }
    First iteration of output without the std::cin.ignore():

    Enter string 1: s1
    Enter string 2: s2
    Enter int 1: 1
    Enter [Y/N]: Y
    Enter string 1: Enter string 2: S2
    First iteration of output with the std::cin.ignore():

    Enter string 1: s1
    Enter string 2: s2
    Enter int 1: 1
    Enter [Y/N]: Y
    Enter string 1: S1

  5. #5
    Village id10t
    Join Date
    May 2008
    Posts
    57
    Ok so i have to put cin.ignore() in before the while to test the input of pool... It works now, but im dont quiet understand why... why is cin.ignore() so important?

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Because when you use std::cin to read a char, it reads a char and leaves the '\n' you entered in the stream. Then when getline() is called, it reads until it hits the '\n', which it does right away.

    Example:

    Code:
    cout<<"Please indicate of the garden has a pool"<<endl;
    cin>>gardensP[i].Pool;
    When this happens, and you type "Y" and press enter, this is the input stream:

    Code:
    'Y', '\n'
    std::cin will read the 'Y' char. So the input stream now looks like this:

    Code:
    '\n'
    You then come back to this in the loop:

    Code:
    cout<<"Please enter the address of garden "<<i+1<<":"<<endl;
    getline(cin,gardensP[i].Address,'\n');
    And getline() reads the '\n' in the stream and decides it reached the end of what you wanted.

    Placing std::cin.ignore() after reading the 'Y' char will read and discard the '\n' char.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 01-02-2009, 07:24 AM
  2. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  3. cin and getline() problems....
    By AdampqmabA in forum C++ Programming
    Replies: 3
    Last Post: 10-06-2008, 05:55 PM
  4. getline, while, string problems
    By Makachu in forum C++ Programming
    Replies: 7
    Last Post: 09-23-2007, 04:17 PM
  5. use of getline()
    By dat in forum C++ Programming
    Replies: 2
    Last Post: 11-14-2001, 01:02 AM