Help with Classes & Objects

Hey guys,this is my first attempt at writing Object-Oriented code, so please forgive me if some parts of it appear muddled, they probably are.

The problem I've been having is that the code does not output as expected. I have attempted to make a program with 4 books; each book is assigned a name and a page number by the user and that name and page number is then displayed.

This works fine for the first book, but once the program moves onto the second book it never asks for a book name to be input and therefore can't display the name of the book.

A sample output of the program follows:

1
2
3
4
5
6
7
8
Enter Book Name : First Book
Enter Page Number : 34
Book Name : First Book
Page Number : 34
Enter Book Name : Enter Page Number : 56
Book Name : 
Page Number : 56
Enter Book Name : Enter Page Number : 


As you can see, the process of entering the name for the first book works ok, but you cannot enter a name for the second book, and so on.


The code for my program itself is as follows:


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
46
47
48
49
50
51
52
53
54
55
56
  #include <iostream>
using namespace std;

class book

{
 private:
  string bookName;
  int pNum;
 public:
  void setName()
  {
    cout<<"Book Name : "<<bookName<<"\n";
  }
  string getName()
  {
    cout<<"Enter Book Name : ";
    getline(cin,bookName);
    return bookName;
  }
  void setPageNum()
  {
    cout<<"Page Number : "<<pNum<<"\n";
  }
  int getPageNum()
  {
    cout<<"Enter Page Number : ";
    cin>>pNum;
    return pNum;
  }

};

int main ()
{
  book book1, book2, book3, book4;
  book1.getName();
  book1.getPageNum();
  book1.setName();
  book1.setPageNum();
  book2.getName();
  book2.getPageNum();
  book2.setName();
  book2.setPageNum();
  book3.getName();
  book3.getPageNum();
  book3.setName();
  book3.setPageNum();
  book4.getName();
  book4.getPageNum();
  book4.setName();
  book4.setPageNum();

  return 0;
}


Any help or pointers would be very much appreciated! Thanks for your time.
It's a very common beginner mistake.

When you type some text and then press enter, pressing enter will enter a new line character '\n'.

If you type the book name "abc" and press enter, cin will contain "abc\n". The getline function reads until it finds a newline character (the newline character is discarded) so after the the book name has been read (line 18) cin will be empty.

If you then type the page number 56 and press enter cin will contain "56\n". The >> operator will read until the first non-digit character, but it will not discard the non-digit character so after the page number has been read (line 28) cin will contain "\n".

When the geline function is called next time it will find the newline character right away so there it will not have to wait. It simply returns right away leaving bookName empty (because there was no characters before the newline character).

What you should do is to make sure that you don't leave a newline character in cin after you have used operator >>. You can use cin.ignore();. That will ignore the next character, but the user might enter space or other characters after the page number before he press enter so to be safe you could use cin.ignore(numeric_limits<streamsize>::max(), '\n');. That will ignore the whole rest of the line.
Worked perfectly, thanks for your help Peter87!
Topic archived. No new replies allowed.