confused

B

bryant058

cout<<"-Please enter the spacing:";
cin>>space;
cout<<"-Please enter a string of length<70:"<<endl;
getline(cin,s);
 
A

anon

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??

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
 
O

osmium

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??

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.
 
J

James Kanze

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??

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...
}
 
M

Marcus Kwok

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??

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);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,301
Messages
2,571,549
Members
48,295
Latest member
JayKillian
Top