Cannot Compare String to String

Hi, I am having trouble with my code not being able to compare two strings. One is user entered, and the other is from a structure. My professor likes us to use only what I have displayed, I am just having trouble with comparing the strings.

This one works just fine:

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
void searchTitle(Book *&bptr, int &numberOfBooks) {
    string bookTitle;
    bool found = false;

    cout << "\nPlease enter the book Title: ";
    getline(cin,bookTitle);

    bookTitle = "\"" + bookTitle + "\"";

    cout << "\nAuthor || Title || ISBN || # of copies || Status\n\n====================\n";

    for(int x = 0; x < numberOfBooks; x++)
    {
        if(bookTitle == bptr[x].title)
        {
            cout << bptr[x].author << " || " << bptr[x].title << " || " << bptr[x].isbn << " || " << bptr[x].copies << " || " << bptr[x].status << endl;
            found = true;
        }
    }
    if(!found)
    {
        cout << "\nError...Book not found...\n\n";
    }

    cout << "\nPress Enter to continue...\n\n";
    cin.ignore();
}


However, this one does not work at all.

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
void searchAuthor(Book *&bptr, int &numberOfBooks) {
    string bookAuthor;
    bool found = false;

    cout << "\nPlease enter the book Author: ";
    getline(cin,bookAuthor);

    cout << "\nAuthor || Title || ISBN || # of copies || Status\n\n====================\n";

    for(int x = 0; x < numberOfBooks; x++)
    {
        if(bookAuthor == bptr[x].author)
        {
            cout << bptr[x].author << " || " << bptr[x].title << " || " << bptr[x].isbn << " || " << bptr[x].copies << " || " << bptr[x].status << endl;
            found = true;
        }
    }
    if(!found)
    {
        cout << "\nError...Book not found...\n\n";
    }

    cout << "\nPress Enter to continue...\n\n";
    cin.ignore();
}


Whenever I input the desired author, the error message is displayed. I have checked my spelling numerous times to see if I was incorrect, but I was not. I am not sure what the problem is.

Also, they should work just fine, since I copied and pasted the first code to the second code and changing "Title" to "Author". There is a third one in which the user can search with ISBN, and that is also able to work. I am just curious as to why only the Author code does not work.
Last edited on
Post the Book class.
Try printing out the value of bptr[x].author. Maybe it doesn't contain what you think it does.
@helios
Here is the Book structure, I did not use class.

1
2
3
4
5
6
7
struct Book {
   string title;
   string author;
   string isbn;
   string status;
   int copies;
};


@dhayden
I changed the searchAuthor code to display all authors. I had Jimmy Newtron as a test for easier memorization.

There are more authors, I just used a vertical ellipsis to shorten the list until Jimmy Newtron showed up.

This is the output.

Please enter the book Author: Jimmy Newtron

Author || Title || ISBN || # of copies || Status
====================
Paul Kalanithi

Yuval Noah Harari

.
.
.

Jimmy Newtron

Error...Book not found...

Press Enter to continue...


It is very interesting since I thought that maybe the spaces were messing with the input, however, there are titles with spaces in them and searchTitle code works just fine. Therefore, I am baffled as to why this one does not work.
Last edited on
I agree. I would find the location of the entry in the lookup table and print it, possibly in hex, and do the same for your input, and compare them by hand to see if there is a difference.

I suspect there is some slight difference and the exact match code of = is failing.

Or make a simpler entry into your x structure with name = 'bob' or something really easy and try that, see if it works ... build up figure out what is broken that way. Could be hiddne space on end of the strings, or the like...

in practice, on a search like this, you might want to do caseless comparison once you get it working. The user might enter in all caps or something.
Last edited on
@jonnin
I see, okay. I will try to compare them in a way different from literal string. Hopefully it will work.

EDIT: Thank you everyone that helped me. I believe changing from string to a different format actually worked. Woo hoo. Now I am actually done.
Last edited on
Glad you got it. Generally what I do here is drop all whitespace and upcase it then compare that. Here the chance of a mismatch from doing that is very low, but you could replace whitespace between valid chars with a | or similar character before comparison. So if you had 3 spaces in a row, collapse that to a single | character. The idea is cleanup and making the best chance to get your matches correctly.
I believe changing from string to a different format actually worked.
I
I'm glad your code works now, but do you know why it works now, or why it didn't before? It's possible that the change simply masked a bug.
This is the output.


Please enter the book Author: Jimmy Newtron

Author || Title || ISBN || # of copies || Status
====================
Paul Kalanithi

Yuval Noah Harari

.

That looks to me like the names are "Paul Kalanithi\n" and "Yuval Noah Harari\n". Maybe that's the problem.
If you are right @dhayden, I guess that could have happened using a char array rather than a std::string at some stage. For example, fgets() will include the newline as part of the string.
http://www.cplusplus.com/reference/cstdio/fgets/
Topic archived. No new replies allowed.