Trouble using getline and using ostream& operator<<

I am currently having a little problem. I have all my code currently working except for when I go to output. I am using getline currently to have the user input their name which has a space in it and it inputs fine, but when I go to output it using ostream& operator<< it will not out put. All the text around it outputs and integers I use for a date also output fine but the strings will not. If I don't use getline and just cin it works fine, but then I can't have the user input anything with a space in it which I need them to be able to do.

Any ideas or help is appreciated. If you would like to see the code I can post it to but it's a little lengthy.
If you would like to see the code I can post it to but it's a little lengthy.

Try to post the smallest reproducible code snippet. It's hard to pinpoint the problem in such a specific scenario, without code.
Thanks for the reply.

This is where the user inputs their info
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
if (userInput == "add")
		{         
			

			cout << "Please input name of borrower:" << endl;
			getline(cin, name);
			cin.ignore(100, '\n');
			cout << "Please input borrowers DOB:" << endl;
			cin >> month >> day >> year;
			Date dob(month, day, year);
			cout << "Please input the book title:" << endl;
			getline(cin, title);
			cin.ignore(100, '\n');
			cout << "Please input the date that the book was borrowed:" << endl;
			cin >> month >> day >> year;
			Date borrow(month, day, year);

			TeamPerson Borrower(name, dob);

			
			newBook = new Books(title, Borrower, borrow);
			books.push_back(newBook);

		}


Here is the print function
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void PrintAllItems(vector<Books*> books) {
	int count = 0; // Loop index

	if (books.size() != 0)
	{
		// For each item call class member function to print
		for (count = 0; count < books.size(); count++) {
			cout << count << " - " << (*books.at(count));
		}
	}
	else
	{
		cout << "Theirs are no books currently being borrowed." << endl;
	}
	return;
}


I have 3 classes but this one has one of the problem spots
Books.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#ifndef BOOKS_H
#define BOOKS_H
#include<string>
#include<vector>
#include<ostream>
#include "Person.h"
#include<iostream>
using namespace std;
class Books
{
public:
	Books() {}; //default constructor, don’t implement it
	Books(string, TeamPerson, Date);//overloaded constructor
	TeamPerson get_person(); //return the person who borrowed the book
	string get_book();//return the name of the book
	friend ostream& operator<<(ostream& os, const Books &b);//print book info
	bool overdue(Date &current);//return true if the book is overdue


private:
	string b_name; //book name
	TeamPerson person; //person borrowing the book
	Date date; //date when the book was borrowed

};

#endif 


Books.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <string>
#include<ostream>
using namespace std;
#include "Date.h"
#include "Person.h"
#include "Books.h"

Books::Books(string n, TeamPerson p, Date d) :b_name(n), person(p), date(d) {}

TeamPerson Books::get_person() //return the person who borrowed the book
{
	return person;
}

string Books::get_book()//return the name of the book
{
	return b_name;
}

ostream& operator<<(ostream& os, const Books &b)//print book info
{
	os << "The book: " << b.b_name << " was checked out on: " << b.date << "By: " << b.person << endl;
	return os;
}


bool Books::overdue(Date &current)//return true if the book is overdue
{
	bool over = false;

	if (current.getYear() != date.getYear())
	{
		over = true;
	}
	else if (current.getMonth() != date.getMonth())
	{
		over = true;
	}
	


	return over;

}


If you look at

1
2
3
4
5
ostream& operator<<(ostream& os, const Books &b)//print book info
{
	os << "The book: " << b.b_name << " was checked out on: " << b.date << "By: " << b.person << endl;
	return os;
}


b.b_name will not output but d.date will look to my date class and works fine, and b.person will look to my person class which has the same problem where the date will output fine but the persons name will not.

If I change the are where the user inputs info to:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
if (userInput == "add")
		{         
			

			cout << "Please input name of borrower:" << endl;
			cin >> name;
			//getline(cin, name);
			//cin.ignore(100, '\n');
			cout << "Please input borrowers DOB:" << endl;
			cin >> month >> day >> year;
			Date dob(month, day, year);
			cout << "Please input the book title:" << endl;
			cin >> title;
			//getline(cin, title);
			//cin.ignore(100, '\n');
			cout << "Please input the date that the book was borrowed:" << endl;
			cin >> month >> day >> year;
			Date borrow(month, day, year);

			TeamPerson Borrower(name, dob);

			
			newBook = new Books(title, Borrower, borrow);
			books.push_back(newBook);

		}


The output works fine, but then I cant have them enter their fullname with a space or a book title with spaces
Last edited on
1
2
3
4
getline(cin, name);
cin.ignore(100, '\n');
cout << "Please input borrowers DOB:" << endl;
cin >> month >> day >> year;

cin.ignore() should be after formatted input (operator>>) and not after getline().

If the delimiter is found, it is extracted and discarded (i.e. it is not stored and the next input operation will begin after it).

http://www.cplusplus.com/reference/string/string/getline/
When you input the month/day/year all in one line, that input will be discarded.
Last edited on
Thank you so much!
Topic archived. No new replies allowed.