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!\n\n";
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 note\n[L] List notes\n[V] View note\n[E] Exit\nChoice: ";
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: