信奥C++结构体练习

#include <iostream>
#include <string>
#include <string.h>
using namespace std;
//注意点,C风格字符串形式 char name[50] 定义名称为50个字符长度的数组。
//如果要给赋值 cin>>name;
//name="张三" 会报错,要用strcpy(name,"张三")这种数组赋值方式。
//在C++网络,引入<string>类型,可以直接赋值 string name;
//name="张三"

struct Student{
	char name[50];
	string classname; 
	int age;
};

int compare_student(Student sa,Student sb){
	//输入两个学生的信息,第一个大于第二个的年龄则返回1
	int result=0;
	if(sa.age>sb.age){
		result=1;		
	}	
 	return result;	
}

void pnt(Student st){
	cout<<"name:"<<st.name<<endl;
	cout<<"班级:"<<st.classname<<endl;
	cout<<"age:"<<st.age<<endl;
	
}

int main(){
	Student stu={"张三","1902班",22};
    Student	sb ={"李四","1802班",20};
	Student sc;	
	sc.classname="1802";
//	sc.name="李明";
    strcpy(sc.name,"李明");
    sc.age=23;
    
	pnt(stu);
	pnt(sc);
	
    cout<<"输入学生姓名,年龄";
    cin>>sc.name;
    cin>>sc.age;
//    strcpy(sc.classname,"1902");
	sc.classname="1902";
    
    
	cout<<stu.name<<stu.age<<"比"<<sb.name<<sb.age<<"年龄大:"<<compare_student(stu,sb)<<endl;

	
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值