String input Problems; out of things to try

Hey guys, I've run into a problem inputting a string with this code...

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <algorithm>

using namespace std;

int main()
{
bool terminate = true;
char option;
string title_final;
while(terminate == true)
{
cout << "A. Add new DVD to collection." << endl;
cout << "H. Nothing." << endl;
cout << "What would you like to do? (Please use only caps - A, B, C, D, E, F, G, or H) : " << endl;

cin >> option;
if(option == 'A')
{
//set title
cout << "Title: ";
getline(cin, title_final);

cout << title_final << endl;
}
if(option == 'H')
{
terminate = false;
}
}
system("pause");
return 0;
}

I don't know why getline(cin, title_final); isn't working. According to most sites it should... I'm a student so there must be something I'm just unable to catch. Does anyone see it?

Thanks
I've done some testing and getline(cin, title_final); only doesn't work when its in that loop that I have "while(terminate == true)". I have no clue why at the moment.
Ok I'm getting a bit closer to the source...

I'm testing this code now:

#include <iostream>
#include <string>

using namespace std;

int main()
{
string temp;
char a;
int b;

cout << "Option: ";
cin >> a;


if(a == 'a')
{
cout << "Enter temp: ";
cin >> temp;
getline(cin, temp);
}
if(a == 'b')
{
b = 1;
}


cout << temp << endl;

system("pause");
return 0;
}

I'm down to conditionals bugging me out.
I'm getting the full string except for the first word for some reason.
The problem is that cin >> temp; reads the next word and leaves everything after the word still inside the stream. If you pressed enter after writing the word there will be a new line character after the word so when you call getline it will find a new line character and return right away, giving you an empty string.

You need to get rid of the new line character before calling getline. You can use cin.ignore(); to remove one character. If there can be more characters before the new line character you can use cin.ignore(numeric_limits<streamsize>::max(), '\n'); to ignore the rest of the line.
1
2
3
cin >> temp;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
getline(cin, temp);
This code probably will look super unprofessional to you guys but it works for the purpose i need...

string temp;
string temp2;
string temp3;

cout << "Enter temp: ";
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cin >> temp;
getline(cin, temp2);

temp3 = temp + temp2;

cout << temp3 << endl;


I'm basically getting the first word from cin>> and then the rest gets stored from the getline function into temp2. And then I add the two into temp3 completing the whole string from 1 user input.

I tried peters way, thank you for you help, but it was causing me to have to input the string twice in order to store it into temp for some reason.

Anyway, I thought I would share even if it may be terrible coding, maybe someone more experienced can confirm that, and if not everyone's welcome to use it.
It works without cin.ignore(numeric_limits<streamsize>::max(), '\n');

so,

cin >> temp;
getline(cin, temp2);
temp3 = temp + temp2;
cout << temp3 << endl;

//and clear strings for later use
temp.clear();
temp2.clear();
temp3.clear();
Topic archived. No new replies allowed.