Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

confused

1 view
Skip to first unread message

brya...@gmail.com

unread,
Apr 2, 2007, 9:36:25 AM4/2/07
to
cout<<"-Please enter the spacing:";
cin>>space;
cout<<"-Please enter a string of length<70:"<<endl;
getline(cin,s);
-----------------------------------------------------------------
when executeing,after i key in space=2,
the program show "-Please enter a string of length<70:"
then it stop,
why can't i continuing key in the string??

anon

unread,
Apr 2, 2007, 9:42:38 AM4/2/07
to

How did you compile it??? I keep getting:
$ g++ test.cpp -o test
test.cpp:1: error: expected constructor, destructor, or type conversion
before ‘<<’ token
test.cpp:2: error: expected constructor, destructor, or type conversion
before ‘>>’ token
test.cpp:3: error: expected constructor, destructor, or type conversion
before ‘<<’ token
test.cpp:4: error: expected constructor, destructor, or type conversion
before ‘(’ token

osmium

unread,
Apr 2, 2007, 10:09:50 AM4/2/07
to
<brya...@gmail.com> wrote:

That is not a program, it is just an out of context fragment. Perhaps you
want to embed part or all of this is a loop?
Look up the while and for statements. Note that what the user (you) types
is put in a buffer and your program does not even see the buffer until you
press the Enter key.


James Kanze

unread,
Apr 3, 2007, 5:35:31 AM4/3/07
to

A little bit more context would be helpful, but if this is the
entire contents of your main, the program probably will not wait
for user input after the second prompt. None of the formatting
extractors (the >> operators) read trailing white space, so at
the very least, the '\n' at the end of the first line will still
be in the stream when you do the getline.

As a general rule, mixing reading with >> and getline doesn't
work too well. (The one exception is when you use getline to
read the remainder of the line you've partially parsed.) What
you probably want to do is something like:

std::cout << "-Please enter the spacing: " ;
std::cout.flush() ;
getline( line ) ;
std::istringstream s( line ) ;
if ( ! (s >> space >> std::ws) || s.get() != EOF ) {
// Error...
} else {
std::cout << "-Please enter a string of length < 70: " ;
std::cout.flush() ;
getline( line ) ;
// process string...
}

--
James Kanze (GABI Software) email:james...@gmail.com
Conseils en informatique orientee objet/
Beratung in objektorientierter Datenverarbeitung
9 place Semard, 78210 St.-Cyr-l'Ecole, France, +33 (0)1 30 23 00 34

Marcus Kwok

unread,
Apr 3, 2007, 2:12:07 PM4/3/07
to

As James Kanze explained in his post, after doing "cin >> space;", the
'\n' is probably still sitting in your input stream, so that the
getline() reads that instead of waiting for you to type something extra.
He gave you one way to deal with it; here is another:


#include <iostream>
#include <string>
#include <limits>

int main()
{
using std::cin;
using std::cout;
using std::getline;
using std::numeric_limits;
using std::streamsize;
using std::string;

cout << "-Please enter the spacing: ";

cout.flush();
int space;
cin >> space;

// add the following line to ignore everything until the newline
cin.ignore(numeric_limits<streamsize>::max(), '\n');

cout << "-Please enter a string of length<70:\n";
cout.flush();
string s;
getline(cin, s);
}

--
Marcus Kwok
Replace 'invalid' with 'net' to reply

0 new messages