c语言 scanf_s输入字符串报错
本帖最后由 2740881635 于 2022-6-25 11:07 编辑#include<stdio.h>
int main() {
int run[] = { 31,29,31,30,31,30,31,31,30,31,30,31 };
int feirun[] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
char birthday, date;
int birth_year=0, birth_month=0, birth_day=0, date_year=0, date_month=0, date_day=0;
int D_value=0;
int all_life=0;
double f;
//printf("请输入你的生日(如1988-05-20):");
//scanf_s("%d-%d-%d", &birth_year,&birth_month,&birth_day);
//getchar();
//printf("请输入今年日期(如2016-03-28):");
//scanf_s("%d-%d-%d", &date_year, &date_month, &date_day);
printf("请输入你的生日(如1988-05-20):");
scanf_s("%s", birthday); //问题一
getchar();
printf("请输入今年日期(如2016-03-28):");
scanf_s("%s", date); //问题二
for (int i = 0; i < 10; i++) {
if (i >= 0 && i <= 3) {
birth_year = birth_year * 10 + (int)birthday - 48;
date_year = date_year * 10 + (int)date - 48;
}
else if (i >= 5 && i <= 6) {
birth_month = birth_month * 10 + (int)birthday - 48;
date_month = date_month * 10 + (int)date - 48;
}
else if (i >= 8 && i <= 9) {
birth_day = birth_day * 10 + (int)birthday - 48;
date_day = date_day * 10 + (int)date - 48;
}
}
for (int i=birth_year; i <= date_year-1; i++) {
if (i % 4 == 0) {
D_value += 366;
}
else {
D_value += 365;
}
}
if (birth_year % 4 == 0) {
for (int j = 0; j < birth_month-1; j++) {
D_value -= run;
}
}
else {
for (int j = 0; j < birth_month-1; j++) {
D_value -= feirun;
}
}
if (date_year % 4 == 0) {
for (int j = 0; j < date_month-1; j++) {
D_value += run;
}
}
else {
for (int j = 0; j < birth_month-1; j++) {
D_value += feirun;
}
}
D_value = D_value - birth_day + date_day;
for (int k = birth_year; k < birth_year + 80; k++) {
if (k % 4 == 0) {
all_life += 366;
}
else {
all_life += 365;
}
}
printf("你在这个世界上总共生存了%d天\n", D_value);
printf("如果能活到80岁,你还剩下%d天\n", all_life - D_value);
f = D_value / all_life;
printf("你已经使用了%.2f\%的生命,请好好珍惜剩下的时间!",f*100);
system("pause");
return 0;
}
代码为课后作业18动动手2的我的答案。
问题是我使用VS2022时,scanf_s("%s",birthday);
这一行在输入字符串后会出现错误信息:0x00007FF773F3459D 处有未经处理的异常(在 17.exe 中): 堆栈 Cookie 检测代码检测到基于堆栈的缓冲区溢出。
求求各位大佬解答,几个作业都遭遇到这个问题{:10_266:}
scanf_s("%s",birthday);写成 scanf_s("%s",&birthday,sizeof(birthday));
scanf_s("%s", date);写成 scanf_s("%s", &date,sizeof(date)); scanf_s函数少加了一个检查边界的参数
应当将scanf_s("%s", birthday);改成scanf_s("%s", birthday,sizeof(birthday));
将scanf_s("%s", date);改成scanf_s("%s", date,sizeof(date));
还有就是system函数需要引入#include <windows.h>
#include <stdio.h>
#include <windows.h>
int main() {
int run[] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int feirun[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
char birthday, date;
int birth_year = 0, birth_month = 0, birth_day = 0, date_year = 0, date_month = 0, date_day = 0;
int D_value = 0;
int all_life = 0;
double f;
//printf("请输入你的生日(如1988-05-20):");
//scanf_s("%d-%d-%d", &birth_year,&birth_month,&birth_day);
//getchar();
//printf("请输入今年日期(如2016-03-28):");
//scanf_s("%d-%d-%d", &date_year, &date_month, &date_day);
printf("请输入你的生日(如1988-05-20):");
scanf_s("%s", birthday, sizeof(birthday)); //问题一
getchar();
printf("请输入今年日期(如2016-03-28):");
scanf_s("%s", date, sizeof(date)); //问题二
for (int i = 0; i < 10; i++) {
if (i >= 0 && i <= 3) {
birth_year = birth_year * 10 + (int)birthday - 48;
date_year = date_year * 10 + (int)date - 48;
}
else if (i >= 5 && i <= 6) {
birth_month = birth_month * 10 + (int)birthday - 48;
date_month = date_month * 10 + (int)date - 48;
}
else if (i >= 8 && i <= 9) {
birth_day = birth_day * 10 + (int)birthday - 48;
date_day = date_day * 10 + (int)date - 48;
}
}
for (int i = birth_year; i <= date_year - 1; i++) {
if (i % 4 == 0) {
D_value += 366;
}
else {
D_value += 365;
}
}
if (birth_year % 4 == 0) {
for (int j = 0; j < birth_month - 1; j++) {
D_value -= run;
}
}
else {
for (int j = 0; j < birth_month - 1; j++) {
D_value -= feirun;
}
}
if (date_year % 4 == 0) {
for (int j = 0; j < date_month - 1; j++) {
D_value += run;
}
}
else {
for (int j = 0; j < birth_month - 1; j++) {
D_value += feirun;
}
}
D_value = D_value - birth_day + date_day;
for (int k = birth_year; k < birth_year + 80; k++) {
if (k % 4 == 0) {
all_life += 366;
}
else {
all_life += 365;
}
}
printf("你在这个世界上总共生存了%d天\n", D_value);
printf("如果能活到80岁,你还剩下%d天\n", all_life - D_value);
f = D_value / all_life;
printf("你已经使用了%.2f的生命,请好好珍惜剩下的时间!", f * 100);
system("pause");
return 0;
} 临时号 发表于 2022-6-25 13:47
scanf_s函数少加了一个检查边界的参数
应当将scanf_s("%s", birthday);改成scanf_s("%s", birthday,sizeof ...
谢谢大佬{:10_275:} loverinmydream 发表于 2022-6-25 11:27
scanf_s("%s",birthday);写成 scanf_s("%s",&birthday,sizeof(birthday));
scanf_s("%s", date);写 ...
谢谢大佬{:10_275:}
应该不用加&吧 2740881635 发表于 2022-6-25 14:01
谢谢大佬
应该不用加&吧
这里可加可不加,因为数组传入函数本身传入的就是指针,没有必要用&取地址
如果问题解决了,不要忘了设置最佳答案哦 临时号 发表于 2022-6-25 15:14
这里可加可不加,因为数组传入函数本身传入的就是指针,没有必要用&取地址
如果问题解决了,不要忘了设置最 ...
的确,我没有注意到这是数组
页:
[1]