问题:
解答:
main.cpp
#include <iostream>
#include <string>
#include "golf.h"
using namespace std;
#define SIZE 5
int main()
{
golf ann;
setgolf(ann, "AnnBirdfree", 24);
golf andy;
setgolf(andy);
showgolf(ann);
showgolf(andy);
return 0;
}
golf.h
#pragma once
const int Len = 40;
struct golf
{
char fullname[Len];//这是一个数组
int handicap;
};
void setgolf(golf& g, const char* name, int hc);
int setgolf(golf& g);
void handicap(golf& g, int hc);
void showgolf(const golf& g);
golf.cpp
#include "golf.h"
#include <iostream>
using namespace std;
void setgolf(golf& g, const char* name, int hc)
{
//g.fullname = name;
strcpy_s(g.fullname, name);
g.handicap = hc;
}
int setgolf(golf& g)
{
char name[Len];
int hc=0;
cout << "请输入名字:";
cin.getline(name, Len);
cout << "请输入高尔夫等级:";
while (!(cin >> hc))
{
cin.clear();
while (cin.get()!='\n')
{
continue;
}
cout << "请输入高尔夫等级:";
}
if (name[0] != '\0')
{
setgolf(g, name, hc);
return 1;
}
else
{
return 0;
}
}
void handicap(golf& g, int hc)
{
g.handicap = hc;
}
void showgolf(const golf& g)
{
cout<<"高尔夫的姓名为:" << g.fullname << endl;
cout << "高尔夫的等级为:" << g.handicap << endl;
}
运行结果:
考查点:
- 多文件
- strcpy_s数组的赋值
注意:
-
数组是不能直接复制的,数组名是一个指针不能修改.
-
数组读一行数据
2024年9月2日19:38:16