Thread: Text files and validation

  1. #16
    Registered User
    Join Date
    Oct 2010
    Posts
    29
    Yes, the input file is as follows:

    Code:
    1 9999
    1 5
    1 50
    5.00 900.00
    6.00 1000.00

    The code for this section originally looked like this:

    Code:
    		prodNum = getint(1,9999,"Product Number");
    		prodType = getint(1,5,"Product Type");
    		getstring(desc);
    		prodQuant = getint(1,50,"Product Quantity");
    		prodCost = getreal(5.00,900.00,"Product Cost");
    		prodPrice = getreal(6.00,1000.00,"Product Price");
    The assignment calls for having an external file provide the validation range numbers.

    So, the code is now:

    Code:
    while (fscanf(fp, "%lf%lf", &min, &max) != EOF)
    
           /* These use the getint() function  because they are int's */		
    		prodNum = getint(min,max,"Product Number");
    		prodType = getint(min,max,"Product Type");
    		getstring(desc);
    		prodQuant = getint(min,max,"Product Quantity");
    
           /* These use the getreal() function because they are doubles */
    
    		prodCost = getreal(min,max,"Product Cost");
    		prodPrice = getreal(min,max,"Product Price");
    		
    		fclose(fp);
    If you'd like, I can submit the code prior to this assignment so you can compile it and see how it works.

    This assignment only calls for changing the add(), getint(), and getreal() functions.

    And, thank you so much for your patience and help.
    Last edited by Futomara; 10-30-2010 at 11:17 AM.

  2. #17
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Code:
    while (fscanf(fp, "%lf%lf", &min, &max) != EOF)
    
           /* These use the getint() function  because they are int's */		
    		prodNum = getint(min,max,"Product Number");   //////// This will be in the above while.........
    		prodType = getint(min,max,"Product Type");  //////// From here down wil not
    		getstring(desc);
    		prodQuant = getint(min,max,"Product Quantity");
    
           /* These use the getreal() function because they are doubles */
    
    		prodCost = getreal(min,max,"Product Cost");
    		prodPrice = getreal(min,max,"Product Price");
    		
    		fclose(fp);
    In the above code your while will only execute one line of code because there are no {} surrounding the rest of the block. I don't think that is what you want.

    Since you are changing this program to get the input from the file instead of a user you will have to change your functions getint(), getreal(). They are also causing problems right now.


    Please explain what each line of your input file is:

    Code:
    1 9999   /// Is this min, max
    1 5          // What is this? Another min max?
    1 50
    5.00 900.00
    6.00 1000.00

    Jim

  3. #18
    Registered User
    Join Date
    Oct 2010
    Posts
    29
    In the above code your while will only execute one line of code because there are no {} surrounding the rest of the block. I don't think that is what you want.
    Correct... this is not what I want. I want it to cycle through each item.

    Since you are changing this program to get the input from the file instead of a user you will have to change your functions getint(), getreal(). They are also causing problems right now.
    Yes, but I don't know what to change in those functions.


    Please explain what each line of your input file is:
    Yes, each line is min, max. I showed an example of the code prior to this assignment in order that it may be clearer.

    So, I need to know where to put the braces and how to change the rest of the code so it all works. Though it's wrong, this is what I have so far:

    Code:
    /*=======================add()==================================================*/
    void add()
    {
    	int prodNum, prodType, prodQuant;
    	double prodCost, prodPrice;
    	char choice;
    	double costTotalByType[6]; /* define array to hold total costs by type (0 != category */
    	char desc[40];
    	
    	/* vars to have values read in from text file */
    	int min = 0, max = 0;
    	
    
    	FILE *fp; /*  create file pointer */
    
    	init_costs(costTotalByType, 6); /* initialize cost array to zero */
    	fp = fopen("limits.txt", "r"); /* open file for read only */
    	if (fp == NULL) 
    		{
    			printf("File does not exist");
    		}
    	do
    		{
    		title();
    		
    		/* display data entry items and take input */
    		
    		while (fscanf(fp, "%lf%lf", &min, &max) != EOF)
    		{
    		prodNum = getint(min,max,"Product Number");
    		prodType = getint(min,max,"Product Type");
    		getstring(desc);
    		prodQuant = getint(min,max,"Product Quantity");
    		prodCost = getreal(min,max,"Product Cost");
    		prodPrice = getreal(min,max,"Product Price");
    		}
    		fclose(fp);
    
    		costTotalByType[prodType] += prodCost;
    		show(prodNum, prodType, prodQuant, prodCost, prodPrice, desc);
    		show_costs(costTotalByType,6);
    		
    		/*  ask continue?	*/
    		printf("Continue (Y/N): ");
    		scanf("%c%*c", &choice);
    		choice = toupper(choice);
    		}
    	while (choice != 'N');
    
    }
    /*=====================getint()====================================================*/
    int getint(int min, int max, char item[])
    {
    	int n;
    
    	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);
    }
    /*=====================getreal()====================================================*/
    double getreal(double min, double max, char item[])
    {
    	double n;
    
    	printf("Enter the %s: %.2f to %.2f: ", item, min, max);
    	scanf("%lf%*c", &n);
    	while (n < min || n > max)
    		{
    		message("\nChoice not in range.\a");
    		printf("Enter the %s: %.2f to %.2f: ", item, min, max);
    		scanf("%lf%*c", &n);
    		}
    	return(n);
    }

  4. #19
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You need to change min max to doubles.

    However I think you need to step back and with pencil and paper figure out what you are doing.

    Did you get the input file from your instructor?


    with validation: product number (1 - 9999) */
    /* product type (1 - 5) */
    /* quantity (1 -50) */
    /* cost (5.00 - 900.00) */
    /* price (6.00 - 1000.00)
    It doesn't seem to have enough information to fill out this list.

    For now I would change this section of code to

    Code:
    while (fscanf(fp, "%lf%lf", &min, &max) != EOF)
    		{
                        printf("%lf %lf\n", min, max);
    /*		prodNum = getint(min,max,"Product Number");
    		prodType = getint(min,max,"Product Type");
    		getstring(desc);
    		prodQuant = getint(min,max,"Product Quantity");
    		prodCost = getreal(min,max,"Product Cost");
    		prodPrice = getreal(min,max,"Product Price");
    */
    		}
    and see if you print out all the correct items.

    Jim

  5. #20
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    Code:
    int getint(int min, int max, char *item)
    {
      int n;
      char tmp[20];
    
      do
      {
        printf("Enter the %s: %d to %d: ", item, min, max);
        fgets( tmp,20,stdin );
      } while( 1!=sscanf(tmp,"%d",&n) || n<min || n>max );
    
      return n;
    }
    /*=====================getreal()====================================================*/
    double getreal(double min, double max, char *item)
    {
      double n;
      char tmp[20];
    
      do
      {
        printf("Enter the %s: %.2f to %.2f: ", item, min, max);
        fgets( tmp,20,stdin );
      } while( 1!=sscanf(tmp,"%lf",&n) || n<min || n>max );
    
      return n;
    }
    Add also
    #undef UNICODE
    #undef _UNICODE
    before windows.h in your VStudio sources if you dont use unicode stuff.

  6. #21
    Registered User
    Join Date
    Oct 2010
    Posts
    29
    I don't think this will work because getint() is called in the menu() function with actual values instead of variables.

    Code:
    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);
    }

  7. #22
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    I dont unterstand why this not will work.

  8. #23
    Registered User
    Join Date
    Oct 2010
    Posts
    29

    Update

    I want to re-explain the problem, as I still do not have all the solutions.

    The assignment is to modify a prior lab. Here are the relevant lines of unmodified code from the prior lab:

    Code:
    /*=======================add()==================================================*/
    void add()
    {
    	int prodNum, prodType, prodQuant;
    	double prodCost, prodPrice;
    	char choice;
    	double costTotalByType[6]; /* define array to hold total costs by type (0 != category */
    	char desc[40];
    
    
    	init_costs(costTotalByType, 6); /* initialize cost array to zero */
    	do
    		{
    
    		title();
    
    		/* display data entry items and take input */
    		prodNum = getint(1,9999,"Product Number");
    		prodType = getint(1,5,"Product Type");
    		getstring(desc);
    		prodQuant = getint(1,50,"Product Quantity");
    		prodCost = getreal(5.00,900.00,"Product Cost");
    		prodPrice = getreal(6.00,1000.00,"Product Price");
    		costTotalByType[prodType] += prodCost;
    		show(prodNum, prodType, prodQuant, prodCost, prodPrice, desc);
    		show_costs(costTotalByType,6);
    
    		/*  ask continue?	*/
    		printf("Continue (Y/N): ");
    		scanf("%c%*c", &choice);
    		choice = toupper(choice);
    		}
    	while (choice != 'N');
    }
    /*=====================getint()====================================================*/
    int getint(int min, int max, char item[])
    {
    	int n;
    
    	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);
    }
    /*=====================getreal()====================================================*/
    double getreal(double min, double max, char item[])
    {
    	double n;
    
    	printf("Enter the %s: %.2f to %.2f: ", item, min, max);
    	scanf("%lf%*c", &n);
    	while (n < min || n > max)
    		{
    		message("\nChoice not in range.\a");
    		printf("Enter the %s: %.2f to %.2f: ", item, min, max);
    		scanf("%lf%*c", &n);
    		}
    	return(n);
    }
    The assignment instructions are to modify the prior Lab by adding code for manipulating a text file. The text file called “limits.txt” contains the validation ranges (minimum and maximum) for each validation of product number, product type, quantity, cost, and price. Place each validation range on a separate line, with minimum and maximum on each line separated by a space. The text file should look like this:

    Code:
    1 9999
    1 5
    1 50
    5.00 900.00
    6.00 1000.00
    Then, I am to remove the current validation values from my code and within my add() function, open my new text file and read the minimums and maximums into appropriate variables and use those variables in the calls to getint() and getreal().

    Here is what I came up with:

    Code:
    /*=======================add()==================================================*/
    void add()
    {
    	int prodNum, prodType, prodQuant;
    	double prodCost, prodPrice;
    	char choice;
    	double costTotalByType[6]; /* define array to hold total costs by type (0 != category */
    	char desc[40];
    	
    	/* vars to have values read in from text file */
    	int min= 0, max = 0;
    		
    	FILE *fp; /*  create file pointer */
    
    	init_costs(costTotalByType, 6); /* initialize cost array to zero */
    
    	fp = fopen("limits.txt", "rb"); /* open file for read only */
    	
    	if (fp == NULL) 
    		{
    			printf("File does not exist");
    		}
    	
     	while (fscanf(fp, "%d%d", &min, &max) != EOF)
    		{
    
    			do
    			{
    			title();
    					
    			/* display data entry items and take input */
    			
    			prodNum = getint(min,max,"Product Number");
    			prodType = getint(min,max,"Product Type");
    			getstring(desc);
    			prodQuant = getint(min,max,"Product Quantity");
    			prodCost = getreal(min,max,"Product Cost");
    			prodPrice = getreal(min,max,"Product Price");
    				
    			costTotalByType[prodType] += prodCost;
    			show(prodNum, prodType, prodQuant, prodCost, prodPrice, desc);
    			show_costs(costTotalByType,6);
    			
    			/*  ask continue?	*/
    			printf("Continue (Y/N): ");
    			scanf("%c%*c", &choice);
    			choice = toupper(choice);
    			fclose(fp);
    			}
    			while (choice != 'N');
    			
    		}	
    		
    }
    /*=====================getint()====================================================*/
    int getint(int min, int max, char item[])
    {
    	int n;
    
    	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);
    }
    /*=====================getreal()====================================================*/
    double getreal(double min, double max, char item[])
    {
    	double n;
    
    	printf("Enter the %s: %.2f to %.2f: ", item, min, max);
    	scanf("%lf%*c", &n);
    	while (n < min || n > max)
    		{
    		message("\nChoice not in range.\a");
    		printf("Enter the %s: %.2f to %.2f: ", item, min, max);
    		scanf("%lf%*c", &n);
    		}
    	return(n);
    }
    When I run the program, I do not get the right values for validation for each input. I only get 1 and 9999 for the int operations and 1.00 9999.00 for the double operations.

    Here is an example run:

    Code:
    Sierra Sporting Goods
    
    Enter the Product Number: 1 to 9999: 1
    Enter the Product Type: 1 to 9999: 1
    Enter the description: nerf footballs
    Enter the Product Quantity: 1 to 9999: 1
    Enter the Product Cost: 1.00 to 9999.00: 1
    Enter the Product Price: 1.00 to 9999.00: 2
    
    The product number is -->  0001
    The product type is ---->  1
    The description is ----->  Nerf Footballs
    The quantity is -------->  1
    The cost is ------------>  1.00
    The price is ----------->  2.00
    
    
    Total product price ----> 2.00
    Total product cost -----> 1.00
    Total product profit ---> 1.00
    
    
    Product Cost by Type
    
    Type       Dept               Cost
    ----------------------------------
     1         Camping            1.00
     2         Tennis             0.00
     3         Golf               0.00
     4         Snow Sports        0.00
     5         Water Sports       0.00
    
    Total                         1.00
    
    Continue (Y/N):
    Obviously, the values from the text file are not being properly read, or something. Is it because of a change needed in my getint() and getreal() functions? I am confused and need some help to solve this.

  9. #24
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Your problem description makes more sense now.

    In the following code:
    Code:
    while (fscanf(fp, "%d%d", &min, &max) != EOF)
    		{
    
    			do
    			{
    			title();
    					
    			/* display data entry items and take input */
    			
    			prodNum = getint(min,max,"Product Number");
    			prodType = getint(min,max,"Product Type");
    			getstring(desc);
    			prodQuant = getint(min,max,"Product Quantity");
    			prodCost = getreal(min,max,"Product Cost");
    			prodPrice = getreal(min,max,"Product Price");
    I would remove the while(scanf.......
    And change to:

    Code:
    			do
    			{
    			title();
    					
    			/* display data entry items and take input */
    			fscanf(fp, "%d%d", &min, &max);
    			prodNum = getint(min,max,"Product Number");
                            fscanf(fp, "%d%d", &min, &max);
                            prodType = getint(min,max,"Product Type");
                            getstring(desc);
                            fscanf(fp, "%d%d", &min, &max);
    			prodQuant = getint(min,max,"Product Quantity");
                            fscanf(fp, "%lf%lf", &doubleMin, &doubleMax);
    			prodCost = getreal(doubleMin,doubleMax,"Product Cost");
                            fscanf(fp, "%lf%lf", &doubleMin, &doubleMax);
    			prodPrice = getreal(doubleMin,doubleMax,"Product Price");
    You must create the doubleMin, doubleMax variables of type double.

    You will also have to move:

    Code:
            clearerr (fp);   /// Add this here to insure no eof() errors.
    
            fp = fopen("limits.txt", "rb"); /* open file for read only */
    	
    	if (fp == NULL) 
    		{
    			printf("File does not exist");
    		}
    To inside your do loop.


    Also if the user wants to do more records you need to clear the file errors. See cearerr()
    This is because when you read the last entry you might set the eof flag on your file.


    Jim

  10. #25
    Registered User
    Join Date
    Oct 2010
    Posts
    29
    Thanks for taking another look at my code.

    In testing out your solution, the program crashes because of the clearerr(fp) citing "The variable 'fp' is being used without being initialized."

    If I remove that line of code, here's the output. It's close, but the doubles are still wrong.

    Code:
    Sierra Sporting Goods
    
    Enter the Product Number: 1 to 9999: 1
    Enter the Product Type: 1 to 5: 1
    Enter the description: nerf balls
    Enter the Product Quantity: 1 to 50: 1
    Enter the Product Cost: 1.00 to 50.00: 1   <---- should be 5.00 - 900.00
    Enter the Product Price: 1.00 to 50.00: 2   <---- should be 6.00 - 1000.00
    {snip}
    So, this is starting to move in the right direction. The instructions say to modify the getint() and getreal() functions. I wonder if a single line of code to read the file is best used and then the getint() and getreal() functions are used to analyze how each is to be used. {scratches head}

  11. #26
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Try on your
    Code:
    if(fscanf(fp, "%lf%lf", &doubleMin, &doubleMax) != 2)
          printf("ERROR GETTING DOUBLES");
    else
          printf("%f   %f", doubleMin, doubleMax);
    And let me know what prints out.

    Jim
    Last edited by jimblumberg; 10-31-2010 at 12:10 PM.

  12. #27
    Registered User
    Join Date
    Oct 2010
    Posts
    29
    I went back through and found I didn't change a couple min and max to doubleMin and doubleMax. The program now outputs properly.

    Code:
    Sierra Sporting Goods
    
    Enter the Product Number: 1 to 9999: 1
    Enter the Product Type: 1 to 5: 1
    Enter the description: bouncy balls
    Enter the Product Quantity: 1 to 50: 1
    Enter the Product Cost: 5.00 to 900.00: 5
    Enter the Product Price: 6.00 to 1000.00: 6
    
    The product number is -->  0001
    The product type is ---->  1
    The description is ----->  Bouncy Balls
    The quantity is -------->  1
    The cost is ------------>  5.00
    The price is ----------->  6.00
    
    
    Total product price ----> 6.00
    Total product cost -----> 5.00
    Total product profit ---> 1.00
    
    
    Product Cost by Type
    
    Type       Dept               Cost
    ----------------------------------
     1         Camping            5.00
     2         Tennis             0.00
     3         Golf               0.00
     4         Snow Sports        0.00
     5         Water Sports       0.00
    
    Total                         5.00
    
    Continue (Y/N):
    I can't help but think that getint() and getreal() are supposed to be modified. But, this is working, so I don't see what could be done to them. Also, shouldn't I check for EOF and then fclose(fp)?

  13. #28
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Try to enter another set of entries, if it works you should be ok.

    But you should check the return value from scanf and insure you are getting your 2 parameters.

    Jim

  14. #29
    Registered User
    Join Date
    Oct 2010
    Posts
    29
    Well, you are right. It fails when I try to go through the loop again. Here is the current code I'm testing:

    Code:
    /*=======================add()==================================================*/
    void add()
    {
    	int prodNum, prodType, prodQuant;
    	double prodCost, prodPrice;
    	char choice;
    	double costTotalByType[6]; /* define array to hold total costs by type (0 != category */
    	char desc[40];	
    	int min= 0, max = 0; /* vars to have values read in from text file (both int and double)*/
    	double doubleMin= 0, doubleMax = 0;
    		
    	FILE *fp; /*  create file pointer */
    	fp = fopen("limits.txt", "r"); /* open file for read only */
    	
    	if (fp == NULL) /* make sure file exists */
    		{
    			printf("File does not exist");
    		}
    
    	init_costs(costTotalByType, 6); /* initialize cost array to zero */
    
    	do
    	{
    			title();
    					
    			/* display data entry items and take input */
    			fscanf(fp, "%d%d", &min, &max);
    			prodNum = getint(min,max,"Product Number");
    			fscanf(fp, "%d%d", &min, &max);
    			prodType = getint(min,max,"Product Type");
    			getstring(desc);
    			fscanf(fp, "%d%d", &min, &max);
    			prodQuant = getint(min,max,"Product Quantity");
    			fscanf(fp, "%lf%lf", &doubleMin, &doubleMax);
    			prodCost = getreal(doubleMin,doubleMax,"Product Cost");
    			fscanf(fp, "%lf%lf", &doubleMin, &doubleMax);
    			prodPrice = getreal(doubleMin, doubleMax,"Product Price");
    			/* verify EOF */
    			if (feof(fp))
    			{
    				/* close file */
    				fclose(fp);
    			}
    			else
    				{
    					printf("EOF was not reached. Data is corrupt.");
    				}
    			/* reset pointer to beginning of file */
    			//rewind(fp);
    							
    			costTotalByType[prodType] += prodCost;
    			show(prodNum, prodType, prodQuant, prodCost, prodPrice, desc);
    			show_costs(costTotalByType,6);
    			
    			/*  ask continue?	*/
    			printf("Continue (Y/N): ");
    			scanf("%c%*c", &choice);
    			choice = toupper(choice);
    	}
    	while (choice != 'N');			
    }
    The subsequent loop:

    Code:
    Sierra Sporting Goods
    
    Enter the Product Number: 1 to 50: 1
    Enter the Product Type: 1 to 50: 1
    Enter the description: damn
    Enter the Product Quantity: 1 to 50: 1
    Enter the Product Cost: 6.00 to 1000.00: 6
    Enter the Product Price: 6.00 to 1000.00: 7
    EOF was not reached. Data is corrupt.
    Last edited by Futomara; 10-31-2010 at 01:00 PM. Reason: to add information

  15. #30
    Registered User
    Join Date
    Oct 2010
    Posts
    29
    I got it!!

    Code:
    /*=======================add()==================================================*/
    void add()
    {
    	int prodNum, prodType, prodQuant;
    	double prodCost, prodPrice;
    	char choice;
    	double costTotalByType[6]; /* define array to hold total costs by type (0 != category */
    	char desc[40];	
    	int min= 0, max = 0; /* vars to have values read in from text file (both int and double)*/
    	double doubleMin= 0, doubleMax = 0;
    	
    	init_costs(costTotalByType, 6); /* initialize cost array to zero */
    
    	do
    	{
    
    			FILE *fp; /*  create file pointer */
    			fp = fopen("limits.txt", "r"); /* open file for read only */
    			if (fp == NULL) /* make sure file exists */
    				{
    					printf("File does not exist");
    				}
    
    			title();
    									
    			/* display data entry items and take input */
    			fscanf(fp, "%d%d", &min, &max);
    			prodNum = getint(min,max,"Product Number");
    			fscanf(fp, "%d%d", &min, &max);
    			prodType = getint(min,max,"Product Type");
    			getstring(desc);
    			fscanf(fp, "%d%d", &min, &max);
    			prodQuant = getint(min,max,"Product Quantity");
    			fscanf(fp, "%lf%lf", &doubleMin, &doubleMax);
    			prodCost = getreal(doubleMin,doubleMax,"Product Cost");
    			fscanf(fp, "%lf%lf", &doubleMin, &doubleMax);
    			prodPrice = getreal(doubleMin, doubleMax,"Product Price");
    			
    			/* close file */
    			fclose(fp);
    													
    			costTotalByType[prodType] += prodCost;
    			show(prodNum, prodType, prodQuant, prodCost, prodPrice, desc);
    			show_costs(costTotalByType,6);
    			
    			/*  ask continue?	*/
    			printf("Continue (Y/N): ");
    			scanf("%c%*c", &choice);
    			choice = toupper(choice);
    	}
    	while (choice != 'N');			
    }
    I want to thank everyone, especially Jim, for contributing. Now, I am going to relax for a while. Again, thanks!!

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