std::getline used twice in code, skips to second getlineWhy does std::getline() skip input after a formatted extraction?C++ — getline, and cin ignore () .deleting first characters in strings on outputHow to concatenate a std::string and an int?What's the best way to trim std::string?How to convert std::string to lower case?How to convert a std::string to const char* or char*?How can I profile C++ code running on Linux?std::wstring VS std::stringWhy is “using namespace std;” considered bad practice?getline issues with cin.ignoreskipping over my last cin inputProgram is saving every time the user presses enter, and spitting it out when cin is called
How is linear momentum conserved in circular motion?
How to prevent cables getting intertwined
I have found ports on my Samsung smart tv running a display service. What can I do with it?
Using roof rails to set up hammock
Time at 1G acceleration to travel 100 000 light years
Why do you need to heat the pan before heating the olive oil?
Common Marsupials and Rare Antelopes
How to avoid offending original culture when making conculture inspired from original
Will users know a CardView is clickable?
Can a character with the Polearm Master feat make an opportunity attack against an invisible creature that enters their reach?
Why was New Asgard established at this place?
Do Battery Electrons Only Move If There is a Positive Terminal at the End of the Wire?
How did space travel spread throughout the Star Wars galaxy?
Do my partner and son need an SSN to be dependents on my taxes?
Is this broken pipe the reason my freezer is not working? Can it be fixed?
I wish, I yearn, for an answer to this riddle
My student in one course asks for paid tutoring in another course. Appropriate?
Is using Legacy mode is a bad thing to do?
Digital signature that is only verifiable by one specific person
How can I prevent a user from copying files on another hard drive?
How useful is the GRE Exam?
What does this Swiss black on yellow rectangular traffic sign with a symbol looking like a dart mean?
You may find me... puzzling
What kind of chart is this?
std::getline used twice in code, skips to second getline
Why does std::getline() skip input after a formatted extraction?C++ — getline, and cin ignore () .deleting first characters in strings on outputHow to concatenate a std::string and an int?What's the best way to trim std::string?How to convert std::string to lower case?How to convert a std::string to const char* or char*?How can I profile C++ code running on Linux?std::wstring VS std::stringWhy is “using namespace std;” considered bad practice?getline issues with cin.ignoreskipping over my last cin inputProgram is saving every time the user presses enter, and spitting it out when cin is called
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
In my program which is for storing notes, I use std::getline twice to ask the user first for the title of the note, then to ask for the note's body. When running the program, it skips the first getline and goes straight to the second one.
I've tried std::cin, cin.ignore(), cin.sync(), and std::getline of course.
I have three files total. A header and class file, and a main file.
For showing what code matters, I'm only showing snippets of the class file and main file where the problem is involved at.
notebook.cpp
// -- snip --
Note Note::create_note()
Note s;
std::string title;
std::string note;
std::cout << "nPlease enter the note's title: n";
std::getline(std::cin, title);
s.set_title(title);
std::cout << "Please enter the note: ";
std::getline(std::cin, note);
s.set_body(note);
std::cout << "Note added!nn";
return s;
// -- snip --
tuffynotes.cpp
// -- snip --
char choice;
Note n;
// For storing up to 100 notes.
Note notes[100];
int size = 0;
// used for looping until user is done
bool flag = true;
int main()
while (flag)
std::cout << "Welcome to TuffyNotes!n";
std::cout << "[C] Create a noten[L] List notesn[V] View noten[E] ExitnChoice: ";
std::cin >> choice;
std::cout << "n";
switch (choice)
case 'C':
case 'c':
notes[size] = n.create_note();
size++;
break;
// -- snip --
The output says
Please enter the note's title:
Please enter the note:
when it should first allow me to input the title before skipping straight to asking for input for the body.
Please enter the note's title:
c++ cin
|
show 9 more comments
In my program which is for storing notes, I use std::getline twice to ask the user first for the title of the note, then to ask for the note's body. When running the program, it skips the first getline and goes straight to the second one.
I've tried std::cin, cin.ignore(), cin.sync(), and std::getline of course.
I have three files total. A header and class file, and a main file.
For showing what code matters, I'm only showing snippets of the class file and main file where the problem is involved at.
notebook.cpp
// -- snip --
Note Note::create_note()
Note s;
std::string title;
std::string note;
std::cout << "nPlease enter the note's title: n";
std::getline(std::cin, title);
s.set_title(title);
std::cout << "Please enter the note: ";
std::getline(std::cin, note);
s.set_body(note);
std::cout << "Note added!nn";
return s;
// -- snip --
tuffynotes.cpp
// -- snip --
char choice;
Note n;
// For storing up to 100 notes.
Note notes[100];
int size = 0;
// used for looping until user is done
bool flag = true;
int main()
while (flag)
std::cout << "Welcome to TuffyNotes!n";
std::cout << "[C] Create a noten[L] List notesn[V] View noten[E] ExitnChoice: ";
std::cin >> choice;
std::cout << "n";
switch (choice)
case 'C':
case 'c':
notes[size] = n.create_note();
size++;
break;
// -- snip --
The output says
Please enter the note's title:
Please enter the note:
when it should first allow me to input the title before skipping straight to asking for input for the body.
Please enter the note's title:
c++ cin
2
Groovy. Thanks. You've run up against Why does std::getline() skip input after a formatted extraction? You have to be very careful when mixing>>andgetline. If you needgetline, it's generally better to only usegetlineand then parse the line into the stuff you want.std::stringstreamoften helps here.
– user4581301
Mar 25 at 5:23
@user4581301 I'm assuming that you want me to replace the last two remaining cin statements in my code with getline statements. When I did this, I received an error. The cin statements were for an integer and for a character, and now they're giving m errors.
– Nolan D.
Mar 25 at 5:38
Please make minimal reproducible examples. One for the already shown code (it could use some work on the "minimal" and one for the modified code. This also servers the general principle to add information to the question instead of hiding it in comments.
– Yunnosch
Mar 25 at 5:59
When you entered the value read intochoice, the newline was left in the input buffer. The first call togetlinereads that newline, and is happy because it reached a newline. You are less happy, though.
– Jonathan Leffler
Mar 25 at 6:06
@JonathanLeffler So what do you suppose I do exactly?
– Nolan D.
Mar 25 at 6:11
|
show 9 more comments
In my program which is for storing notes, I use std::getline twice to ask the user first for the title of the note, then to ask for the note's body. When running the program, it skips the first getline and goes straight to the second one.
I've tried std::cin, cin.ignore(), cin.sync(), and std::getline of course.
I have three files total. A header and class file, and a main file.
For showing what code matters, I'm only showing snippets of the class file and main file where the problem is involved at.
notebook.cpp
// -- snip --
Note Note::create_note()
Note s;
std::string title;
std::string note;
std::cout << "nPlease enter the note's title: n";
std::getline(std::cin, title);
s.set_title(title);
std::cout << "Please enter the note: ";
std::getline(std::cin, note);
s.set_body(note);
std::cout << "Note added!nn";
return s;
// -- snip --
tuffynotes.cpp
// -- snip --
char choice;
Note n;
// For storing up to 100 notes.
Note notes[100];
int size = 0;
// used for looping until user is done
bool flag = true;
int main()
while (flag)
std::cout << "Welcome to TuffyNotes!n";
std::cout << "[C] Create a noten[L] List notesn[V] View noten[E] ExitnChoice: ";
std::cin >> choice;
std::cout << "n";
switch (choice)
case 'C':
case 'c':
notes[size] = n.create_note();
size++;
break;
// -- snip --
The output says
Please enter the note's title:
Please enter the note:
when it should first allow me to input the title before skipping straight to asking for input for the body.
Please enter the note's title:
c++ cin
In my program which is for storing notes, I use std::getline twice to ask the user first for the title of the note, then to ask for the note's body. When running the program, it skips the first getline and goes straight to the second one.
I've tried std::cin, cin.ignore(), cin.sync(), and std::getline of course.
I have three files total. A header and class file, and a main file.
For showing what code matters, I'm only showing snippets of the class file and main file where the problem is involved at.
notebook.cpp
// -- snip --
Note Note::create_note()
Note s;
std::string title;
std::string note;
std::cout << "nPlease enter the note's title: n";
std::getline(std::cin, title);
s.set_title(title);
std::cout << "Please enter the note: ";
std::getline(std::cin, note);
s.set_body(note);
std::cout << "Note added!nn";
return s;
// -- snip --
tuffynotes.cpp
// -- snip --
char choice;
Note n;
// For storing up to 100 notes.
Note notes[100];
int size = 0;
// used for looping until user is done
bool flag = true;
int main()
while (flag)
std::cout << "Welcome to TuffyNotes!n";
std::cout << "[C] Create a noten[L] List notesn[V] View noten[E] ExitnChoice: ";
std::cin >> choice;
std::cout << "n";
switch (choice)
case 'C':
case 'c':
notes[size] = n.create_note();
size++;
break;
// -- snip --
The output says
Please enter the note's title:
Please enter the note:
when it should first allow me to input the title before skipping straight to asking for input for the body.
Please enter the note's title:
c++ cin
c++ cin
edited Mar 25 at 6:34
Nolan D.
asked Mar 25 at 4:47
Nolan D.Nolan D.
156
156
2
Groovy. Thanks. You've run up against Why does std::getline() skip input after a formatted extraction? You have to be very careful when mixing>>andgetline. If you needgetline, it's generally better to only usegetlineand then parse the line into the stuff you want.std::stringstreamoften helps here.
– user4581301
Mar 25 at 5:23
@user4581301 I'm assuming that you want me to replace the last two remaining cin statements in my code with getline statements. When I did this, I received an error. The cin statements were for an integer and for a character, and now they're giving m errors.
– Nolan D.
Mar 25 at 5:38
Please make minimal reproducible examples. One for the already shown code (it could use some work on the "minimal" and one for the modified code. This also servers the general principle to add information to the question instead of hiding it in comments.
– Yunnosch
Mar 25 at 5:59
When you entered the value read intochoice, the newline was left in the input buffer. The first call togetlinereads that newline, and is happy because it reached a newline. You are less happy, though.
– Jonathan Leffler
Mar 25 at 6:06
@JonathanLeffler So what do you suppose I do exactly?
– Nolan D.
Mar 25 at 6:11
|
show 9 more comments
2
Groovy. Thanks. You've run up against Why does std::getline() skip input after a formatted extraction? You have to be very careful when mixing>>andgetline. If you needgetline, it's generally better to only usegetlineand then parse the line into the stuff you want.std::stringstreamoften helps here.
– user4581301
Mar 25 at 5:23
@user4581301 I'm assuming that you want me to replace the last two remaining cin statements in my code with getline statements. When I did this, I received an error. The cin statements were for an integer and for a character, and now they're giving m errors.
– Nolan D.
Mar 25 at 5:38
Please make minimal reproducible examples. One for the already shown code (it could use some work on the "minimal" and one for the modified code. This also servers the general principle to add information to the question instead of hiding it in comments.
– Yunnosch
Mar 25 at 5:59
When you entered the value read intochoice, the newline was left in the input buffer. The first call togetlinereads that newline, and is happy because it reached a newline. You are less happy, though.
– Jonathan Leffler
Mar 25 at 6:06
@JonathanLeffler So what do you suppose I do exactly?
– Nolan D.
Mar 25 at 6:11
2
2
Groovy. Thanks. You've run up against Why does std::getline() skip input after a formatted extraction? You have to be very careful when mixing
>> and getline. If you need getline, it's generally better to only use getline and then parse the line into the stuff you want. std::stringstream often helps here.– user4581301
Mar 25 at 5:23
Groovy. Thanks. You've run up against Why does std::getline() skip input after a formatted extraction? You have to be very careful when mixing
>> and getline. If you need getline, it's generally better to only use getline and then parse the line into the stuff you want. std::stringstream often helps here.– user4581301
Mar 25 at 5:23
@user4581301 I'm assuming that you want me to replace the last two remaining cin statements in my code with getline statements. When I did this, I received an error. The cin statements were for an integer and for a character, and now they're giving m errors.
– Nolan D.
Mar 25 at 5:38
@user4581301 I'm assuming that you want me to replace the last two remaining cin statements in my code with getline statements. When I did this, I received an error. The cin statements were for an integer and for a character, and now they're giving m errors.
– Nolan D.
Mar 25 at 5:38
Please make minimal reproducible examples. One for the already shown code (it could use some work on the "minimal" and one for the modified code. This also servers the general principle to add information to the question instead of hiding it in comments.
– Yunnosch
Mar 25 at 5:59
Please make minimal reproducible examples. One for the already shown code (it could use some work on the "minimal" and one for the modified code. This also servers the general principle to add information to the question instead of hiding it in comments.
– Yunnosch
Mar 25 at 5:59
When you entered the value read into
choice, the newline was left in the input buffer. The first call to getline reads that newline, and is happy because it reached a newline. You are less happy, though.– Jonathan Leffler
Mar 25 at 6:06
When you entered the value read into
choice, the newline was left in the input buffer. The first call to getline reads that newline, and is happy because it reached a newline. You are less happy, though.– Jonathan Leffler
Mar 25 at 6:06
@JonathanLeffler So what do you suppose I do exactly?
– Nolan D.
Mar 25 at 6:11
@JonathanLeffler So what do you suppose I do exactly?
– Nolan D.
Mar 25 at 6:11
|
show 9 more comments
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55331401%2fstdgetline-used-twice-in-code-skips-to-second-getline%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55331401%2fstdgetline-used-twice-in-code-skips-to-second-getline%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Groovy. Thanks. You've run up against Why does std::getline() skip input after a formatted extraction? You have to be very careful when mixing
>>andgetline. If you needgetline, it's generally better to only usegetlineand then parse the line into the stuff you want.std::stringstreamoften helps here.– user4581301
Mar 25 at 5:23
@user4581301 I'm assuming that you want me to replace the last two remaining cin statements in my code with getline statements. When I did this, I received an error. The cin statements were for an integer and for a character, and now they're giving m errors.
– Nolan D.
Mar 25 at 5:38
Please make minimal reproducible examples. One for the already shown code (it could use some work on the "minimal" and one for the modified code. This also servers the general principle to add information to the question instead of hiding it in comments.
– Yunnosch
Mar 25 at 5:59
When you entered the value read into
choice, the newline was left in the input buffer. The first call togetlinereads that newline, and is happy because it reached a newline. You are less happy, though.– Jonathan Leffler
Mar 25 at 6:06
@JonathanLeffler So what do you suppose I do exactly?
– Nolan D.
Mar 25 at 6:11