Text files and validation

This is a discussion on Text files and validation within the C Programming forums, part of the General Programming Boards category; How about : Code: int getint(FILE *fp, char item[]) { int n; int min; int max; fscanf(fp, "%d%d", &min, &max); ...

  1. #31
    Registered User
    Join Date
    Mar 2009
    Posts
    343
    How about :

    Code:
    int getint(FILE *fp, char item[])
    {
        int n;
        int min;
        int max;
    
        fscanf(fp, "%d%d", &min, &max);
      
        printf("Enter the %s: %d to %d: ", item, min, max);
        scanf("%d%*c", &n);
        while (n < min || n > max)
            {
            message("\nChoice not in range.\a");
            printf("Enter the %s: %d to %d: ", item, min, max);
            scanf("%d%*c", &n);
            }
        return(n);
    }
    Make a similar change in getreal() but use floats/doubles instead of ints.

    This will avoid a lot of redundant code - since each call to get* has an fscanf into variables which are used just in that function, move the call there. It'll clean up the calling code significantly.

  2. #32
    Registered User
    Join Date
    Oct 2010
    Posts
    29
    Won't that cause a problem with this function when it's called here (red)?:

    Code:
    /*========================menu()=================================================*/
    int menu()
    {
    	int choice;
    
    	title();
    
    	/* display menu items */
    	printf("\t1 = Add a record\n");
    	printf("\t2 = Report\n");
    	printf("\t3 = Delete a record\n");
    	printf("\t4 = Change a record\n");
    	printf("\t5 = Quit\n\n");
    
    	/* display selection question and validate */
    	choice = getint(1,5,"choice");
    
    	return (choice);
    }

  3. #33
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,788
    Just a nit but...
    Code:
    char *department[] = {"Camping", "Tennis", "Golf", "Snow Sports", "Water Sports"};
    That should be:
    Code:
    const char *department[] = {"Camping", "Tennis", "Golf", "Snow Sports", "Water Sports"};
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #34
    Registered User linuxlover's Avatar
    Join Date
    Nov 2010
    Location
    INDIA
    Posts
    52
    anybody help me to solve my problem in my todays post?.................

  5. #35
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,955
    Quote Originally Posted by linuxlover
    anybody help me to solve my problem in my todays post?
    You don't seem to have a posted a problem in this thread. If you're talking about another thread, then just be patient.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 3 of 3 FirstFirst 123
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. input validation, kinda
    By dynamethod in forum C++ Programming
    Replies: 1
    Last Post: 09-15-2008, 12:52 AM
  2. help me with files
    By xclarkiex5x in forum C++ Programming
    Replies: 27
    Last Post: 11-05-2007, 04:07 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21