Hello,

I am still working on the self study thing. I have decided to do or attempt to do a project that requires a header files with 2 other source files. The header file was given in the book. It is as follows:

#ifndef GOLF_H_
#define GOLF_H_

//golf.h -- for pe9-1.cpp

const int Len=40;
struct golf
{
char fullname[Len];
int handicap;
};

// function sets golf structure to provided name, handicap
// using values passed as arguments to the function
void setgolf(golf & g, const char*name, int hc);

// function solicits name and handicap from user
// and sets the numbers of g to the values entered
// returns 1 if name is entered, 0 if name is empty string
int setgolf(golf & g);

// function resets handicap to new value
void handicap(golf & g, int hc);

//function displays contents of golf structure
void showgolf(const golf & g);

#endif

As far as I can tell the next file should ask for input and include main. I have gotten some of it done already.

#include <iostream>
#include "golf.h"
using namespace std;
int main()
{
char fullname[Len];
int handicap;

cout <<"Please enter your name and handicap: ";
while (cin >> fullname >> handicap)
{

One question that I have is what should come after while? The instructions in the book aren't as clear as I'd like.

The instructions are as follows:

Put together a multifile program based on the header: One file, named golf.cpp, should provide suitable function definitions to match the prototypes in the header file. A second file should contain main() and demonstrate all the features of the prototyped functions. For example, a loop should solicit input for an array of golf structures and terminate when the array is full or the user enters an empty string for the golfer's name. The main() function should use only the prototyped functions to access the golf structures.

Am I at least on the right track? Should golf.cpp even have main() in it? Or should that be pe9-1.cpp?

Any help will be very much appreciated.

You need 3 files:

golf.h contains the functions declaration.
golf.cpp contains the definition of the function in golf.h
main.cpp contains the main() function

There are a few ways to do it and you have two separate problems here. Problem 1 is creating the "golf" class with its functions, then successfully compile the "main" file and the "golf" file(s) and linking them so you can use the "golf" functions from "main". Problem 2 is your while loop problem.


For problem 1 you need to decide whether you want to compile them separately, then link them, or just treat it all as one big program and compile it all at once. Regardless, to use the "golf" functions, you'll need to include them in your main file with this command:

#include "golf.h"

You've already done this, so that's good. invisal's suggestion is a good one. Again, you have to decide whether to compile separately, then link, or treat as one big program. Generally most people compile them separately, then link them. If you go that route, in your "golf.cpp" file you would include the same line at the top as in "main.cpp":

#include "golf.h"

Neither golf.cpp nor golf.h will have the "main" function in them. Put that in "main.cpp" (or if you want to keep the earlier name, put it in "pe9-1.cpp").

For problem 2, the while loop, you are asking the user to enter his/her name in main instead of in function "setgolf". If you look at the specifications for function setgolf (below), you'll notice that the prompting for user input is in there. The "cin" statements should be in there.


// function solicits name and handicap from user
// and sets the numbers of g to the values entered
// returns 1 if name is entered, 0 if name is empty string
int setgolf(golf & g);

Your "while" loop inside the "main" function may look something like this:

golf g;
while (setgolf (g) != 0)
{
// code here with whatever you want to do in the while loop
}

There is more to your project and I'm happy to answer more, but hopefully this will get you started. Feel free to post again with follow up questions or clarification/elaboration and I'm happy to respond.

Hej again!!

This is what I got from main9-1.cpp:

#include <iostream>
#include "golf.h"


using namespace std;
int main()
{
char fullname[Len];
int handicap_a;


cout <<"Please enter your name and handicap: ";
while (cin >> fullname >> handicap_a)
{
handicap(handicap_a);
setgolf(fullname);
cout << "Next name and handicap(q to quit) :";
}
cout << "Bye!\n";
system("PAUSE");
return EXIT_SUCCESS;
}


from golf.cpp I got:


#include <iostream>
#include "golf.h"



void setgolf(golf & g, const char*name, int handicap_a)
{
using namespace std;
char fullname;
int handicap_a;


cout << "Golfer's name is: " << fullname;
cout << "Golfer's handicap is: " << handicap_a;


}

As you can probably imagine, I am kindly confused as to what the instructions want, especially golf.cpp.
I have not tried to compile the program yet because I am not sure of what it wants. The golf.cpp, I think should store the name and handicaps or should it spit them out before it gets more input?

Thanks as always!!

Edited 3 Years Ago by happygeek: fixed formatting

I am writing a skeleton for you. I'll try it out then post it within the hour. In the meantime, a few questions.

1) What operating system are you using for this (i.e. Windows, Linux)?
2) Are you using a programming IDE (i.e. Dev C++ or Visual C++)?
3) Are you compiling/running from within an IDE or from the command line using a command such as g++?
4) Are you using a "makefile" that you will design yourself to compile and link?

