IMHO, getline in C++ is not that "clean". Please have a look at this:

#include <iostream>

using namespace std;

int main()
{
     int i;
     string s;

     cin >> i;
     getline(cin, s);
     cout << i << endl;
     cout << s << endl;
     return 0;
}

With this input:
1
Hello

What output will you expect? and what is the real output?

I think scanf is better in such situations.


在 2009-9-5,下午1:46, Carlos Guia 写道:

> I'm not a 100% sure, so who likes this will have to test it before a  
> real contest, but I think you can eat the '\n' like this:
>
> char str[100];
> scanf("%[^\n]s\n",str);
>
> Anyway, I think the cleanest way is to either use getline or fgets.
>
> Carlos Guía
>
>
> On Sat, Sep 5, 2009 at 12:57 AM, Aditya V Daga <[email protected]>  
> wrote:
> We can also use
>
> char str[100];
> scanf("%[^\n]s",str);getchar();
>
> (getchar() used for eating '\n' charater)
>
> On Sat, Sep 5, 2009 at 1:20 AM, vexorian <[email protected]> wrote:
>
> string x;
> getline(cin, x);
>
> Also:
>
> char meh[100];
> gets(meh);
>
> null terminated strings are a bad habit if you plan making apps with  
> C+
> + later . I think though that there is a safer version of gets that
> takes a limit for the number of characters...
>
>
> On Sep 3, 10:47 pm, 有D <[email protected]> wrote:
> > hey, I am not very famliar with C++.
> > Can any one tell me how to acquire one line(including space) from  
> the
> > console conveniently?
> > I used the scanf("%s", &str);
> > but it will be cut off if there is any space in your input.
> > for example, if you input "hello  world"
> > than the world is just omited. str = hello;
> >
> > --
> > 有D
>
>
> >


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"google-codejam" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/google-code?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to