【问题标题】:C2106: '=' : left operand must be l-value [closed]C2106:'=':左操作数必须是左值 [关闭]
【发布时间】:2017-01-18 18:55:27
【问题描述】:

所以,我正在学习 C 作为我的第一语言,并且在进行一些编码以进行练习时,我遇到了上述错误。我按照书中所说的做了一切(Stephen G. Kochan:Programming in C,第三版)。 我究竟做错了什么? 我正在使用 Microsoft Visual Studio 2015。

感谢您的帮助! 标记

struct date
{
int month;
int day;
int year;
};

int main(void)
{
    struct date today, tomorrow;
    int numberOfDays(struct date d);

    printf("Adja meg a mai datumot (hh nn eeee): ");
    scanf_s("%i%i%i", &today.month, &today.day, &today.year);

    if (today.day != numberOfDays(today))
    {
        tomorrow.day = today.day + 1;
        tomorrow.month = today.month;
        tomorrow.year = today.year;
    }
    else if (today.month == 12)
    {
        tomorrow.day = 1;
        tomorrow.month = 1;
        tomorrow.year = today.year + 1;
    }
    else
    {
        tomorrow.day = 1;
        tomorrow.month = today.month + 1;
        tomorrow.year = today.year;
    }
    printf("A holnapi datum: %i/%i/%.2i.\n", tomorrow.month, tomorrow.day, tomorrow.year % 100);

    return 0;
}

int numberOfDays(struct date d)
{
    int days;
    bool isLeapYear(struct date d);
    const int daysPerMonth[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

    if (isLeapYear(d) == true && d.month == 2)
        days = 29;
    else
        days = daysPerMonth[d.month - 1];

    return days;
}

bool isLeapYear(struct date d)
{
    bool leapYearFlag;

    if ( (d.year % 4 == 0 && d.year % 100 = 0) || d.year % 400 == 00) //The error shows up here
        leapYearFlag = true;
    else
        leapYearFlag = false;

    return leapYearFlag;
}

【问题讨论】:

  • 先尝试一个更简单的程序。当你一步一步建立它时,你会发现你的错误或更好的问题。
  • d.year % 100 = 0 应该是d.year % 100 == 0
  • Kerrek:我就是这样做的。这是本书的第 9 章。拉沙内:谢谢!可悲的是,错误仍然存​​在。

标签: c operand


【解决方案1】:

这里有错别字

if ( (d.year % 4 == 0 && d.year % 100 = 0) || d.year % 400 == 00) 
                                    ^^^^

我想你是说

if ( (d.year % 4 == 0 && d.year % 100 != 0) || d.year % 400 == 00) 
                                     ^^^^

而 00 等价于 0.:)

函数可以写得更简单

bool isLeapYear( struct date d )
{
    return ( d.year % 4 == 0 && d.year % 100 != 0 ) || ( d.year % 400 == 0 );
}

【讨论】:

  • 谢谢。是的,那些是错别字。可悲的是,错误仍然存​​在。
  • @MárkBurka 我没有看到任何其他编译错误。
  • 是的。好吧,无论如何,谢谢!
猜你喜欢
  • 2013-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多