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;








0















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:









share|improve this question



















  • 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











  • @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 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

















0















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:









share|improve this question



















  • 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











  • @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 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













0












0








0


1






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:









share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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











  • 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











  • @JonathanLeffler So what do you suppose I do exactly?

    – Nolan D.
    Mar 25 at 6:11












  • 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











  • @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 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







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












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
);



);













draft saved

draft discarded


















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















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해