i currently having problem finding out why the program currently looping infinite(i think so)
when i run it, it will run infinitely and won’t stop or print the next part
the first three function is for some context but all of them work perfectly fine
int calNoRec(int choice, int num, FILE** station, FILE** time) {
double display = 0;
int recTotal = 0, recNum;
chooseFileRead(choice, &station, &time);
while (fscanf(time, "%lf|", &display) != EOF)
recTotal++;
fclose(station);
fclose(time);
recNum = ceil(recTotal / num * 1.00);
return recNum;
}
int calStation(int choice, FILE** station, FILE** time) {
char stations[100];
int noStation = 0;
chooseFileRead(choice, &station, &time);
while (fscanf(station, "%[^n]n", stations) != EOF)
noStation++;
fclose(station);
fclose(time);
return noStation;
}
This is the part where it loop inifinitely
void searchFromToWithTime() {
FILE* time, * station;
int stationNum, recordNum;// how many times to loop
int choice = 0;// which line and direction
int enter = 0;// enter for press enter to continue
stationName s[100];
double display[100][100];
TicketAvailiableDetails ticket[1000], buy[100];
char departureStations[30], destinationStations[30];
double departureTimes;
int i, j, h, fileNo, departNum = -1, destiNum = -1, ticketNo, ticketWanted, buying = 0;
char repeat;
do
{
printf("Enter the departure station : ");
scanf("%[^n]", &departureStations);
rewind(stdin);
for (i = 0; i < 30; i++)
departureStations[i] = toupper(departureStations[i]);
printf("Enter the departure time : ");
scanf("%lf", &departureTimes);
rewind(stdin);
printf("Enter the destination station: ");
scanf("%[^n]", &destinationStations);
ticketNo = 0;
rewind(stdin);
for (i = 0; i < 30; i++)
destinationStations[i] = toupper(destinationStations[i]);
for (fileNo = 1; fileNo < 15; fileNo++)
{
stationNum = calStation(fileNo, &station, &time);
recordNum = calNoRec(fileNo, stationNum, &station, &time);
chooseFileRead(fileNo, &station, &time);
for (j = 0; j < recordNum; j++)
for (h = 0; h < stationNum; h++)
fscanf(time, "%lf|", &display[j][h]);
for (i = 0; i < stationNum; i++)
{
fscanf(station, "%[^n]n", s[i].name);
if (strcmp(departureStations, s[i].name) == 0)
departNum = i;
if (strcmp(destinationStations, s[i].name) == 0)
destiNum = i;
if (departNum != -1 && destiNum != -1)
if (departNum < destiNum)
{
for (j = 0; j < recordNum; j++)
{
if (display[departNum][j] > departureTimes)
{
strcpy(ticket[ticketNo].departureStation, departureStations);
strcpy(ticket[ticketNo].destinationStation, destinationStations);
ticket[ticketNo].departureTime = display[departNum][j];
ticket[ticketNo].destinationTime = display[destiNum][j];
ticketNo++;
}
}
}
}
departNum = -1, destiNum = -1;
fclose(station);
fclose(time);
}
if (ticketNo == 0)
printf("No Ticket Found!n");
else
{
for (i = 0; i < ticketNo; i++)
printf("%d. %s --> %s | %.2lf - %.2lfn", i + 1, ticket[i].departureStation, ticket[i].destinationStation, ticket[i].departureTime, ticket[i].destinationTime);
do
{
printf("Enter the ticket number wanted (-1 to exit): ");
scanf("%d", &ticketWanted);
rewind(stdin);
if (ticketWanted == -1)
exit(-1);
buy[buying] = ticket[ticketWanted-1];
printf("Buy another one(Y/N) :");
scanf("%c", &repeat);
repeat = toupper(repeat);
buying++;
} while (repeat == 'Y');
}
printf("Find another ticket? (Y/N)");
scanf("%c", &repeat);
repeat = toupper(repeat);
system("cls");
fclose(time);
fclose(station);
} while (repeat != 'Y');
}
find out why is it not working
New contributor
NotashiSan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
4