Click to See Complete Forum and Search --> : Request help with loop


Jody
April 25th, 1999, 09:18 PM
I'm having problems trying to figure out how to use loops. I want my program to promt the user 5 times for year, month, and day. I then want each of those 5 dates to display and show the other date formats (in columns). Currently it displays each one right after the user completes entry.



// I can't get the loop to work. I want the user to be prompt for 5 dates and
// then have those dates display in columns.

/*********************************************************************/
/* This program will take a date in U.S. date format (mm/dd/yy) */
/* and convert it to all of the following formats. */
/* Date formats are as follows: */
/* */
/* European date format: dd/mm/yy */
/* Julian date format: yyjjj */
/* Military date format: dd-MON-yyyy */
/* Business date format: dd month yyyy */
/* */
/*********************************************************************/

#include<stdio.h>
#include <conio.h>
#define MONTHS 12

int main()
{
int indexj,index;
printf("\nThis program will convert up to five dates into the US, \n");
printf("European, the Julian, military, and business formats.\n");
char again='y';
while (again == 'Y' || again == 'y')
{
//define arrays for days and months
int days[MONTHS]={31,28,31,30,31,30,31,31,30,31,30,31};
char mon[12][4]={"JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"};
char month[12][10]={"January","February","March","April","May","June","July","August","September","October","November","December"};

//define arrays for conversion
int mm[5];
int dd[5];
int yy[5];
int jjj[5];
int yyyy[5];
for (index=0;index<5;index++)
{
days[1]=28;
//define variables
printf("Please input a four digit year:\n"); //input year
scanf("%d",&yy[index]);
while (!(yy[index] > 1899 && yy[index] < 2200)) //error check
{
printf ("\aPlease enter a year between 1900 and 2199:\n");
scanf("%d",&yy[index]);
}
if ((yy[index] % 100 == 0 && yy[index] % 400 == 0)||(yy[index] % 100 != 0 && yy[index] % 4 ==0))
{
days[1]=days[1]+1;
}
printf("Please input month as a number between 1 and 12:\n"); //input month
scanf("%d",&mm[index]);
while (mm[index] < 1 || mm[index] > 12) //error check
{
printf ("\aPlease enter a number between 1 and 12:\n");
scanf("%d",&mm[index]);
}
printf("Please input day as a number:\n"); //input day
scanf("%d",&dd[index]);
while (dd[index] < 1 || dd[index] > days[mm[index]-1]) //error check
{
printf ("\aPlease re-enter a valid number :\n");
scanf("%d",&dd[index]);
}
if ( yy[index] <2000) //adjusts for two digit years
{
yyyy[index] = yy[index]-1900;
}
if (yy[index] >= 2000 && yy[index] < 2100)
{
(yyyy[index] = yy[index]-2000);
}
if (yy[index] >= 2100)
{
(yyyy[index] = yy[index]-2100);
}
//converting into julian
jjj[index]=0;
for (indexj=1;indexj<=mm[index]-1;indexj++)
jjj[index] += days[indexj-1]; //add days up
clrscr();

//print results
printf("\nUS date:\tEuropean:\t Julian:\tMilitary:\tBusiness\n");
printf("%2d/%2d/%2d\t%2d/%2d/%2d\t %2d%-3d\t\t%d-%s-%d\t%d %s %d \n", mm[index], dd[index], yyyy[index], dd[index], mm[index], yyyy[index], yyyy[index], jjj[index]+dd[index], dd[index], mon[mm[index]-1], yy[index], dd[index], month[mm[index]-1], yy[index]);
}
}
}

Smriti
April 27th, 1999, 03:43 AM
Hi,

All you have to do is after your first 'for' loop ends put in another 'for' loop with the two printf statements for printing in other formats. The loop is as follows:

printf("\nUS date:\tEuropean:\t Julian:\tMilitary:\tBusiness\n");
for (index=0;index<5;index++)
{

printf("%2d/%2d/%2d\t%2d/%2d/%2d\t %2d%-3d\t\t%d-%s-%d\t%d %s %d \n", mmindex], dd[index], yyyy[index], dd[index], mm[index], yyyy[index], yyyy[index],
jjj[index]+dd[index], dd[index], mon[mm[index]-1], yy[index], dd[index],
month[mm[index]-1], yy[index]);

}

Make sure you have taken out the previous printf statements.This should get your loop working allright.

There is another flaw in the coding, you have not accepted any input from the user to end the main while loop. you might want to ask the user if he wishes to continue or not.

Hope this helps.

Regards,
Smriti Venkatesh
smritivenkatesh@aditi.com

LALeonard
April 27th, 1999, 12:10 PM
Is it just me or are you posting the same question over and over again?

LA Leonard - http://www.DefinitiveSolutions.com