The program returns many errors and I've tried for 2 days to figure out the problems
I '//' commented out the addRecord and enterRecordDetails and the program worked. I had written those functions in other programs.
Code:
#include "seatbooking.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int validateSectionLetter(char c)
{
if ((!isalpha(c)) || (c == 'Z') || (c == 'z'))
{
printf("\n** You entered an invalid section. Please enter a section from A to Y. **\n\n");
return 0;
}
return c;
}
char enterSectionLetter()
{
char c = 0;
do
{
printf("Enter Section [A - Y]: ");
c = getchar();
while (getchar() != '\n');
}
while(!validateSectionLetter(c));
return c;
}
void printGrid(int grid[][GRID_HEIGHT])
{
int x, y;
printf("\n\n | ");
for (x = 0; x < GRID_WIDTH; x++)
{
printf("%d ", x+1);
}
printf("\n");
printf("==");
for (x = 0; x < GRID_WIDTH; x++)
{
printf("==");
}
printf("\n");
for (y = 0; y < GRID_HEIGHT; y++)
{
printf("%c| ", y + 'A');
for (x = 0; x < GRID_WIDTH; x++)
{
printf("_ ");
}
printf("\n");
}
printf("\n");
}
// enter seat detail
void enterRecordDetails(User *user)
{
printf("Enter name: ");
scanf("%s", user->name);
printf("Enter surname: ");
scanf("%s", user->surname);
printf("Enter CountryCode: ");
scanf("%s", user->country);
printf("Enter Passport: ");
scanf("%s", user->passport);
}
// add record
int addRecord(const char *filename, const User *studentRecord)
{
FILE *file = fopen(filename, "ab");
if (file == NULL)
{
printf("Error opening file %s.\n", filename);
return 0;
}
fwrite(studentRecord, sizeof(User), 1, file);
fclose(file);
printf("Record saved to %s\n", filename);
return 1;
}
int validateMenuChoice(int choice)
{
if ((choice < 1) || (choice > 4))
{
printf("** You entered a invalid choice. Please choose an option from 1 to 4. ** \n\n");
return 0;
}
return 1;
}
int menu()
{
int choice = 0;
do
{
printf("** Olympics 2012 Seat Booking System **\n\n\n");
printf("1. Show Bookings\n");
printf("2. Book a seat\n");
printf("3. Display country stats\n");
printf("4. Exit\n");
printf("\nEnter Choice: ");
scanf("%d", &choice);
}
while (!validateMenuChoice(choice));
return choice;
}
int main()
{
int choice = 0;
int grid[GRID_HEIGHT][GRID_WIDTH] = {0};
choice = menu();
User user = {0};
char row;
char section = 'a';
int col;
switch (choice)
{
case 1:
break;
case 2:
enterSectionLetter();
printGrid(grid);
printf("\nEnter Row [A - J]: ");
scanf("%c",&row);
printf("\nEnter Seat Column Number [1 - 9]: ");
scanf("%d",&col);
enterRecordDetails(&user);
addRecord(FILENAME, &user);
break;
case 3:
break;
}
}