首页 > 编程语言 >C/C++ struct结构体的三种使用方法

C/C++ struct结构体的三种使用方法

时间:2022-10-29 11:14:38浏览次数:54  
标签:hero struct scanf C++ sex 三种 printf include

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <conio.h>
using namespace std;
struct hero {
    char name[16];
    char sex; //W代表女,M代表男;
    int strong;
    int age;
}; //hero; //第一种用法
struct hero hero;  //第二种用法
int main(void) {
    //struct hero hero;  //第三种用法
    printf("请输入英雄的姓名:");
    scanf_s("%s",hero.name,sizeof(hero.name));

    printf("请输入英雄的性别:");
    cin.ignore((numeric_limits<std::streamsize>::max)(), '\n');
    scanf_s("%c", &hero.sex, sizeof(hero.sex));//这里会吃掉回车符,所以要清除缓冲区

    printf("请输入英雄的武力值:");
    scanf_s("%d", &hero.strong);

    printf("请输入英雄的年龄:");
    scanf_s("%d", &hero.age);

    printf("\n-----角色信息表-----\n");
    printf("【姓名】:%s\n",hero.name);
    printf("【性别】:%c\n", hero.sex);
    printf("【武力值】:%d\n", hero.strong);
    printf("【年龄】:%d\n", hero.age);

    system("pause");
    return 0;
}

标签:hero,struct,scanf,C++,sex,三种,printf,include
From: https://www.cnblogs.com/smartlearn/p/16838268.html

相关文章

  • 周六900C++班级2022-10-29 广搜
    7588:农夫抓牛农夫知道一头牛的位置,想要抓住它。农夫和牛都位于数轴上,农夫起始位于点N(0<=N<=100000),牛位于点K(0<=K<=100000)。农夫有两种移动方式:1、从X移动到X-1或X+......
  • 【WPF】绘制图形的三种方法及区别
     WPF中用于绘图的类主要有三个,分别是Shape类、Drawing类和DrawingVisual类,Shape类存在于System.Windows.Shapes命,而Drawing类和DrawingVisual类则都存在于System.Window......
  • 三种常用的Vue表单修饰符使用场景...
    vue的项目开发中须知道的三个vue表单修饰符(.lazy,.number,.trim)1,.lazy简单地说:我们在input输入框输入的时候,标签里的内容会实时变化,加了.lazy修饰符后,只有当鼠标光标离......
  • 记录第一次使用c++和汇编联合编译
    32位从Ida把目标函数扣出来toUapperprocneararg_0=dwordptr4pushesimovesi,[......
  • 关于struct和typedef
    c/c++中有定义数据结构语句structstudent//定义数据结构体{inta;//.....}structstudenta1;//初始化变量可以看到,定义结构体后每次进行初始......
  • C++ primer笔记 7.1 定义抽象数据类型
    7.1定义抽象数据类型structSales_data{std::stringbookNo;unsignedunits_sold=0;doublerevenue=0.0;std::stringisbn()const{returnboo......
  • Homework 3 : C++ class inheritance Answer
    Homework3:C++classinheritanceAnswerHomework3:C++classinheritanceInstructor:ZhiyaoLiangCIS111&EIE1112021Spring源码传送门传送门:https://pan.ba......
  • Homework 2 : Classes and Data Structure Answer
    Homework2:ClassesandDataStructureAnswerHomework2:ClassesandDataStructureC++:CS111EIE111LP104MacauUniv.ofSci.andTech.2021SpringInstr......
  • 英雄联盟用什么语言编写?C/C++ yyds
    一直有读者咨询C/C++可以干什么,今天这篇文章分享一下具体应用领域。 1、操作系统MicrosoftWindows:汇编->C语言->C++Linux:C语言AppleMacOS: 主要为C语言,部......
  • C++——new和malloc的区别
    new是关键字/操作符,而malloc是函数new一个对象的时候,不但分配内存,而且还会调用类的构造函数(当然如果类没有构造函数,系统也没有给类生成构造函数,那没法调用构造函数了)......