-
more debugging
C:\Borland\BCC55\src>bcc32 golfh.cpp golfm.cpp golff.cpp
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
golfh.cpp:
golfm.cpp:
golff.cpp:
Error E2277 golff.cpp 25: Lvalue required in function setgolf(golf &,const char *,int)
I assume this means the types dont match in the line
g.fullname=name //????
Warning W8057 golff.cpp 27: Parameter 'name' is never used in function setgolf(g
olf &,const char *,int)
*** 1 errors in Compile ***
Code:
#include <iostream>
#include "golfh.cpp"
using namespace std;
int setgolf(golf & g)
{
cout << "Enter golfers name:";
cin.get(g.fullname,Len);
if (cin)
{
return 0;
}
else
{
cout << "Enter player's handicap:";
cin >> g.handicap;
return 1;
}
}
void setgolf(golf & g, const char *name, int hc)
{
g.fullname=name;// error here ????
g.handicap=hc;
}
void handicap(golf & g, int hc)
{
g.handicap=hc;
}
void showgolf(const golf & g)
{
cout << "Player's name:"<<g.fullname<<endl;
cout << "Handicap:"<<g.handicap<<endl;
cout << endl;
}
-
> Warning W8057 golff.cpp 27: Parameter 'name' is never used in function setgolf(golf &,const char *,int)
That means you declare "name" as a parameter, but don't use it in the function.
What's your golf struct?
-
header file
Here is the header file with the struct and prototypes
Code:
//golff.h for 8.3 pg 369 c++ primer plus
const int Len=40;
struct golf
{
char fullname[Len];
int handicap;
};
int setgolf(golf & g);
void setgolf(golf & g, const char *name, int hc);
void handicap(golf & g, int hc);
void showgolf(const golf & g);
-
Never mind - I looked it up.
use strcpy in place of
g.fullname=name;// error here ????
-
main file
Here is the main file
Code:
#include <iostream>
#include "golfh.cpp"
using namespace std;
int main()
{
const int Max=20;
golf folf[Max];
int end,c;
c=0;
end=1;
setgolf(folf[c],"Troy",40);// here is the calling function
while (end && (c<Max))
{
showgolf(folf[c]);
c++;
end=setgolf(folf[c]);
}
return 0;
}
-
in the cpp file and the main program file it should be
#include "golff.h"
rather than
#include "golff.cpp"
use the strcpy suggestion as per GovtCheese's post.
in setgolf() I think you might want this:
if(!cin)
{
return 0;
}
otherwise, cin should "always" be true, so end will always be assigned the value of 0 so the while loop will only work once.
-
It will not compile as golf.h because it assumes the header file is a c file rather than a c++ file. There is prbly a special extension for c++ headers but cpp worked fine.
According to c++ primer plus cin should evaluate to false if the peceding line cin.get(g.fullname,Len); fails. ie by entering a blank line. I coule not get this to work so i used this instead:
if (strlen(g.fullname)==0)
-
I always get twisted up in these discussions. BUT: it is my understanding that the conditional for an if statement needs to evaluate to true for the body of the if statement to run. If cin evaluates to false when when the preceding line fails then the conditional of the if will be false if the conditional is just cin, and the body of the if statement will not run meaning end will never be zero. If cin evaluates to false when the preceding line fails, then !cin will evaluate to true and the body of the if will run. If cin in the preceding line evaluates to true then !cin will evaluate to false and the body of the if will not run if the preceding line does not fail. Therefore I still think you want !cin rather than cin as the conditional of the if statement, but if the code works as you desire, so be it.
-
I think you are right conceptouallyw but neither way worked using cin or !cin hence my use of strlen