|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
输入数据为:
Liu Mengmeng
0821131666666
88 90 93 91 85
输入进去后会报错,貌似是数组越界?但是没明白为什么
- #include<stdio.h>
- struct Student{
- char name[200];
- char id[200];
- int score[5];
- double avreage;
- int total;
- };
- int main()
- {
- struct Student one;
-
- scanf("%s", one.name);
- scanf("%s", one.id);
- for(int i = 0;i < 5; i++)
- {
- scanf("%d", one.score[i]);
- }
- for(int i = 0;i<4;i++)
- {
- if(one.score[i] < one.score[i+1])
- {
- int temp;
- temp = one.score[i];
- one.score[i+1] = one.score[i];
- one.score[i] = temp;
- i = 0;
- }
- }
- for(int i = 0;i<4;i++)
- {
- one.total += one.score[i];
- }
- printf("Name:%s\n", one.name);
- printf("ID:%s\n", one.id);
- for(int i = 0;i < 4; i++)
- {
- printf("Score:%d ", one.score[i]);
- }
- for(int i = 0;i<4;i++)
- {
- printf("Score:%d\n ", one.score[i]);
- }
- printf("average:%.2f ", one.total/5);
- printf("total:%d", one.total);
-
- return 0;
- }
复制代码
本帖最后由 ExiaGN001 于 2023-2-18 15:45 编辑
有用请设置最佳谢谢
另:希望本版鱼油在提问的时间注意以下几点:
1. 提问尽量不要点将,例如: 小甲鱼来问答下... XX来回答下... (影响他人回帖积极性)
2. 代码大于20行以上时,最好写清自己的思路和注释,这样回答的人才能尽快给大家答案!(避免扔上一大堆代码,说是有错误,请人指点!)
3. 发代码请务必使用编辑器的“添加代码文字”(这个符号:<>)(以上是官方说法)
Bug报告:
Bug类型:
C语言—运行出错(RE)
程序返回值:-1073741819 (0xC0000005)
分析结果:
采用了错误的输入数据(程序本身无问题)
解释/原因:
C语言里,scanf函数在读取字符串时会读取到第一个空白符停止,而空白符会被忽略。
空白符包括Tab键,空格和换行符。
而分析输入数据:
- Liu Mengmeng
- 0821131666666
- 88 90 93 91 85
复制代码
可以注意到内容是如下格式:
而程序的读入格式是:
%d与%s不兼容导致程序在运行时退出。
在C++中可通过使用getline()或cin.getline读取一行(读到设置的字符上限或换行符时才停止)。
|
|