473,396 Members | 1,929 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 1929
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Haoyu Zhang | last post by:
Dear Friends, Python assignment is a reference assignment. However, I really can't explain the difference in the following example. When the object is a list, the assignment seems to be a...
2
by: Brian Roberts | last post by:
I'm confused about the use of hasattr/getattr, or possibly namespaces. I know how to do this: class UnderstandThis(object): def do_foo(self): pass def do_bar(self): pass def doit(self, cmd):...
11
by: Ohaya | last post by:
Hi, I'm trying to understand a situation where ASP seems to be "blocking" of "queuing" requests. This is on a Win2K Advanced Server, with IIS5. I've seen some posts (e.g.,...
6
by: ree32 | last post by:
I am a bit confused with capabilities of XML. I have an XML document with information on images(photos). Is there way to use XSL/XSLT to create a page that will display the images as gallery. ...
5
by: Jeff Amiel | last post by:
Yes, I've read the FAQ's... I'm still confused. I'm trying to help out a buddy to extract data from an .mdb file that has special 'permissions' on it. If I try to open it with the standard...
10
by: Lauren Wilson | last post by:
Ok I have searched the MS website for info on this. I am totally confused. If I want to deploy an Access 2003 app and allow my users to run it using Access 2003 Runtime, where do I get the...
1
by: Benny Ng | last post by:
Hi,All, Export Method: ------------------------------------------------------------------------- strFileNameExport = "Results" Response.Clear() Response.Buffer = True...
2
by: Daniel | last post by:
I'm new to .Net and all of its abilities so I hope this makes sense. Basically I'm confused on when is the appropriate time to use web forms controls vs. regular HTML. For example in ASP...
11
by: timmu | last post by:
Someone asked me a question about integer division and printf yesterday, I tell him he should do a casting to float/double before you do any interger division. But he doesn't think so, so I try...
2
by: Peter | last post by:
Hi, I have a problem with Listview using checkboxes. If i check items by code BEFORE the form is shown the Listview.Items are confused during the ItemChecked Event !!! After showing the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.