就这个编程问题,我想了一天,求解答呀

#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文件里有函数定义😓 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值