Simple Checkbook Program with Leap Year

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 833 Views - Last Post: 08 October 2018 - 02:49 AM Rate Topic: -----

#1 jplutts   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 10
  • Joined: 04-October 18

Simple Checkbook Program with Leap Year

Posted 04 October 2018 - 10:48 PM

Hello everyone,
I am writing a simple checkbook program. The program runs correctly, but I need to add in a function where it will say invalid date fore a leap year. I'm not sure if I'm way off base or close I have attached what I have so far below, and my error codes are below that. Any help is greatly appreciated.

// Program #2- Simple Checkbook Program
//

#include <stdio.h>
#include <stdlib.h>

#define SENTINEL 99
int main(void)
{

// Create and initialize local variables
int month =0;
int day =0;
int year =0;
int referenceNumber =0;
double withdrawalAmount =0.0;
double depositAmount;

printf("Joseph P.  CSCI1110 03\n");
printf("Program #2- Simple Checkbook Program\n\n");
printf("To exit, enter the input of 99\n\n");

printf("MM DD YYYY Reference# Withdrawal# Deposit\n");
printf("== == ==== =========  ==========  ======== \n");

//Start a loop for reading input
while(1)
{

//enter input
            printf("enter the MMDDYYYY Reference# Withdrawal Deposit: ");
            scanf("%2d %2d %4d %d %lg %lg", &month, &day, &year, &referenceNumber, &withdrawalAmount, &depositAmount);
if  (month == 02, day == 29, year !% 400 )
    printf("please enter a valid date");

//check for errors and SENTINEL
        if(month==SENTINEL)break;

//calculations

//print output
            printf("%02d %02d %04d   %05d %10.2f  %10.2f\n",month,day,year,referenceNumber,withdrawalAmount,depositAmount);
}
            return 0;
}


[code]main.c: In function ‘main’:
main.c:37:35: error: expected ‘)’ before ‘!’ token
if (month == 02, day == 29, year !% 400 )
^
code]

Is This A Good Question/Topic? 1
  • +

Replies To: Simple Checkbook Program with Leap Year

#2 andrewsw   User is offline

  • Entwickler
  • member icon

Reputation: 6604
  • View blog
  • Posts: 26,911
  • Joined: 12-December 12

Re: Simple Checkbook Program with Leap Year

Posted 05 October 2018 - 02:30 AM

if  (month == 02, day == 29, year !% 400 )

That is not how you right a set of conditions, please review your study material.
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6445
  • View blog
  • Posts: 22,076
  • Joined: 05-May 12

Re: Simple Checkbook Program with Leap Year

Posted 05 October 2018 - 07:52 AM

The code above is C++, not C#. Moving...
Was This Post Helpful? 2
  • +
  • -

#4 Salem_c   User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2219
  • View blog
  • Posts: 4,302
  • Joined: 30-May 10

Re: Simple Checkbook Program with Leap Year

Posted 05 October 2018 - 08:35 AM

First, review the rules for leap years.
https://en.wikipedia.../wiki/Leap_year

> 33 if (month == 02, day == 29, year !% 400 )
> 34 printf("please enter a valid date");
So say 31st September would be valid by your logic?

Consider writing a function along the lines of
int isValidDate( int day, int month, int year ) {
    // validate all aspects of the date
    // return 1 if it's valid, 0 otherwise.
}




Also, be wary of writing "02" in the code just to make things look tidy.
The zero prefix on a number turns it into an octal (base 8) number.

Sure, 02 is the same as 2, but 012 is very different to 12, leading to all kinds of head scratching for newbies.
Was This Post Helpful? 2
  • +
  • -

#5 jplutts   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 10
  • Joined: 04-October 18

Re: Simple Checkbook Program with Leap Year

Posted 05 October 2018 - 09:36 PM

View PostSalem_c, on 05 October 2018 - 08:35 AM, said:

First, review the rules for leap years.
https://en.wikipedia.../wiki/Leap_year

