473,890 Members | 1,988 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

getline(cin, string) Question

Had this question from a student and confirmed the behavior in VC++ .NET
2003:
cout << "Please enter your name: ";
getline(cin,myN ame);
cin.ignore(100, '\n');

The only problem with this is I have to press "enter"
twice before the input is accepted. Is there a way to
eliminate the need for twice pressing "enter"?


I realize this has been around quite a while (research indicates since
VC5?), but is there a practical workaround that can be placed in the code?
If not, what are the alternatives?
Thanks!

--
*************** *************** *************** *************** **********
** All opinions are mine alone.Do not attribute elsewhere!
*************** *************** *************** *************** **********
** Peter van der Goes, Professor, Computers & Information Technology
** Rose State College
*************** ******** Contact *************** *************** ******
** Office e-mail: pv**@rose.edu
** Home e-mail: p_**********@co x.net
** Web Page: http://www.rose.edu/Faculty/pvan/index.htm
*************** *************** *************** *************** **********
Nov 16 '05 #1
1 4623
Peter van der Goes <p_**********@m ars.cox.net> wrote:
Had this question from a student and confirmed the behavior in VC++ .NET
2003:
cout << "Please enter your name: ";
getline(cin,myN ame);
cin.ignore(100, '\n');

The only problem with this is I have to press "enter"
twice before the input is accepted. Is there a way to
eliminate the need for twice pressing "enter"?
I realize this has been around quite a while (research indicates since
VC5?), but is there a practical workaround that can be placed in the code?
If not, what are the alternatives?


What's that 'ignore()' call for? Remove it
and you won't need to hit enter twice.
Thanks!

Schobi

--
Sp******@gmx.de is never read
I'm Schobi at suespammers dot org

"Sometimes compilers are so much more reasonable than people."
Scott Meyers
Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
3491
by: Sonoman | last post by:
I am trying to do this: cin >> temp; if (temp == "n"){ Then do something... } temp was declared as a string and the input I give at the prompt is n, but it skips the condition for the if statement when I think it should go into the if statement. I tried swithching to 'n' for the condition but it gave me
5
1897
by: Danny Anderson | last post by:
Hola- I didn't get any responses on a previous post, so I am trying to reword my problem and post compile-able code that exhibits the behavior I am describing. On the second iteration of the loop below, the file opened is the default (which in this case is ".csv", which makes a 'hidden' file for Linux folk). The step that prompts for a file name step is skipped completely. Why? Is this a cin issue or does it have something to do with...
18
8270
by: Amadeus W. M. | last post by:
I'm trying to read a whole file as a single string, using the getline() function, as in the example below. I can't tell what I'm doing wrong. Tried g++ 3.2, 3.4 and 4.0. Thanks! #include <iostream> #include <fstream> #include <cstdlib> #include <string>
1
1347
by: Jack | last post by:
Hi: Assume that the content of the file textfile.txt is: Have a good day. Hello world. I use the following code to access the file textfile.txt: #include <iostream>
1
3136
by: m3zmeriz3d | last post by:
i typed a function to enter data into a file to be recalled later, now i have done something here and as soon as this function begins it skips the first 'zips' getline and makes it a blank space and goes directly to 'city' - someone please tell me what the hell happenned here - g'day - mark void addData() { string zips = ""; string city = ""; ofstream outFile;
1
2427
by: l46kok | last post by:
Just wondering, for character of arrays, you can set the delimiter manually like cin.getline(character array, size, delimeter) But for the STL string, apparently, you have to use the following form of getline getline(cin, string)
11
427
by: Key9 | last post by:
Hi all This is an simple app : read cin and print to cout I want it's action like "cat" with out arguement. #include <iostream> #include <string> using namespace std;
4
5956
by: PulsarSL | last post by:
Hi I'm working on improving the "Simpletron" computer described in Deitel & Deitel's "C++ How to Program" 3rd edition. Check out http://www.lehigh.edu/~ejk0/smlspec.pas for a description if you're curious. I'm trying to add my own opcodes for string input/output using the string type from string.h
5
3633
by: erictheone | last post by:
so here is my code. My getlines for the strings keyword and phrase at lines 44 and 79 respectively don't work. Please help!!! #include <cstdlib> #include <string> #include <iostream> #include <fstream> using namespace std; string removeAllWhite( string eric) {
0
9978
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
11232
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
10819
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...
1
10923
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10462
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9633
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
8016
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...
1
4676
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
4271
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.