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 (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
#ifndef BOOKS_H
#define BOOKS_H
#include<string>
#include<vector>
#include<ostream>
#include "Person.h"
#include<iostream>
usingnamespace 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 ¤t);//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
#include <iostream>
#include <string>
#include<ostream>
usingnamespace 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 ¤t)//return true if the book is overdue
{
bool over = false;
if (current.getYear() != date.getYear())
{
over = true;
}
elseif (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: