Please help!! If/else loop

the program runs, but when I input 1600 it says it is a leap year and is not?
should I use double for remainder?

#include <iostream>
using namespace std;
int main () {
//declare variable integer for year, y;
int y, remainder;
//ask user to input a year;
cout << "Enter a year and press ENTER " << endl;
//place year in int y;
cin >> y;
//divide year by 4 and find remainder;
remainder= y % 4;
//main if statement: if rem of y/4 is 0 divide by 400, if there is no rem print y is not leap year and end prg
if (remainder==0) {remainder = y % 400;
// if rem of y/400 is 0 then print y is leap year and end prg, otherwise divide y/100
if (remainder==0) cout << y << " is a leap year." << endl; return main;
else remainder = y % 100;
//if rem of y/100=0 cout y is not leap year, end prg, otherwise print y is leap year, end prg;
if (remainder==0) cout << y << " is not a leap year." << endl; return main;
else cout << y << " is a leap year" << endl; return main;}
else cout <<y<< " is NOT a leap year." << endl; return 0;
}



The code you posted doesn't compile for me. I think you may be missing some curly braces around your if statements.

I took the liberty of reformatting the code (please use [co de] and [/co de] tags, removing the space, to display your code in a better format). And while it won't compile, I can mentally step through it and see your result.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>

using namespace std;

int main () {
	//declare variable integer for year, y; 
	int y, remainder;

	//ask user to input a year; 
	cout << "Enter a year and press ENTER " << endl;

	//place year in int y; 
	cin >> y;

	//divide year by 4 and find remainder; 
	remainder= y % 4;

	//main if statement: if rem of y/4 is 0 divide by 400, if there is no rem print y is not leap year and end prg 
	if (remainder==0) {
		remainder = y % 400;
		// if rem of y/400 is 0 then print y is leap year and end prg, otherwise divide y/100 
		if (remainder==0)
			cout << y << " is a leap year." << endl;
		return main;
		else
			remainder = y % 100;
	
	//if rem of y/100=0 cout y is not leap year, end prg, otherwise print y is leap year, end prg; 
	if (remainder==0)
		cout << y << " is not a leap year." << endl;
	return main;
	else
		cout << y << " is a leap year" << endl; return main;
	}
	else
		cout <<y<< " is NOT a leap year." << endl;
	return 0;
}


Take a look at line 16, which gets the modulo of 1600 % 4, which is 0
So on line 20, it gets the modulo of 1600 % 400 which is 0, so on line 23 it tells you it is a leap year.

Also, your comments on lines 18 and 21 conflict with each other. So perhaps line 23 should be saying something different?
Last edited on
thanks very much! I got it running but it prints 2 statements 1600 is a leap year. I tried definition of remainder variables, r1, r2, r3.
do you have to put the { in the if statement right after the condition in ()? or do you place it before the if? I found using a tree diagram helped for this.
Can most professional programmers do the tree in their head? anyway I'm having health problems...
I can see that there is a main if else statement with the else coming at the very end
how does one use return main or exit (0)? I can't get either to end the program after printing
the problem when I get it running is that the program doesn't end and goes to another part of the if/else loop and prints twice for 1600, 1600 is a leap year

I'm not sure about how lines 18/24 conflict?
Last edited on
this is the prg that will compile but it still prints twice 1600 is a leap year

#include <iostream>

using namespace std;

int main () {
//declare variable integer for year, y;
int y, remainder;

//ask user to input a year;
cout << "Enter a year and press ENTER " << endl;

//place year in int y;
cin >> y;

//divide year by 4 and find remainder;
remainder= y % 4;

//main if statement: if rem of y/4 is 0 divide by 400, if there is no rem print y is not leap year and end prg
if (remainder==0) {
remainder = y % 400;
// if rem of y/400 is 0 then print y is leap year and end prg, otherwise divide y/100
if (remainder==0)
cout << y << " is a leap year." << endl;
return main;
else
remainder = y % 100;

//if rem of y/100=0 cout y is not leap year, end prg, otherwise print y is leap year, end prg;
if (remainder==0)
cout << y << " is not a leap year." << endl;
return main;
else
cout << y << " is a leap year" << endl; return main;
}
else
cout <<y<< " is NOT a leap year." << endl;
return 0;
}
anyway I tried this but this won't run

r1= y % 4;
r2= y % 400;
//divide year by 4 and find remainder, r1 = y % 4, r2 = y % 400, r3= y % 100;
//main if statement: if rem of y/4 is 0 divide by 400, if there is no rem print y is not leap year and end prg;
if (r1==0)
// if rem of y/400 is 0 then print y is leap year and end prg, otherwise y mod 100;
if (r2==0) {cout << y << " is a leap year." << endl;
exit (0);
else r3= y % 100;}
//if rem of y/100=0 cout y is not leap year, end prg, otherwise print y is leap year, end prg;
{if (r3==0) cout << y << " is NOT a leap year." << exit (0);
else cout << y << " is a leap year" <<endl; return 0}}
else cout <<y<< " is NOT a leap year." << endl;
return 0;
}
[co de] write code here [co de]
I hope the following helps, and doesn't confuse you. My apologies if it does.

1) The semicolon ends a statement. The syntax of C++ does not have a problem with you putting more than one semicolon on a line. However, it can cause confusion for the developer (you). Things may make more sense if you put each statement on its own line.

2) The "if" statement reads in a condition. If the condition is true, it runs the very next statement. If you want it to run more than one statement, place the statements in curly braces. As an example, see below. All of line 2 will print only if the remainder is zero. Line 3 will always print.

1
2
3
if (remainder == 0)
    cout << "This will print if the remainder is equal to zero" << endl;
cout << "This will always print." << endl;


If you put line 3 at the end of line 2, as it is below, the program will run exactly the same.

1
2
if (remainder == 0)
    cout << "This will print if the remainder is equal to zero" << endl; cout << "This will always print." << endl;


If you want more than one statement to run if a condition is met, surround the lines with curly braces. See below...

1
2
3
4
5
if (remainder == 0) {
    cout << "This will print if the remainder is equal to zero." << endl;
    cout << "This will also print if the remainder is equal to zero." << endl;
}
cout << "This will always print." << endl;


Run through your code and place each statement on its own line. Put curly braces around lines you wish to run together. Re-post your new code here, surrounding your code with [co de] and [/co de] so that we get the pretty blue boxes. Let us know how it goes.
thanks I was able to solve it on my own by making the last if/else statement part of the upper else statement with a {.
your post really clarified the structure of the syntax. I'm new and have been having bad health problems so thanks so much!

r= y % 4;
if (r==0) {r= y % 400;
if (r==0) cout << y << " is a leap year." << endl;
else {r= y % 100;
if (r==0) cout << y << " is NOT a leap year." << endl;
else cout << y << " is a leap year" <<endl;}}
else cout << y << " is NOT a leap year." << endl;

//L8 if y mod 4 r=0 then go on to r mod 400 test, otherwise jump to L13 and p\
rint y is not LY
// L9 if r mod 400 is 0 print y is a leap year, otherwise test r mod 100
// L10 if r mod 100=0 print y not LY, otherwise print y is LY
//if main if statement y mod 4 is untrue then print y is not LY
return 0;
}

thanks again for the help. So where can I find a listing of all commands with the parameters and descriptions like you wrote?
Topic archived. No new replies allowed.