Help with Structures.

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!


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
57
#include <iostream>
#include <cstring>
#define SIZE 5

using namespace std;
int i;
int main()

{
      struct books
      {
             char title[50];
             char isbn[14];
             char author1[50];
             char author2[50];
             char author3[50];
      } book[SIZE];
      system ("cls");
      
      for (i = 0; i < SIZE; i++)
      {
          cout << "Enter ISBN of book " << (i + 1) << ": " << endl; 
          cin.getline (book[i].isbn, 14);
          cout << "Enter book " << (i + 1) << ": " << endl; 
          cin.getline (book[i].title,50);
          cout << "Enter  author 1 of book " << (i + 1) << ": " << endl; 
          cin.getline (book[i].author1,50);
          cout << "Enter author 2 of book " << (i + 1) << ": " << endl; 
          cin.getline (book[i].author2,50);
          cout << "Enter author 3 of book " << (i + 1) << ": " << endl; 
          cin.getline (book[i].author3,50);
    }
    
    for (i = 0; i < SIZE; i++)
    {
        cout << "ISBN of book " << i+1 << ":" <<book[i].isbn << endl;
        cout << "Title of book " << i+1 << ":" << book[i].title << endl;
        cout << "Author 1 of book " << i+1 << ":" << book[i].author1 << endl;
        if (strlen[book[i].author2]-1 > 1)
        {
            cout << "Author 2:" << book[i].author2 << endl;
        }
        else
        {
            continue;
        }
        if (strlen[book[i].author3]-1 > 1)
        {
            cout << "Author 3:" << book[i].author3 << endl;
        }
        else
        {
            continue;
        }
    }
    system("pause");
}
You wrote strlen with square brackets, is that a typo?
nope. Sorry, not sure with the right way to do it
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).
Last edited on
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:
Last edited on
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.

Try this instead:
if(strlen(book[i].author3) > 0)
Last edited on
it worked!!! Thank you so much man!
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?)
Last edited on
Topic archived. No new replies allowed.