# n00b needs help with small program

• 04-15-2009
rightbrainer
n00b needs help with small program
Hello all

I have always wanted to learn to program. I have started and quit many times. it is tough as a self-taught student. I found a good online course recently. I intend to see it through. Here is my first program. I need help from one of you :)

My challenge was to write a program that determines whether entered year is a leap year. If entered year is not a leap year, it should prompt for another year that is a leap year, not close. it should keep prompting for a leap year until it gets one.

So far I have it calculating whether it's a leap year or not..

Code:

#include <stdio.h>
#include <cs50.h>

int
main(int argc, char * argv[])
{
int x;
printf("Enter a year: ");
x = GetInt();
if( ((x % 4 == 0) && (x % 100 != 0)) || (x % 400 == 0) )
printf("The Year is a Leap Year");
else
printf("The Year is Not a Leap Year");
return 0;
}

So far so good. Now I need to figure out how to get it to loop and keep prompting for a leap year until it gets one.

It is my feeling that this needs a DO WHILE loop... although I am not sure.. nor do I know quite how to write it.

Can you help me?

Thanks much

-Noob
• 04-15-2009
Code:

#include <stdio.h>
#include <cs50.h>

int
main(int argc, char * argv[])
{
int x, gotOne = 0;

do  {
printf("Enter a year: ");
x = GetInt();

if( ((x % 4 == 0) && (x % 100 != 0)) || (x % 400 == 0) ) {
printf("The Year is a Leap Year");
gotOne = 1;
}
else
printf("The Year is Not a Leap Year");

}while(gotOne == 0);

return 0;
}

A do loop will *always* be entered into, once. A while loop, may or may not be entered into, once (since the conditional test is done right at the top of the loop).

Just fyi.

Welcome to the forum, and I hope you enjoy the tutorial you found.
• 04-15-2009
rightbrainer
Adak, thank you so much! It works. :)

Ok so let me see if I have this right (don't laugh!)

Code:

#include <stdio.h>
#include <cs50.h>

int
main(int argc, char * argv[])
{
int x, gotOne = 0;

do  {
printf("Enter a year: ");
x = GetInt();

if( ((x % 4 == 0) && (x % 100 != 0)) || (x % 400 == 0) ) {
printf("The Year is a Leap Year");
gotOne = 1;
}
else
printf("The Year is Not a Leap Year");

}while(gotOne == 0);

return 0;
}

So before, I only had one integer variable declared (x). Then you added another called gotOne. Is this correct? And your variable is also an integer (even though you didn't type "int" before it?)

Your variable is assigned a value of 0 and in the do loop the year entered by the user is assigned to my x variable.

If user enters a leap year, your variable is assigned a 1 (and at the same time my variable is still assigned the value the user entered)

If user does not enter leap year, your variable remains assigned a value of 0--as you set it to begin with. And at the end of the code if your variable is assigned a zero still, the code is executed again starting at the DO part.

Am I on track?

Thanks again!
• 04-15-2009
Ok so let me see if I have this right (don't laugh!)

Code:

#include <stdio.h>
#include <cs50.h>

int
main(int argc, char * argv[])
{
int x, gotOne = 0;

do  {
printf("Enter a year: ");
x = GetInt();

if( ((x % 4 == 0) && (x % 100 != 0)) || (x % 400 == 0) ) {
printf("The Year is a Leap Year");
gotOne = 1;
}
else
printf("The Year is Not a Leap Year");

}while(gotOne == 0);

return 0;
}

So before, I only had one integer variable declared (x). Then you added another called gotOne. Is this correct? And your variable is also an integer (even though you didn't type "int" before it?)

Yes. These are all variables of type int:
int isLeap, notLeap, a=0, b=5, c=bobbysAge, lat=lattitude, ripe, bright, red, blue;
The rule is, you can't have a variable with a reserved C word, and you can't use a variable until you declare it.

By convention, local or automatic variables begin with lowercase letters, global variables begin with uppercase letters (and may be all uppercase).

Variables named i, j, and k, are usually used for simple loop counters:
Code:

for(i = 0; i < someVariable; i++)
printf("%d ", arrayName[i]);

Your variable is assigned a value of 0 and in the do loop the year entered by the user is assigned to my x variable.

If user enters a leap year, your variable is assigned a 1 (and at the same time my variable is still assigned the value the user entered)

You can think of isLeap like a light switch. When it's off, no light, no leap year. When it's on, you have light, and you have a leap year. Using isLeap that way makes isLeap a "flag" or "boolean" variable. Like a light switch, there is no "degree" of on or off, there is just *on* or *off*, 0 or 1.

If user does not enter leap year, your variable remains assigned a value of 0--as you set it to begin with. And at the end of the code if your variable is assigned a zero still, the code is executed again starting at the DO part.

Am I on track?

Right on track. :)

Thanks again

You're very welcome.