Thread: nu b q: overload and getline

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    20

    nu b q: overload and getline

    I'm having trouble with my istream overload (I think it's the istream). At the 2nd cin it doesn't reconize the getline for some reason. Any ideas?

    Also, is this the proper way to 'code tag' (I was chastised before is all).

    Code:
    istream& operator>>(istream& input, markets& m)
    {
    	cout<<"City name";
    	input.getline (m.city,20);:confused:
    	cout<<"Buy price of sheep"<<endl;
    	input>>m.shp_buy;
    	cout<<"Sell price of sheep"<<endl;
    	input>>m.shp_sell;
    	cout<<"Maxium sheep to trade"<<endl;
    	input>>m.shp_max;
    	return(input);
    };
    
    ostream& operator<<(ostream& oput, markets& m)
    {
    	oput<<m.city<<tab<<m.shp_buy<<tab<<m.shp_sell<<m.shp_max<<endl;
    	return(oput);
    };
    
    main ()
    {
    	markets city1;
    	markets city2;
    	markets city3;
    
    	cout<<endl<<endl<<endl<<endl<<endl;
    	cin>>city1;
    	cin>>city2;:confused:
    	cin>>city3;
    	cout<<endl<<endl<<endl;
    	cout<<"City"<<tab<<"Sheep Sell"<<tab<<"Sheep Buy"<<tab<<"Maxium Trade"<<endl;
    	cout<<city1;
    	cout<<city2;
    	cout<<city3;
    	return (0);
    }

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    Try putting input.ignore() after input>>m.shp_max. It should remove the '\n' left in the buffer which is probably being picked up in your next call to input.getline().

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    20
    Thanks, it seemed to work. Can I ask the why and how? What is ignore and why is there a '\n' in the buffer? Is the '\n' the return? Thanks again

  4. #4
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    Using operator >> leaves the final '\n' in the buffer, which is normally automatically extracted and ignored by the next call to operator >>. istream::getline() doesn't ignore a leading '\n' like operator >>, and treats it as the delimeter. Therefore you need to ensure there's no '\n' in the buffer before calling getline().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ifstream getline
    By jimboob in forum C++ Programming
    Replies: 2
    Last Post: 09-25-2004, 10:37 AM
  2. getline()
    By sweets in forum C++ Programming
    Replies: 4
    Last Post: 04-05-2004, 04:22 PM
  3. getline overload, solution didn't work
    By Clane in forum C++ Programming
    Replies: 7
    Last Post: 03-10-2002, 09:53 AM