Thread: Help needed please.

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    18

    Help needed please.

    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;
    	}
    }

  2. #2
    Registered User
    Join Date
    Apr 2012
    Posts
    18
    This is the structure in the header file:

    Code:
    
    typedef struct User
    {
            char name[25];
            char surname[25];
            char country[3];
            char passport[10];
    }User;

  3. #3
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    Did you compile with warnings, or just duplicate your thread for no reason?

    ..\test.c:156: warning: missing braces around initializer
    ..\test.c:160: warning: unused variable 'section'
    ..\test.c:154: warning: unused variable 'grid'

    (and that's assuming the header file that you didn't include actually defines a lot of the constants that you use)

    Those hint to me that you just throw code at the compiler and hope it comes out right, rather than stepping through things manually to find your problem.

    What debugger are you using?

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  4. #4
    Registered User
    Join Date
    Apr 2012
    Posts
    18
    I had use those functions in a similar program and no errors were given by the compiler. I am using Visual Studio Professional.

  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    18
    Help needed please.-untitled-jpg

  6. #6
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    Let me give you a hint. The warnings (and "IntelliSense" warning) are telling you of things that look very suspicious. Those cause some of the errors (which are mainly just the compiler trying to pile on regardless and make sense of absolute junk) and should be easy to fix.

    Some are because of the warnings that I got from gcc and posted for you.

    And some look like they are caused by not including the header file or mis-spelling the names of things that are in the header file (but we can't help you there without the header file!)

    Once you clear up the warnings, which should be quite easy because they *tell* you what's wrong, most of those errors will disappear.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  7. #7
    Registered User
    Join Date
    Apr 2012
    Posts
    18
    Ok thank you a lot for your time
    I'll try and fix the errors and let you know

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help needed again
    By Vikramnb in forum C++ Programming
    Replies: 7
    Last Post: 01-09-2009, 02:03 PM
  2. Help needed!!
    By Vikramnb in forum C++ Programming
    Replies: 7
    Last Post: 01-09-2009, 09:20 AM
  3. help needed
    By golf in forum C Programming
    Replies: 1
    Last Post: 04-07-2008, 09:46 PM
  4. Help needed.
    By hyrule in forum C++ Programming
    Replies: 2
    Last Post: 09-26-2005, 09:51 AM
  5. C++ help needed?
    By atif in forum C++ Programming
    Replies: 2
    Last Post: 04-28-2002, 06:43 PM