Hi! So I was asked to create a C++ program that will ask the user to input 5 books with the ISBN, Title of the book and author/s. It will ask for 3 authors, if the book has only one author, you should leave "author 2 and author 3" blank and display only one author. Thing is... I'm having a problem with the if else condition at the last part of my program. I cant seem to make it work. Thanks in advance!
Function calls use parenthesis () whereas accessing indexes of arrays use square brackets [], you've used the wrong kind for the strlen calls in your code.
Incorrect:
strlen[book[i].author2]
Correct:
strlen(book[i].author2)
And, as a helpful note, try using std::string when you're allowed to for assignments (I don't know if you're allowed to on this one).
Thanks for the help man! Just one last question, how will I hide "author of 2/3" if it is blank? Just want to display the one which has a value.
Enter ISBN of book 2:
sdfhgfhgf
Enter book 2:
hello
Enter author 1 of book 2:
john smith
Enter author 2 of book 2:
juan
Enter author 3 of book 2:
ISBN of book 1:vghhgfd
Title of book 1:hahaha hihihi
Author 1 of book 1:john grisham
Author 2:
Author 3:
ISBN of book 2:sdfhgfhgf
Title of book 2:hello
Author 1 of book 2:john smith
Author 2:juan
Author 3:
The issue may be how you indicate that the author is blank. When you type into the console do you have to type a space or something before it will stop making you press enter?
My professor told us that if there is only one author, just click enter when it asks for the two more authors then the program should display the one with a value.
my ouput:
Enter ISBN of book 2:
sdfhgfhgf
Enter book 2:
hello
Enter author 1 of book 2:
john smith
Enter author 2 of book 2:
juan
Enter author 3 of book 2:
ISBN of book 1:vghhgfd
Title of book 1:hahaha hihihi
Author 1 of book 1:john grisham
Author 2:
Author 3:
ISBN of book 2:sdfhgfhgf
Title of book 2:hello
Author 1 of book 2:john smith
Author 2:juan
Author 3:
my professor wants to have this output:
Enter ISBN of book 2:
sdfhgfhgf
Enter book 2:
hello
Enter author 1 of book 2:
john smith
Enter author 2 of book 2:
juan
Enter author 3 of book 2:
ISBN of book 1:vghhgfd
Title of book 1:hahaha hihihi
Author 1 of book 1:john grisham
ISBN of book 2:sdfhgfhgf
Title of book 2:hello
Author 1 of book 2:john smith
Author 2:juan
Try looking at your condition for printing it out.
if (strlen(book[i].author3)-1 > 1)
This is saying "if the length of the author string minus one is greater than 1"
This seems like it should work, but the issue is that the value returned by strlen is unsigned, so 0 minus 1 is 2^32-1, which is definitely greater than 1.
strlen() function returns size_t type. It's unsigned integer type. Unsigned numbers can't have negative values, for them 0 − 1 = <max possible number>. E.g. in case of unsigned char (if it has 8 bytes) it will be 0 − 1 = 255.
Now look at lines 39 and 47: if (strlen(book[i].author2)-1 > 1)
If strlen() returns 0, then 0 - 1 will be greater than 1!
Use common math and move -1 to the right: if (strlen(book[i].author2) > 2)
(BTW, why 2? Shouldn't it be 0?)