Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys,
my problem is when I was trying to enter a book name, I won't success, codes like below:
C#
class SalseItem
{
    friend istream& operator>>(istream& is,SalseItem& si){
        cout<<"Please input the book name:"<<endl;
        is>>si.name;
        cout<<"Please input the number:"<<endl;
        is>>si.num;
        cout<<"Please enter the price:"<<endl;
        is>>si.price;
        if(!is)
           si = SalesItem();//default constructor
        return is;
    }
    ...
}

int main(int argc, char* argv[])
{
	SalseItem si;
	cin>>si;//I enter "The code project" + key_return,then si.name == "The" ,not "The code project";
	cout<<si;
	return 0;
}


How can I achieve the job ,inputing exactly "The code project" to a si.name?

[edit]Spurious code blocks removed - OriginalGriff[/edit]
Posted
Updated 25-Mar-11 21:32pm
v2
Comments
Sandeep Mewara 26-Mar-11 2:49am    
Not clear. Can you elaborate a little?

If you look at the definition for istream>>[^], you will see the problem:
"Extraction ends when the next character is either a valid whitespace or a null character, or if the End-Of-File is reached."

You may want to use getline[^] instead.
 
Share this answer
 
Comments
Alain Rist 26-Mar-11 3:46am    
You were quicker :)
OriginalGriff 26-Mar-11 3:50am    
...but I'm not sure the OP understood either of us...
:laugh:
scu_sundy 26-Mar-11 3:52am    
Thank you griff!
Shall we try some kind of compartmentation symbol change, will istream accept space after any settings?
OriginalGriff 26-Mar-11 3:56am    
AFAIK, no - and the method you added on Alains' reply is the same as using getline...
scu_sundy 26-Mar-11 4:08am    
Not so, if I use 'getline' like he had presented below, I had to click enter twice, it's not what I need.
Hi,
When you write is>>si.name; cin extraction stops reading as soon as it finds any blank space character.
In order to get entire lines, use the function getline:
class SalseItem
{
    friend istream&
    operator>>(istream& is,SalseItem& si)
    {
        cout<<"Please input the book name:"<<endl;
        getline (cin, si.name);
        // ...

cheers,
AR
 
Share this answer
 
Comments
scu_sundy 26-Mar-11 3:47am    
I'm afraid the input will never go on.
OriginalGriff 26-Mar-11 3:51am    
...And you gave an example! 5!
scu_sundy 26-Mar-11 3:55am    
I found a way like below, space will be accepted, key of return will end my input:
char c;
while (!is.eof())
{
if((c = is.get()) != '\n')
si.name += c;
else
break;
}
Emilio Garavaglia 26-Mar-11 4:03am    
That's exactly what getline does
scu_sundy 26-Mar-11 4:12am    
Hi Emilio, I had tried Alain's way just now, but I had to hit enter twice to end inputing a book name,this was not nice to me.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900