dearMakiseKurisu 2019-10-17 22:54 采纳率: 100%
浏览 511
已采纳

新手学习C++,有个关于cout输出字符数组的小问题。

运用cout输出字符数组时,我本想输出001.
可是结果却是1.
这是不是程序默认将数组前面的0舍去了?
如果我想要输出的字符是001又应该怎么改?
(由于是做一道题目时发现的这个问题,所以就没有简略的代码和图片了,代码问题部分加粗了,主要为语句cout << one[i].id)
#include
using namespace std;
struct information
{
char name[20];
int id;
double MT;
double EN;
double PH;
double AVG;
};
void input(information[], int);
void output1(information[], int);
void output2(information[], int);
void operation(information[], int);
void R(information[], int);
int main()
{
information student[100];
int total;
cout << "请输入学生人数";
cin >> total;
cout << "请完善各个学生的数据" << "\n";
input(student, total);
operation(student, total);
cout << "下列为所有学生的成绩表" << "\n";
output1(student, total);
R(student, total);
cout << "其中成绩尤为突出学生是" << "\n";
output2(student, total);
}
void input(information one[], int x)
{
for (int i = 0;i < x;i++)
{
cout << "请输入第" << i + 1 << "个学生的数据" << endl;
cout << "他的学号为";
cin >> one[i].id;
cout << "他的名字为";
cin >> one[i].name;
cout << "他的数学成绩为";
cin >> one[i].MT;
cout << "他的英语成绩为";
cin >> one[i].EN;
cout << "他的物理成绩为";
cin >> one[i].PH;
}
}
void operation(information one[], int x)
{
for (int i = 0;i < x;i++)
{
one[i].AVG = (one[i].MT + one[i].EN + one[i].PH) / 3;
}
}
void output1(information one[], int x)
{
for (int i = 0;i < x;i++)
{
**cout << one[i].id
<< "\t" << one[i].name<< "\t" << one[i].MT << "\t" << one[i].EN << "\t" << one[i].PH << "\t";
printf("%3.4f", one[i].AVG);
cout << "\n";
}**
}
void R(information one[], int x)
{
int i, j;
information team;
for (i = 0;i < x;i++)
{
for (j = 0;j <= x - i - 1;j++)
{
if (one[j].AVG < one[j + 1].AVG)
{
team = one[j];
one[j] = one[j + 1];
one[j + 1] = team;
}
}
}
}
void output2(information one[], int x)
{
for (int i = 0;i < 3;i++)
{
cout << "第" << i + 1 << "名";
cout << one[i].name << "同学,";
cout << "他的平均分是 ";
printf("%3.4f", one[i].AVG);
cout << " 分。" << "\n";
}
}
图片说明

  • 写回答

1条回答

  • threenewbee 2019-10-18 09:24
    关注

    int id;
    换成string id;

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器