#ifndef GOLFH_H_
#define GOLFH_H_
#include <iostream>
#include <cstring>
using namespace std;
const int len = 40;
struct golf{
char fullname[len];
int handicap;
};
void setgolf(golf &,const char*,int);
int setgolf(golf &);
void handicap(golf &,int);
void showgolf(const golf &);
void setgolf(golf & g,const char* name,int hc){
strcpy(g.fullname, name);
g.handicap = hc;
}
int setgolf(golf & g){
cout << "Enter name(!Enter)-> ";
char temp[len];
cin.getline(temp, len);//怀疑是这行和下一行的问题,标记问题点1
if (strlen(temp) != 0 )//同上 标记问题点2
cout <<"strlen[temp]="<<strlen(temp)<<endl;
strcpy(g.fullname, temp);
cout <<"Enter hc(int)-> ";
cin >> g.handicap;
cin.ignore();
return 1;
return 0;
}
void handicap(golf & g,int hc){
g.handicap = hc;
}
void showgolf(const golf & g){
using namespace std;
cout << g.fullname << "->" << g.handicap
<< endl << "===================" << endl;
}
#endif
#include "golfh.h"
int main(){
golf gs[5];
for(int i=0;i<5&&setgolf(gs[i]);i++){
showgolf(gs[i]);
}
//setgolf(gs[0],"model",44);
//showgolf(gs[0]);
//handicap(gs[0],33);
//showgolf(gs[0]);
return 0;
}
代码文件如上。
输出是这样的:
问题点1: cin.getline(temp, len); 采用过cin .get(temp,len); cin>>temp;temp=cin.getline();
问题点2: strlen(temp) != 0 ;采用过 temp[0] != ' ';temp[0] != '\0';
ps:最好不要用string库,虽然我轻微尝试过并且失败了😓,当然有导入string库可以解决也ok啦
ps:解决过程还遇到了bus error问题,还有不要问我为啥.h文件里有函数定义😓