Help With Structs and Modules?

I'm not getting any errors in my program. This is a little incomplete but I just wanted to see if it would output one of the functions in the source file.

So I have two .cpp files
main.cpp and book.cpp

I have one header that holds the struct and a bunch of other functions and it's called book.h

so in my main i have
1
2
3
4
5
6
7
8
9
 
#include "book.h"
#include <iostream>

using namespace std;
int main()
{ 
 void InitializeBook(book bookInfo); 
} 


this is my header:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef BOOK_H_INCLUDED
#define BOOK_H_INCLUDED

#include <iostream>

struct book{
   std::string Title;
   int  Authors[3];
   std::string Genre;
   std::string Publisher;
};

void InitializeBook(book BookInfo);
int AddAuthor(book AuthorOne);
int RemoveLastAuthor(book AuthorNone);

#endif


As for now I'm just messing with the strings and not the authors.

In my book.cpp, I have:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "book.h"
#include <iostream>

using namespace std;

void InitializeBook(book bookInfo)
{


    bookInfo.Title = "The Killer Next Door.";
    bookInfo.Genre = "Mystery";
    bookInfo.Publisher = "Someone";

    cout<<"Title: "<<bookInfo.Title;
    cout<<"Genre: "<<bookInfo.Genre;
    cout<<"Publisher: "<<bookInfo.Publisher;
}


But everytime I build and run the program, nothing is displayed! I don't get any errors either. So what am I doing wrong?

In your main.cpp on line 8
void InitializeBook(book bookInfo);
Declaring a function is not the same as calling it.
and your functions do not pass by reference, so main won't see the changes the function made.

you need to put the & symbol like this:

void InitializeBook(book &bookInfo)
{
..code
}

otherwise, your function edits a copy of the object, and then throws the changes away. If you change it as I said, it changes the original.
Last edited on
@Thomas1965 so I'll say InitializeBook() in main but what will be the parameters inside the () ?
You need to create a book variable in main and pass it to InitializeBook like so.
1
2
3
4
5
int main()
{ 
  book bookInfo;
  InitializeBook(bookInfo); 
} 

Don't forget to make the little change jonnin mentioned.
Also your header should really only be #including <string> you don't need <iostream> inside that header but you do need the <string> header.

Gotcha. Thanks guys!
Topic archived. No new replies allowed.