> 33 if (month == 02, day == 29, year !% 400 )
> 34 printf("please enter a valid date");
So say 31st September would be valid by your logic?

Consider writing a function along the lines of
int isValidDate( int day, int month, int year ) {
    // validate all aspects of the date
    // return 1 if it's valid, 0 otherwise.
}




Also, be wary of writing "02" in the code just to make things look tidy.
The zero prefix on a number turns it into an octal (base 8) number.

Sure, 02 is the same as 2, but 012 is very different to 12, leading to all kinds of head scratching for newbies.


Thanks for the tips, how is this ?

int isValidDate( int day, int month, int year ) {
isValidDate(int month, int day, int year)
    {
        if (1 <= month && month <= 12)
            return false;
        if (1 <= day && day <= 31)
                return false;
            if ((day == 31) && (month == 2 || month == 4 || month == 6 || month == 9 || month == 11) || 
                return false;
            if ((day == 30) && (month == 2)
                return false;
            if ((month == 2) && (day == 29) && (year % 4 != 0) 
                return false;
            if ((month == 2) && (day == 29) && (year % 400 == 0) 
                return true;
            if ((month == 2) && (day == 29) && (year % 100 == 0)  
                return false;
            if ((month == 2) && (day == 29) && (year % 4 == 0) 
                return true;
            if ((month == 2) && (day > 29)
    
}


Was This Post Helpful? 0
  • +
  • -

#6 jplutts   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 10
  • Joined: 04-October 18

Re: Simple Checkbook Program with Leap Year

Posted 05 October 2018 - 10:13 PM

int isValidDate( int day, int month, int year ) {
isValidDate(int month, int day, int year)
    {
        if (1 <= month && month <= 12)
            return false;
        if (1 <= day && day <= 31)
                return false;
            if ((day == 31) && (month == 2 || month == 4 || month == 6 || month == 9 || month == 11) || 
                return false;
            if ((day == 30) && (month == 2)
                return false;
            if ((month == 2) && (day == 29) && (year % 4 != 0) 
                return false;
            if ((month == 2) && (day == 29) && (year % 400 == 0) 
                return true;
            if ((month == 2) && (day == 29) && (year % 100 == 0)  
                return false;
            if ((month == 2) && (day == 29) && (year % 4 == 0) 
                return true;
            if ((month == 2) && (day > 29)
    
}



[/quote]
I think I'm closer, but now I am getting these error codes
main.c: In function ‘isValidDate’:
main.c:40:17: error: expected expression before ‘int’
     isValidDate(int month, int day, int year)
                 ^
main.c:40:5: error: too few arguments to function ‘isValidDate’
     isValidDate(int month, int day, int year)
     ^
main.c:38:5: note: declared here
 int isValidDate( int day, int month, int year )
     ^
main.c:41:1: error: expected ‘;’ before ‘{’ token
 {
 ^
main.c:72:1: error: expected declaration or statement at end of input
 }
 ^
main.c: In function ‘main’:
main.c:72:1: error: expected declaration or statement at end of input
main.c:72:1: error: expected declaration or statement at end of input

Was This Post Helpful? 0
  • +
  • -

#7 Salem_c   User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2219
  • View blog
  • Posts: 4,302
  • Joined: 30-May 10

Re: Simple Checkbook Program with Leap Year

Posted 05 October 2018 - 10:30 PM

> 01 int isValidDate( int day, int month, int year ) {
> 02 isValidDate(int month, int day, int year)
Just delete line 2.

It's
int isValidDate( int day, int month, int year ) {
  // code goes here
}



Your last if() is missing a statement.
Was This Post Helpful? 1
  • +
  • -

#8 andrewsw   User is offline

  • Entwickler
  • member icon

Reputation: 6604
  • View blog
  • Posts: 26,911
  • Joined: 12-December 12

Re: Simple Checkbook Program with Leap Year

Posted 05 October 2018 - 11:53 PM

You have lots of mismatched parentheses.
Was This Post Helpful? 0
  • +
  • -

#9 jplutts   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 10
  • Joined: 04-October 18

Re: Simple Checkbook Program with Leap Year

Posted 06 October 2018 - 01:19 AM

#include <stdio.h>
#include <stdlib.h>

#define SENTINEL 99
int main(void)
{

// Create and initialize local variables
int month =0;
int day =0;
int year =0;
int referenceNumber =0;
double withdrawalAmount =0.0;
double depositAmount;

printf("Joseph P.  03\n");
printf("Program #2- Simple Checkbook Program\n\n");
printf("To exit, enter the input of 99\n\n");

printf("MM DD YYYY Reference# Withdrawal# Deposit\n");
printf("== == ==== =========  ==========  ======== \n");

//Start a loop for reading input
while(1)
{

//enter input
            printf("enter the MMDDYYYY Reference# Withdrawal Deposit: ");
            scanf("%2d %2d %4d %d %lg %lg", &month, &day, &year, &referenceNumber, &withdrawalAmount, &depositAmount);

{

int isValidDate( int day, int month, int year )

{
    
        if (1 <= month && month <= 12)
            return false;
        if (1 <= day && day <= 31)
                return false;
            if ((day == 31) && (month == 2 || month == 4 || month == 6 || month == 9 || month == 11) 
                return false;
            if ((day == 30) && (month == 2)
                return false;
            if ((month == 2) && (day == 29) && (year % 4 != 0) 
                return false;
            if ((month == 2) && (day == 29) && (year % 400 == 0) 
                return true;
            if ((month == 2) && (day == 29) && (year % 100 == 0)  
                return false;
            if ((month == 2) && (day == 29) && (year % 4 == 0) 
                return true;
            if ((month == 2) && (day > 29)
                return false;
}

}
//check for errors and SENTINEL
        if(month==SENTINEL)break;

//calculations

//print output
            printf("%02d %02d %04d   %05d %10.2f  %10.2f\n",month,day,year,referenceNumber,withdrawalAmount,depositAmount);
}
            return 0;
}


error codes


main.c: In function ‘isValidDate’:
main.c:45:20: error: ‘false’ undeclared (first use in this function)
             return false;
                    ^
main.c:45:20: note: each undeclared identifier is reported only once for each function it appears in
main.c:49:17: error: expected ‘)’ before ‘return’
                 return false;
                 ^
main.c:74:1: error: expected declaration or statement at end of input
 }
 ^
main.c: In function ‘main’:
main.c:74:1: error: expected declaration or statement at end of input
main.c:74:1: error: expected declaration or statement at end of input
main.c:74:1: error: expected declaration or statement at end of input


I need to declare false?

alternatively instead of return false can I use

if (1 <= month && month <= 12)
          {
 printf("\n\n Please enter a valid date!\n");
 }

under each if statement?

View Postandrewsw, on 05 October 2018 - 11:53 PM, said:

You have lots of mismatched parentheses.

the debugger said to add those, should it be different
Was This Post Helpful? 0
  • +
  • -

#10 Salem_c   User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2219
  • View blog
  • Posts: 4,302
  • Joined: 30-May 10

Re: Simple Checkbook Program with Leap Year

Posted 06 October 2018 - 01:40 AM

Two things.
1. C doesn't understand true and false by default (they come from C++). You have to do either
#define false 0
#define true 1


or
enum { false, true };



2. C doesn't do nested functions. Move your validate function body out of the body of main.
int isValidDate( int day, int month, int year ) {
   // code
}

int main ( ) {
   // code
   if ( month != SENTINEL && isValidDate(day,month,year) ) {
      // code
   }
   // code
}


Was This Post Helpful? 1
  • +
  • -

#11 jplutts   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 10
  • Joined: 04-October 18

Re: Simple Checkbook Program with Leap Year

Posted 06 October 2018 - 02:17 AM

View PostSalem_c, on 06 October 2018 - 01:40 AM, said:

Two things.
1. C doesn't understand true and false by default (they come from C++). You have to do either
#define false 0
#define true 1


or
enum { false, true };



2. C doesn't do nested functions. Move your validate function body out of the body of main.
int isValidDate( int day, int month, int year ) {
   // code
}

int main ( ) {
   // code
   if ( month != SENTINEL && isValidDate(day,month,year) ) {
      // code
   }
   // code
}


on line 7 this goes after
  if(month==SENTINEL)break;
?

// Joseph P.  03
// Program #2- Simple Checkbook Program
//

#include <stdio.h>
#include <stdlib.h>

#define SENTINEL 99
#define false 0
#define true 1

int isValidDate( int day, int month, int year )

{
    
        if (1 <= month && month <= 12)
            return false;
        if (1 <= day && day <= 31)
                return false;
            if ((day == 31) && (month == 2 || month == 4 || month == 6 || month == 9 || month == 11) 
                return false;
            if ((day == 30) && (month == 2)
                return false;
            if ((month == 2) && (day == 29) && (year % 4 != 0) 
                return false;
            if ((month == 2) && (day == 29) && (year % 400 == 0) 
                return true;
            if ((month == 2) && (day == 29) && (year % 100 == 0)  
                return false;
            if ((month == 2) && (day == 29) && (year % 4 == 0) 
                return true;
            if ((month == 2) && (day > 29)
                return false;

}

int main(void){

// Create and initialize local variables
int month =0;
int day =0;
int year =0;
int referenceNumber =0;
double withdrawalAmount =0.0;
double depositAmount;

printf("Joseph P.  03\n");
printf("Program #2- Simple Checkbook Program\n\n");
printf("To exit, enter the input of 99\n\n");

printf("MM DD YYYY Reference# Withdrawal# Deposit\n");
printf("== == ==== =========  ==========  ======== \n");

//Start a loop for reading input
while(1)
{

//enter input
            printf("enter the MMDDYYYY Reference# Withdrawal Deposit: ");
            scanf("%2d %2d %4d %d %lg %lg", &month, &day, &year, &referenceNumber, &withdrawalAmount, &depositAmount);


//check for errors and SENTINEL
        if(month==SENTINEL)break;
        if ( month != SENTINEL && isValidDate(day,month,year) ) {
//calculations

//print output
            printf("%02d %02d %04d   %05d %10.2f  %10.2f\n",month,day,year,referenceNumber,withdrawalAmount,depositAmount);
}

            return 0;
}

I took it out of the body of the main but now am getting this as an error code
main.c: In function ‘isValidDate’:
main.c:21:17: error: expected ‘)’ before ‘return’
                 return false;
                 ^
main.c:73:1: error: expected declaration or statement at end of input
 }
 ^


getting close ! lol again thank you for the help Salem
Was This Post Helpful? 0
  • +
  • -

#12 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7257
  • View blog
  • Posts: 15,137
  • Joined: 16-October 07

Re: Simple Checkbook Program with Leap Year

Posted 06 October 2018 - 03:30 AM

You could use a few more functions:
int isLeapYear(int year) { /* your code here */ }
int isValidMonth(int month) { ...



Actually, I was going to use your month check code to write isValidMonth, but then I looked at it:
if (1 <= month && month <= 12) {
    return false;
}


Whoa, look at that a lot closer. First, how does (month <= 12) make sense? The yoda form on the first half is also confusing. So, (month >= 1 && month <= 12). Hmmm...
int isValidMonth(int month) { return month >= 1 && month <= 12; }

int isValidDate( int day, int month, int year ) {
   // let's be positive
   if (isValidMonth(month) && day>=1) {
       // this is kind of pointless
       // if (1 <= day && day <= 31)
       // oops, and it was also doing the opposite of what you intended
       if (month==2) {
            // this is your special case
            if (isLeapYear(year)) {
                return day <= 29;
            } else {
                return day <= 28;
            }
       }
       // ...
   }
   return false;



First, returning a raw true or false should be avoided. If the result of a check returns a booleand, use that. Also, always use curlies! Yes, they are optional on single line blocks, but you will often find the block you thought was just one line isn't and life is easier if you're just consistent about the whole thing.

Another way to write this would be:
int isLeapYear(int year) { /* your code here */ }
int daysInMonth(int month, int year) { /* your code here */ }
int isValidDate(int day, int month, int year) { return day>=1 && day<=daysInMonth(month, year); }



This feels easier to reason about, as you have a function returning information and are doing a lot fewer conditionals.

Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#13 jplutts   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 10
  • Joined: 04-October 18

Re: Simple Checkbook Program with Leap Year

Posted 06 October 2018 - 09:30 PM

View Postbaavgai, on 06 October 2018 - 03:30 AM, said:

You could use a few more functions:
int isLeapYear(int year) { /* your code here */ }
int isValidMonth(int month) { ...



Actually, I was going to use your month check code to write isValidMonth, but then I looked at it:
if (1 <= month && month <= 12) {
    return false;
}


Whoa, look at that a lot closer. First, how does (month <= 12) make sense? The yoda form on the first half is also confusing. So, (month >= 1 && month <= 12). Hmmm...
int isValidMonth(int month) { return month >= 1 && month <= 12; }

int isValidDate( int day, int month, int year ) {
   // let's be positive
   if (isValidMonth(month) && day>=1) {
       // this is kind of pointless
       // if (1 <= day && day <= 31)
       // oops, and it was also doing the opposite of what you intended
       if (month==2) {
            // this is your special case
            if (isLeapYear(year)) {
                return day <= 29;
            } else {
                return day <= 28;
            }
       }
       // ...
   }
   return false;



First, returning a raw true or false should be avoided. If the result of a check returns a booleand, use that. Also, always use curlies! Yes, they are optional on single line blocks, but you will often find the block you thought was just one line isn't and life is easier if you're just consistent about the whole thing.

Another way to write this would be:
int isLeapYear(int year) { /* your code here */ }
int daysInMonth(int month, int year) { /* your code here */ }
int isValidDate(int day, int month, int year) { return day>=1 && day<=daysInMonth(month, year); }



This feels easier to reason about, as you have a function returning information and are doing a lot fewer conditionals.

Hope this helps.


now I am more confused than ever lol

// Joseph P.  03
// Program #2- Simple Checkbook Program
//

#include <stdio.h>
#include <stdlib.h>

#define SENTINEL 99
#define false 0
#define true 1

int isValidMonth(int month) { return month >= 1 && month <= 12; }

int isValidDate( int day, int month, int year ) {

    if (isValidMonth(month) && day>=1) {

        if (month==2) {

             if (isLeapYear(year)) {

                return day <= 29;

             } else {

                return day <= 28;

             }

        }


    }

return false;

int main(void){

// Create and initialize local variables
int month =0;
int day =0;
int year =0;
int referenceNumber =0;
double withdrawalAmount =0.0;
double depositAmount;

printf("Joseph P.  03\n");
printf("Program #2- Simple Checkbook Program\n\n");
printf("To exit, enter the input of 99\n\n");

printf("MM DD YYYY Reference# Withdrawal# Deposit\n");
printf("== == ==== =========  ==========  ======== \n");

//Start a loop for reading input
while(1)
{

//enter input
            printf("enter the MMDDYYYY Reference# Withdrawal Deposit: ");
            scanf("%2d %2d %4d %d %lg %lg", &month, &day, &year, &referenceNumber, &withdrawalAmount, &depositAmount);


//check for errors and SENTINEL
        if(month==SENTINEL)break;
        if ( month != SENTINEL && isValidDate(day,month,year) ) {
//calculations

//print output
            printf("%02d %02d %04d   %05d %10.2f  %10.2f\n",month,day,year,referenceNumber,withdrawalAmount,depositAmount);
}

            return 0;
}


something along these lines? I'm getting these errors
main.c: In function ‘isValidDate’:
main.c:20:18: warning: implicit declaration of function ‘isLeapYear’ [-Wimplicit-function-declaration]
              if (isLeapYear(year)) {
                  ^
main.c: In function ‘main’:
main.c:73:1: error: expected declaration or statement at end of input
 }
 ^
main.c: In function ‘isValidDate’:
main.c:73:1: error: expected declaration or statement at end of input


this isn't making sense to me because all my {} line up
Was This Post Helpful? 0
  • +
  • -

#14 Salem_c   User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2219
  • View blog
  • Posts: 4,302
  • Joined: 30-May 10

Re: Simple Checkbook Program with Leap Year

Posted 06 October 2018 - 11:31 PM

You still need to write isLeapYear as a function, like you did for isValidMonth.


> this isn't making sense to me because all my {} line up
Oh really :rolleyes2:/>
What are you using to edit your program with, notepad?
Get an editor which is at least capable of showing you matching brace pairs, and it's plainly obvious that you're missing something.
Attached Image

Also, apply a consistent indentation style as well, so it's easy to eyeball that braces are matching. Not to mention making it far easier to understand what is going on.
Here.
// Joseph P.  03
// Program #2- Simple Checkbook Program
//

#include <stdio.h>
#include <stdlib.h>

#define SENTINEL 99
#define false 0
#define true 1

int isValidMonth(int month)
{
  return month >= 1 && month <= 12;
}

int isValidDate(int day, int month, int year)
{
  if (isValidMonth(month) && day >= 1) {
    if (month == 2) {
      if (isLeapYear(year)) {
        return day <= 29;
      } else {
        return day <= 28;
      }
    }
  }
  return false;
}  //!! added to close isValidDate

int main(void)
{
  // Create and initialize local variables
  int month = 0;
  int day = 0;
  int year = 0;
  int referenceNumber = 0;
  double withdrawalAmount = 0.0;
  double depositAmount;

  printf("Joseph P.  03\n");
  printf("Program #2- Simple Checkbook Program\n\n");
  printf("To exit, enter the input of 99\n\n");

  printf("MM DD YYYY Reference# Withdrawal# Deposit\n");
  printf("== == ==== =========  ==========  ======== \n");

  //Start a loop for reading input
  while (1) {
    //enter input
    printf("enter the MMDDYYYY Reference# Withdrawal Deposit: ");
    scanf("%2d %2d %4d %d %lg %lg",
          &month, &day, &year, 
          &referenceNumber, &withdrawalAmount, &depositAmount);

    //check for errors and SENTINEL
    if (month == SENTINEL)
      break;

    if (month != SENTINEL && isValidDate(day, month, year)) {
      //calculations
      //print output
      printf("%02d %02d %04d   %05d %10.2f  %10.2f\n", 
             month, day, year, 
             referenceNumber, withdrawalAmount, depositAmount);
    }
  } //!! added to close while(1)

  return 0;
}


Was This Post Helpful? 0
  • +
  • -

#15 jplutts   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 10
  • Joined: 04-October 18

Re: Simple Checkbook Program with Leap Year

Posted 07 October 2018 - 01:34 AM

View PostSalem_c, on 06 October 2018 - 11:31 PM, said:

You still need to write isLeapYear as a function, like you did for isValidMonth.


> this isn't making sense to me because all my {} line up
Oh really :rolleyes2:/>/>
What are you using to edit your program with, notepad?
Get an editor which is at least capable of showing you matching brace pairs, and it's plainly obvious that you're missing something.
missingbrace.png

Also, apply a consistent indentation style as well, so it's easy to eyeball that braces are matching. Not to mention making it far easier to understand what is going on.
Here.
// Joseph P.  03
// Program #2- Simple Checkbook Program
//

#include <stdio.h>
#include <stdlib.h>

#define SENTINEL 99
#define false 0
#define true 1

int isValidMonth(int month)
{
  return month >= 1 && month <= 12;
}

int isValidDate(int day, int month, int year)
{
  if (isValidMonth(month) && day >= 1) {
    if (month == 2) {
      if (isLeapYear(year)) {
        return day <= 29;
      } else {
        return day <= 28;
      }
    }
  }
  return false;
}  //!! added to close isValidDate

int main(void)
{
  // Create and initialize local variables
  int month = 0;
  int day = 0;
  int year = 0;
  int referenceNumber = 0;
  double withdrawalAmount = 0.0;
  double depositAmount;

  printf("Joseph P.  03\n");
  printf("Program #2- Simple Checkbook Program\n\n");
  printf("To exit, enter the input of 99\n\n");

  printf("MM DD YYYY Reference# Withdrawal# Deposit\n");
  printf("== == ==== =========  ==========  ======== \n");

  //Start a loop for reading input
  while (1) {
    //enter input
    printf("enter the MMDDYYYY Reference# Withdrawal Deposit: ");
    scanf("%2d %2d %4d %d %lg %lg",
          &month, &day, &year, 
          &referenceNumber, &withdrawalAmount, &depositAmount);

    //check for errors and SENTINEL
    if (month == SENTINEL)
      break;

    if (month != SENTINEL && isValidDate(day, month, year)) {
      //calculations
      //print output
      printf("%02d %02d %04d   %05d %10.2f  %10.2f\n", 
             month, day, year, 
             referenceNumber, withdrawalAmount, depositAmount);
    }
  } //!! added to close while(1)

  return 0;
}



Yes I'm using notepad I have codeblocks on my laptop for school, but I work graveshift fulltime so I using work computer with an internet compiler. Is there a specific one you can recommend? I believe I added the isleapeyear function below attached.
// Joseph P.  03
// Program #2- Simple Checkbook Program
//

#include <stdio.h>
#include <stdlib.h>

#define SENTINEL 99
#define false 0
#define true 1

int isLeapYear(year)
{
  isLeapYear(int year)
   if year % 4 != 0;
   return false
   if year % 100 != 0;
   return true
   if year % 400 = 0;
   return true
   else
   return false
}
int isValidMonth(int month)
{
  return month >= 1 && month <= 12;
}

int isValidDate(int day, int month, int year)
{
  if (isValidMonth(month) && day >= 1) {
    if (month == 2) {
      if (isLeapYear(year)) {
        return day <= 29;
      } else {
        return day <= 28;
      }
    }
  }
  return false;
}  

int main(void)
{
  // Create and initialize local variables
  int month = 0;
  int day = 0;
  int year = 0;
  int referenceNumber = 0;
  double withdrawalAmount = 0.0;
  double depositAmount;

  printf("Joseph P.  03\n");
  printf("Program #2- Simple Checkbook Program\n\n");
  printf("To exit, enter the input of 99\n\n");

  printf("MM DD YYYY Reference# Withdrawal# Deposit\n");
  printf("== == ==== =========  ==========  ======== \n");

  //Start a loop for reading input
  while (1) {
    //enter input
    printf("enter the MMDDYYYY Reference# Withdrawal Deposit: ");
    scanf("%2d %2d %4d %d %lg %lg",
          &month, &day, &year, 
          &referenceNumber, &withdrawalAmount, &depositAmount);

    //check for errors and SENTINEL
    if (month == SENTINEL)
      break;

    if (month != SENTINEL && isValidDate(day, month, year)) {
      //calculations
      //print output
      printf("%02d %02d %04d   %05d %10.2f  %10.2f\n", 
             month, day, year, 
             referenceNumber, withdrawalAmount, depositAmount);
    }
  } 

  return 0;
}


but am getting this now
main.c: In function ‘isLeapYear’:
main.c:12:5: warning: type of ‘year’ defaults to ‘int’ [-Wimplicit-int]
 int isLeapYear(year)
     ^
main.c:15:4: error: expected ‘;’ before ‘if’
    if year % 4 != 0;
    ^
main.c:17:4: error: expected ‘;’ before ‘if’
    if year % 100 != 0;
    ^
main.c:19:4: error: expected ‘;’ before ‘if’
    if year % 400 = 0;
    ^
main.c:21:4: error: expected ‘;’ before ‘else’
    else
    ^

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2