William S.

asked • 03/03/21

How to Call a Function from Another File and Then Print the Return of that Function? (C++)

Complete main() by inserting a new book into each list using the respective LinkedListLibrary and VectorLibrary InsertSorted() methods and outputting the number of operations the computer must perform to insert the new book. Each InsertSorted() returns the number of operations the computer performs.


Ex: If the input is:

The Catcher in the Rye
9787543321724
J.D. Salinger

the output is:

Number of linked list operations: 1
Number of vector operations: 1


Where I have been stumped on what to do with this assignment is how do I get the value of the counter, which is supposed to be increasing as operations are completed. When I compile and run the program all I get are 0 for both linked list operations and vector operations.


Am I calling the function in the other file the wrong way?


These are the lines of code that I wrote that are supposed to insert a new book and return the number of operations that it took to do so:


linkedListLibrary.InsertSorted(currNode, linkedListOperations);

vectorLibrary.InsertSorted(tempBook, vectorOperations);


Here is the bottom half of the code that I have so far (The part that I am having trouble with is near the bottom.)

// Insert into linked list
// No need to delete currNode, deleted by LinkedListLibrary destructor
currNode = new BookNode(bookTitle, bookAuthor, bookISBN);
// TODO: Call LL_Library's InsertSorted() method to insert currNode and return
// the number of operations performed
linkedListLibrary.InsertSorted(currNode, linkedListOperations);

linkedListLibrary.lastNode = currNode;

// Insert into VectorList
tempBook = Book(bookTitle, bookAuthor, bookISBN);
// TODO: Call VectorLibrary's InsertSorted() method to insert currNode and return
// the number of operations performed
vectorLibrary.InsertSorted(tempBook, vectorOperations);

// TODO: Print number of operations for linked list
cout << "Number of linked list operations: " << linkedListOperations << endl;

// TODO: Print number of operations for vector
cout << "Number of vector operations: " << vectorOperations << endl;

}

1 Expert Answer

By:

Patrick B. answered • 03/03/21

Tutor
4.7 (31)

Math and computer tutor/teacher

William S.

I set the int values linkedListOperations, and vectorOperations in the first half of the program to equal 0. I think my problem is that I am not passing the object correctly.
Report

03/03/21

Patrick B.

well Yes, you are going to have to pass it by address or by reference. Make sure that if the routine expects a pointer to... then you are passing the address with the ampersand&... for pass by reference, make sure there is an ampersand in the function header
Report

03/04/21

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.