#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