469,085 Members | 1,042 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,085 developers. It's quick & easy.

confused

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

Apr 2 '07 #1
4 1665
br*******@gmail.com wrote:
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
Apr 2 '07 #2
<br*******@gmail.comwrote:
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.
Apr 2 '07 #3
On Apr 2, 3:36 pm, bryant...@gmail.com wrote:
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...
}

--
James Kanze (GABI Software) email:ja*********@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

Apr 3 '07 #4
br*******@gmail.com wrote:
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);
}

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Apr 3 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Haoyu Zhang | last post: by
2 posts views Thread by Brian Roberts | last post: by
6 posts views Thread by ree32 | last post: by
5 posts views Thread by Jeff Amiel | last post: by
2 posts views Thread by Daniel | last post: by
11 posts views Thread by timmu | last post: by
reply views Thread by kglaser89 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.