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 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型