Constructive criticism/suggestions
I finally took the plunge, decided to register and join in on the discussion. :)
I am using the book "Practical C Programming" to help teach myself how to program in C. I've gone through the beginning chapters and done one of the exercises. I was hoping I could get suggestions on how to improve on it or ways I could do things more efficiently. The exercise reads as follows:
Write a program to perform date arithmetic such as how many days there are between 6/6/90 and 4/3/92.
This is what I have so far:
Code:
#include <stdio.h>
int dayspassed(int month, int day, int year, int sMonth, int sDay, int sYear);
int main()
{
char buffer[20] = {0};
int day = 0, month = 0, year = 0;
int sDay = 0, sMonth = 0, sYear = 0;
printf("\nInput first date in MM/DD/YY format: ");
fgets(buffer, sizeof(buffer), stdin);
sscanf(buffer, "%d/%d/%d", &month, &day, &year);
if ((month > 12) || (day > 31))
printf("Error: Invalid date.\n");
else
printf("You chose %d/%d/%d.\n", month, day, year);
printf("\nInput second date in MM/DD/YY format: ");
fgets(buffer, sizeof(buffer), stdin);
sscanf(buffer, "%d/%d/%d", &sMonth, &sDay, &sYear);
if ((sMonth > 12) || (sDay > 31))
printf("Error: Invalid date.\n");
else
printf("You chose %d/%d/%d.\n", sMonth, sDay, sYear);
printf("The number of days between %d/%d/%d and %d/%d/%d is %d.",
month, day, year, sMonth, sDay, sYear, dayspassed(month, day, year, sMonth, sDay, sYear));
return (0);
}
int dayspassed(int month, int day, int year, int sMonth, int sDay, int sYear)
{
int diffDay = 0, diffMonth = 0, diffYear = 0, days = 0;
diffDay = sDay - day;
diffMonth = sMonth - month;
diffYear = sYear - year;
days = diffDay + (diffMonth * 31) + (diffYear * 365);
return (days);
}
I'm mainly looking for information on some methods of input validation, but suggestions on features to add would be great also.
I also was wondering if there is any way to initialize all the variables in one line to 0. Take this piece of code for example:
Code:
int day = 0, month = 0, year = 0;
int sDay = 0, sMonth = 0, sYear = 0;
I figured you could just do day = month = year = 0 but that doesn't seem to work.
Thanks in advance and flame away if I did something stupid. :cool:
Easy to Read Chopped Down Version!
Quote:
I also was wondering if there is any way to initialize all the variables in one line to 0. Take this piece of code for example:
Code:
#include <stdio.h>
int main()
{
int month=0, day=0, year=0, sMonth=0, sDay=0, sYear=0, dMonth=0, dDay=0, dYear=0, days=0;
printf ("Input first date in MM/DD/YY format: \n\n");
scanf ("%d/%d/%d", &month, &day, &year);
if ((month > 12) || (day > 31))
printf("Error: Invalid date.\n");
else
printf("You chose %d/%d/%d.\n\n", month, day, year);
printf("Input second date in MM/DD/YY format: \n\n");
scanf("%d/%d/%d", &sMonth, &sDay, &sYear);
if ((sMonth > 12) || (sDay > 31))
printf("Error: Invalid date.\n");
else
printf("You chose %d/%d/%d\n\n", sMonth, sDay, sYear);
dDay = sDay - day;
dMonth = sMonth - month;
dYear = sYear - year;
days = dDay + (dMonth * 31) + (dYear * 365);
printf("The number of days between %d/%d/%d and %d/%d/%d is %d.\n", month, day, year, sMonth, sDay, sYear, days);
return (0);
}
~EvoTone~
__________________________________________________ ____________
If you wake up every morning and can think of nothing else but, programming you are a programmer!