473,672 Members | 2,490 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

confused

cout<<"-Please enter the spacing:";
cin>>space;
cout<<"-Please enter a string of length<70:"<<en dl;
getline(cin,s);
-----------------------------------------------------------------
when executeing,afte r 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 1947
br*******@gmail .com wrote:
cout<<"-Please enter the spacing:";
cin>>space;
cout<<"-Please enter a string of length<70:"<<en dl;
getline(cin,s);
-----------------------------------------------------------------
when executeing,afte r 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*******@gmai l.comwrote:
cout<<"-Please enter the spacing:";
cin>>space;
cout<<"-Please enter a string of length<70:"<<en dl;
getline(cin,s);
-----------------------------------------------------------------
when executeing,afte r 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:"<<en dl;
getline(cin,s);
-----------------------------------------------------------------
when executeing,afte r 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::istringstr eam 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 objektorientier ter Datenverarbeitu ng
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:"<<en dl;
getline(cin,s);
-----------------------------------------------------------------
when executeing,afte r 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_li mits;
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(nume ric_limits<stre amsize>::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
5475
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 reference. However, when the object is a number, the story is so different. I don't understantd for what reason Python deals with them differently. The example is as follows: >>> a = >>> b = a
2
2614
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): funcname = 'do_' + cmd if hasattr(self, funcname): getattr(self, funcname)()
11
4943
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., http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&selm=Tidy7IDbDHA.2108%40cpmsftngxa06.phx.gbl) that indicate that ASP will queue up requests when they come in with the same "session".
6
1622
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. I am a bit confused as whether I have to run XT processor each time I update the XML file. Or is the does the web browser come with XT processor which will automatically load the xml document and transform it using an XSLT?
5
2787
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 system.mdw file, I get the "Current user account doesn't have permission to covert or enable this database".
10
3035
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 Runtime? I just purchased Office 2003 Professional. Is Access 2003 Runtime included with that or not? It APPEARS that the only way I can get Access 2003 Runtime is to
1
3258
by: Benny Ng | last post by:
Hi,All, Export Method: ------------------------------------------------------------------------- strFileNameExport = "Results" Response.Clear() Response.Buffer = True Response.ContentType ="application/vnd.ms-excel" 'application/msword
2
2055
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 (non-.Net) if I wanted to fill a list it may look something like this: -------START CODE <%
11
3909
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 to do some example to explain, However, after some trying, I confused when I try to do some integer constant division, I know I should do float division something like 3.0/6.0, but I'm still confused where the -0.124709 comes for the following...
2
6826
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 form every thing works fine: checking items by code
0
8485
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8403
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8930
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8828
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7446
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6238
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4227
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2819
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2062
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.