n00b needs help with small program

This is a discussion on n00b needs help with small program within the C Programming forums, part of the General Programming Boards category; Hello all I have always wanted to learn to program. I have started and quit many times. it is tough ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    24

    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?

    Please?

    Thanks much

    -Noob

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    24
    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!

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  2. Program to reverse a number. small error with it..
    By comproghelp in forum C Programming
    Replies: 8
    Last Post: 11-22-2004, 10:52 AM
  3. Help writing small program
    By guitarhead2000 in forum C++ Programming
    Replies: 2
    Last Post: 10-13-2004, 01:42 PM
  4. A little Help with a small program
    By whtpirate in forum C Programming
    Replies: 7
    Last Post: 06-05-2003, 07:15 PM
  5. Very small program...Whats wrong???
    By SmokingMonkey in forum C++ Programming
    Replies: 4
    Last Post: 05-30-2003, 10:09 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21