Thread: Text files and validation

  1. #31
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    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,817
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

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, 05:07 PM