gpt4 book ai didi

c - 保持输出为零

转载 作者:太空宇宙 更新时间:2023-11-04 01:24:52 24 4
gpt4 key购买 nike

我是 C 语言的新手,想创建一个程序来计算服务月数。

我一直得到输出为 0,不知道为什么。

#include<stdio.h>


int main()
{
int ddjoin, mmjoin, yyjoin, ddlev, mmlev, yylev;
int mmdiff = 0;
int mmserv = 0;
int y2y1 = 0;



//Getting Joining Date
printf("When did you join us ? (dd/mm/yyyy)\n");

printf("Date (dd)\n");
scanf_s("%d", &ddjoin);

printf("Month (mm)\n");
scanf_s("%d", &mmjoin);

printf("year (yyyy)\"\n");
scanf_s("%d", &yyjoin);

printf("You joined us on %d/%d/%d\n", ddjoin, mmjoin, yyjoin);

//Getting Termination Date
printf("When did you retire ? (dd/mm/yyyy)\n");

printf("Date (dd)\n");
scanf_s("%d", &ddlev);

printf("Month (mm)\n");
scanf_s("%d", &mmlev);

printf("year (yyyy)\"\n");
scanf_s("%d", &yylev);

printf("You retired on %d/%d/%d\n", ddlev, mmlev, yylev);

y2y1 == (yylev - yyjoin - 1) * 12;
mmdiff == 12 - mmjoin;
mmserv == y2y1 + mmdiff + mmlev;

printf("Your months in service are %d\n", mmserv);

return 0;


}

输出为零。我尝试在开始时不为它们分配 0 值,但随后它给了我“非本地化变量错误”。

最佳答案

y2y1 == (yylev - yyjoin - 1) * 12;    // == operator is to compare values
mmdiff == 12 - mmjoin;
mmserv == y2y1 + mmdiff + mmlev;

不要使用==(它是一个关系运算符)来赋值。使用赋值运算符 = 。将它们写成 -

y2y1 = (yylev - yyjoin - 1) * 12;
mmdiff = 12 - mmjoin;
mmserv = y2y1 + mmdiff + mmlev;

关于c - 保持输出为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33057498/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com