Solving C++ Leap Year Program: An Overview

  • Context: Comp Sci 
  • Thread starter Thread starter lkh1986
  • Start date Start date
  • Tags Tags
    C++ Program Year
Click For Summary
SUMMARY

The discussion revolves around creating a C++ program to determine if a given year is a leap year. The initial code contains errors, particularly the illegal expression "if (value==int)". The correct approach involves using the modulus operator (%) to check if the year is divisible by 4, with additional rules for leap years. Participants suggest refining the conditional statement to accurately reflect leap year logic, emphasizing the importance of understanding the modulus operator introduced in the course.

PREREQUISITES
  • Basic understanding of C++ programming syntax
  • Familiarity with the modulus operator (%) in programming
  • Knowledge of conditional statements in C++
  • Concept of leap year rules as defined in programming
NEXT STEPS
  • Implement the correct leap year logic using the modulus operator in C++
  • Explore C++ conditional statements and their syntax
  • Review C++ input/output operations using iostream
  • Study the rules for leap years in detail, including exceptions
USEFUL FOR

Beginner C++ programmers, students learning about conditional logic, and anyone interested in understanding leap year calculations in programming.

lkh1986
Messages
96
Reaction score
0

Homework Statement


I just started my C++ courses and here is my question.



Homework Equations





The Attempt at a Solution


//This program states whether a given year is a leap year or a non-leap year.

#include <iostream.h>
#include <conio.h>

void main ()
{
int year, value;
char answer,y, n;

while (answer == y)
{
count << "Please input a year: ";
value = year/4;
if (value==int)
count << ""<< year << " is a leap year. There are 29 days in Feb " << year <<".";
else
count << ""<< year << " is a non-leap year. There are 28 days in Feb " << year <<".";
}
count << "Do you want to continue? Press 'y' if yes or 'n' if no.";
getch();
}

It seems that "if (value==int)" is illegal. Is there any other way I can put th expression statement so that the computer can print out whether a given year is a leap year or not?

Thanks.
 
Physics news on Phys.org
What you need to do is to check if the variable year is a multiple of 4 and the modulus operator % is perfect for the task, e.g.,
1%4 = 1, 2%4 = 2, 3%4 = 3, 4%4 = 0, 5%4 = 1, ...

What should the conditional expression be for the if-statement now then?

Btw, the rules for checking a leap year is a bit more involved than the above. Look http://en.wikipedia.org/wiki/Leap_year" for an insight.
 
Last edited by a moderator:
Okay, thanks for the help. My instructor just introduced the modulus operator today.
 

Similar threads

  • · Replies 7 ·
Replies
7
Views
7K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 23 ·
Replies
23
Views
9K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 15 ·
Replies
15
Views
3K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 4 ·
Replies
4
Views
26K