1

Case 3 is an option to add a book to the structure. As long as books with titles without spaces are added, they are ok, whenever I try to put a name that has a space in it, the compiler goes crazy, sorta like what it would do if you execute an infinite loop. Why and what is the solution?

struct bookStruct
{
    string bookTitle;
    int bookPageN;
    int bookReview;
    float bookPrice;
};

const int MAX_BOOKS=10;



case 3:
        {
            for(int i=0;i<MAX_BOOKS;i++)
            {
                if(books[i].bookTitle=="\0")
                {
                cout << "\nPlease Enter the Title: ";
                cin >> books[i].bookTitle ;
                cout << "\nPlease Enter Total Number of Pages: ";
                cin >> books[i].bookPageN ;
                cout << "\nPlease Enter Rating (stars): ";
                cin >> books[i].bookReview ;
                cout << "\nPlease Enter Price: ";
                cin >> books[i].bookPrice;
                cout << "\n\nBook Added.\n\n";
                break;
                }
            }break;

        }
  • You've posted 3 questions regarding your code. Don't you think you should figure this stuff out on your own via searching like google or using a debugger? – Rapptz Feb 05 '13 at 04:25
  • The *compiler* doesn't do anything unusual. It is your program that the compiler created on your behalf that gets stuck. – Eric J. Feb 05 '13 at 04:25
  • @Rapptz I have trying that all day, with no luck and no valid answers from here either. –  Feb 05 '13 at 04:29
  • If you don't think none of the answers given to your questions are valid, or rather "acceptable", then you should not mark them as accepted. – Some programmer dude Feb 05 '13 at 04:32
  • By the way, comparing with `"\0"` is a bit redundant as `""` contains one `'\0'` character, and the former contains two. Even better is `if (books[i].bookTitle.empty())`. – chris Feb 05 '13 at 04:36
  • @chris I think the problem is in the loop, rather than the comparison. –  Feb 05 '13 at 04:45
  • The comparison's fine. I was merely commenting on the redundancy and suggesting the ideal way of checking whether a string is empty. – chris Feb 05 '13 at 04:49
  • Also see: http://stackoverflow.com/a/14312884/14065 – Martin York Feb 05 '13 at 06:21

1 Answers1

4

The input operator >> stops at space when reading strings.
What you want to use is std::getline.

cout << "\nPlease Enter the Title: ";
std::getline(std::cin, books[i].bookTitle);

The input operator >> when reading a number will also stop at a space or newline (leaving them on the input stream). Thus when you wrap around to the next book there is still a '\n' character on the input stream. So for numbers you also need to use std::getline(). But in this case you need to convert the value to an integer.

cout << "\nPlease Enter Total Number of Pages: ";
std::string line;
std::getline(std::cin, line);

std::stringstream linestream(line);
linestream >> books[i].bookPageN ;
Martin York
  • 257,169
  • 86
  • 333
  • 562
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • any suggestions on how to fix it? –  Feb 05 '13 at 04:28
  • 2
    @MokammelHossainSanju Read my answer again. – Some programmer dude Feb 05 '13 at 04:30
  • why this doesnt work and still is putting my code through an infinite loop when I use a space. –  Feb 05 '13 at 06:01
  • 1
    @MokammelHossainSanju: The reason it goes into an infinite loop is the stream becomes bad and there is a bug in some other part of your code that you have not shown. To fix the local problem see the updated answer. – Martin York Feb 05 '13 at 06:13
  • 1
    @MokammelHossainSanju: But you are doing it wrong. You should be defining an input operator for the class `bookStruct`. – Martin York Feb 05 '13 at 06:14