'trying to validate an entry for a date and loop through for each shift of an employee but getting infinite loop
I'm trying to validate an entry for a date and loop through for each shift of an employee but getting infinite loop when the entry is invalid and when the entry is valid its not looping through the function again for the other shifts its just terminating the code after the date is entered.
#include <iostream>
using namespace std;
int main() {
char employeeName[20];
int i;
char trash[10000];
int employeeNumber =1;
int noShiftsWorked;
int currentShift;
int day,month,year;
int date;
while(employeeNumber !=0){//loop to go through process for each employee
printf("please enter employees name:");
scanf ("%s",employeeName);
while (1)//loop to insure user puts in a valid number for the number of shifts worked by the employee
{
printf("How many shifts did %s work this week?",employeeName);
fflush(stdout);
if (1 == scanf("%d", &noShiftsWorked))
break;
/* scanf failed: clear the bad input from stdin */
if (NULL == fgets(trash, sizeof(trash), stdin)) /* NOTE: assumes 1 entry per line and no line longer than 10000 characters */
exit((fprintf(stderr, "Unexpected EOF or error!\n"), 1));
puts("You did not enter a valid number");
}
for(currentShift=1;currentShift<noShiftsWorked+1;currentShift++){
while(date!=1){
printf("Enter the date of shift %d in format dd/mm/yyyy",currentShift);
scanf ("%d/%d/%d",day,month,year);
int IsValidDate(int,int,int);
date = IsValidDate(year,month,day);
}
}
}
return 0;
}
int IsValidDate(int year, int month, int day)
{
unsigned int leap;
unsigned char mon_day[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
/* check the rang of the year */
if ((year < 1) || (year >= 3200))
{
return 0;
}
if ((month < 1) || (month > 12))
{
return 0;
}
/* if it's leap year */
if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
{
leap = 1;
}
else
{
leap = 0;
}
mon_day[1] += leap;
if ((day > mon_day[month - 1]) || (day < 1))
{
return 0;
}
return 1;
}
I seems as though the check is working correctly but what I want the code to do when the date check runs is malfunctioning in both valid and invalid cases
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