Many IDEs like Visual C++ automatically generate a makefile for you and take care of all the linking and stuff behind the scenes for you when you use the "Build" command (or similar) from a pull-down menu.

If you are not using an IDE and are using a compiler such as g++, you'll need either a "makefile" or you'll need to issue three commands from the command line (1 compile command for golf.cpp, 1 compile command for main.cpp, then 1 linking command to link the "golf" object code and "main" object code. A makefile basically does those three commands in order and is put in a file called "makefile", which you run with the command "make".

If these are new terms to you, please don't feel overwhelmed. There really isn't a whole lot to compiling and linking in a simple case like this once you know how. Again, most IDEs do this for you behind the scenes.

Here are three files (no makefile attached)

golf.h

// golf.h file



#ifndef GOLF_H_
#define GOLF_H_



const int Len=40;
struct golf
{
     char fullname[Len];
     int handicap;
};


// function sets golf structure to provided name, handicap
// using values passed as arguments to the function
void setgolf(golf & g, const char*name, int hc);

// function solicits name and handicap from user
// and sets the numbers of g to the values entered
// returns 1 if name is entered, 0 if name is empty string
int setgolf(golf & g);

// function resets handicap to new value
void handicap(golf & g, int hc);

//function displays contents of golf structure
void showgolf(const golf & g);



#endif

golf.cpp

// golf.cpp file


#include "golf.h"
#include <iostream>
using namespace std;



void setgolf(golf & g, const char*name, int hc)
{
     cout << "I am in 'void setgolf(golf & g, const char*name, int hc)' function." << endl;

     // function is void.  Do not return anything.
}



int setgolf(golf & g)
{
     cout << "I am in 'int setgolf(golf & g)' function." << endl;

     return 0;               // since function is an integer, must return some integer
}


void handicap(golf & g, int hc)
{
     cout << "I am in 'void handicap(golf & g, int hc)' function." << endl;

     // function is void.  Do not return anything.
}



void showgolf(const golf & g)
{
     cout << "I am in 'void showgolf(const golf & g)' function." << endl;

     // function is void.  Do not return anything.
}

main.cpp

// main.cpp

#include <iostream>
#include "golf.h"
using namespace std;


int main ()
{
     cout << "This program calls 4 functions declared in "
          << "'golf.h' and implemented in 'golf.cpp'" << endl;

     golf aGolfInstance;   // aGolfInstance is a random name.  
                           // Rename if you like.

     cout << "Calling 'void setgolf(golf & g, const char*name, int hc)' "
          << "function from main function." << endl;
     setgolf (aGolfInstance, "Tiger Woods", 5);

     cout << "Calling 'int setgolf(golf & g)' function from "
          << "main function." << endl;
     setgolf (aGolfInstance);

     cout << "Calling 'void handicap(golf & g, int hc)' function "
          << "from main function." << endl;
     handicap (aGolfInstance, 5);

     cout << "Calling 'void showgolf(const golf & g)' function "
          << "from main function." << endl;
     showgolf(aGolfInstance);

     system ("PAUSE");
     return 0;
}

I just created a project in Dev C++ with these three files. It produced a makefile on its own. The program compiled and ran without errors. It doesn't do what you need as far as the functions doing what you what them to do. However, it does give you an idea of where to put everything and how to call the functions. It prints out the call from main.cpp and it prints out from within the function to prove it is being called. I would start with this skeleton then go from there to start filling in the functions and the function calls to your own program. Again this compiled and ran as-is in Dev C++ without having to provide a makefile. I simply created a new project with these three files and then compiled and ran it. Dev C++ figured out how to compile and link on its own.

Comments
He presents the idea very clearly. Very helpful.

Thank you so much for help with the problem. I must confess I still do not understand the fundamental concept behind 3 files being put together to make one program. I am using Dev-C++ on Windows XP. The program compiled also on my machine flawlessly. I may come back to this after I do classes. I plan on doing port programming in 1 or 2 months and have seen programs using this.

Anyway thanks so much for the help!!

This question has already been answered. Start a new discussion instead.