C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Comment lire simplement une ligne sur le flot d'entrée cin

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ (French)
View previous topic :: View next topic  
Author Message
Eric Bart
Guest





PostPosted: Fri May 21, 2004 12:49 pm    Post subject: Comment lire simplement une ligne sur le flot d'entrée cin Reply with quote



Bonjour,

j'ai trouvé ça dans le fichier "/usr/include/c++/3.3.1/iostream" :

get(__streambuf_type& __sb)
{ return this->get(__sb, this->widen('n')); }

Comment l'utiliser avec une simple string ?

Merci




Back to top
Jean-Marc Bourguet
Guest





PostPosted: Fri May 21, 2004 12:53 pm    Post subject: Re: Comment lire simplement une ligne sur le flot d'entrée Reply with quote



"Eric Bart" <eb-adm (AT) eric-bart (DOT) pasdepubmerci.net> writes:


#include <iostream>
#include <string>

std::string s;
std::getline(std::cin, s);
if (!std::cin) {
// probleme de lecture a traiter
} else {
...
}

A+

--
Jean-Marc
FAQ de fclc++: http://www.cmla.ens-cachan.fr/~dosreis/C++/FAQ
C++ FAQ Lite en VF: http://www.ifrance.com/jlecomte/c++/c++-faq-lite/index.html
Site de usenet-fr: http://www.usenet-fr.news.eu.org
Back to top
giova
Guest





PostPosted: Fri May 21, 2004 1:06 pm    Post subject: Re: Comment lire simplement une ligne sur le flot d'entrée Reply with quote



Jean-Marc Bourguet wrote:

Quote:
"Eric Bart" <eb-adm (AT) eric-bart (DOT) pasdepubmerci.net> writes:


#include <iostream
#include
std::string s;
std::getline(std::cin, s);
if (!std::cin) {
// probleme de lecture a traiter
} else {
...
}

A+

ou bien plus simple a coder mais pose probleme si le texte saisie

comprend des séparateurs tel que caratere espace, tabulation, etc...

#include #include <string>

using namespace std; //evite d'avoir a taper std:: a tout bout de champ Wink
string s;
cout<<"tape un truc puis touche entrée : ";
cin>>s;

Back to top
Eric Bart
Guest





PostPosted: Fri May 21, 2004 2:18 pm    Post subject: Re: Comment lire simplement une ligne sur le flot d'entrée Reply with quote

Ouais super. Merci.


Back to top
Michel Michaud
Guest





PostPosted: Fri May 21, 2004 2:20 pm    Post subject: Re: Comment lire simplement une ligne sur le flot d'entrée Reply with quote

Dans news:pxbn0426iwj.fsf (AT) news (DOT) bourguet.org, Jean-Marc
Bourguet <jm (AT) bourguet (DOT) org> a écrit :
Quote:
std::getline(std::cin, s);
if (!std::cin) {
// probleme de lecture a traiter
} else {
...
}

Il me semble que l'idiome consacré (qui, je le sais,
revient strictement au même) serait plutôt

if ( ! getline(cin, s))
{
// Problème de lecture à traiter
...
}
else
{
// s a été lu...
...
}

ou encore

if (getline(cin, s))
{
// s a été lu...
...
}
else
{
// Problème de lecture à traiter
...
}

Il y a là une vision d'un getline indiquant sa réussite,
alors que if (!cin) me paraît plus artificiel et plus C
que C++. Dans les deux cas, c'est en fait cin.fail() qui
est testé indirectement. J'accepterais d'ailleurs plus
volontier if (cin.fail()) que if (!cin)...

(J'espère ne pas ouvrir ici une guerre de religion !)

--
Michel Michaud [email]mm (AT) gdzid (DOT) com[/email]
http://www.gdzid.com
FAQ de fr.comp.lang.c++ :
http://www.cmla.ens-cachan.fr/~dosreis/C++/FAQ/


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ (French) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.