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; Good Morning, I have been pulling my hair out trying to understand how to implement the use of a text ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    29

    Text files and validation

    Good Morning,

    I have been pulling my hair out trying to understand how to implement the use of a text file in a section of code where functions are used for validation.

    The add() function had the actual validation ranges instead of variables. The task at hand is to use an external file to pass in the validation to the variables min and max, which I added in place of the numbers. Perhaps I'm wrong here. Anyway, the text file should be as follows:

    1 9999
    1 5
    1 50
    5.00 900.00
    6.00 1000.00

    So, I removed the current validation values from my code and replaced them with the variables min and max within my add() function> Next I open my new text file and need to read the minimums and maximums into appropriate variables and use those variables in my calls to getint() and getreal(). This latter part is where I am totally lost.

    I would appreciate any help in figuring this out. I don't know why I am so confused. I don't know how to 'pull' the data out of the text file and 'place' it in the variables. Especially since the type of variable changes (from int to double) in some places.

    Here's my code:

    Code:
    /*--------------------------------------------------------------------------*/
    
    /*																			*/
    /*	Description:	This program is to accept data entry and then display	*/
    /*					the results for a fictitious company. Multiple data		*/
    /*					entry to be accepted until operator quits. Data			*/
    /*					to be calculated and displayed.							*/
    /*					with validation:	product number (1 - 9999)			*/
    /*										product type (1 - 5)				*/
    /*										quantity (1 -50)					*/
    /*										cost (5.00 - 900.00)				*/
    /*										price (6.00 - 1000.00)				*/
    /*					Add 7 functions:	main(), menu(), add(), getreal(),	*/
    /*										getint(), total(), and  show().		*/
    /*					Include category array: 1 = Camping, 2 = Tennis,		*/
    /*					(with running totals)	3 = Golf, 4 = Snow sports,		*/
    /*					(display totals)		5 = Water sports				*/
    /*					Include product description: create new function		*/
    /*					getstring() that uses gets() for keyboard input,		*/
    /*					new function strcase() to convert desc. first			*/
    /*					letter of each word uppercase + balance lowercase,		*/
    /*					display new desc. in show(), create 2D array for		*/
    /*					showcosts() dept.name									*/
    /*					Convert init-costs() & show-costs() to use pointers		*/
    /*					Also, modify getstring() and all string functions		*/
    /*					Add limits.txt to be used for validation (remove old)	*/
    /*																			*/
    
    /*	Compiler:		Microsoft Visual C++ 2010 Express (Windows XP)			*/
    /*																			*/
    /*--------------------------------------------------------------------------*/
    
    /* Disable warning messages 4996 - (scanf deprecated; use scanf_s) */
    #pragma warning( disable : 4996 )
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <string.h>
    
    #define COMPANY "Sierra Sporting Goods" /* define company for potential multiple uses */
    #define TRUE 1
    
    
    int menu(void);
    void add(void);
    int getint(int min, int max, char prompt[]);
    double getreal(double min, double max, char prompt[]);
    double total(int q, double c);
    void show(int prodNum, int prodType, int prodQuant, double prodCost, double prodPrice, char desc[]);
    void title(void);
    void message(char msg[]);
    int quit(void);
    void init_costs(double *c, int t);
    void show_costs(double *c, int t);
    void getstring(char *desc);
    void strcase(char *desc);
    char *department[] = {"Camping", "Tennis", "Golf", "Snow Sports", "Water Sports"};
    
    main() /* main function  */
    {
    	int selection, active = TRUE;
    do
    	{
    	selection = menu();
    		switch (selection)
    		{
    			case 1: add(); break;
    			case 2:
    			case 3:
    			case 4:
    			case 5: active = quit(); break;
    		}
    	}
    while(active);
    return 0;
    }
    /*========================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);
    }
    /*=======================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  (Is this right?)*/
            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 */
    
                    /* what do I put here to get the values from the text file into the variables min and 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");
    		
    		fclose(fp); /* does this go here? */
    
    		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');
    
    }
    
    /* How do I modify the following two functions?  */
    
    /*=====================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);
    }
    /*===================show()======================================================*/
    void show(int prodNum, int prodType, int prodQuant, double prodCost, double prodPrice, char desc[])
    {
    		printf("\nThe product number is -->  %0.4d", prodNum);
    		printf("\nThe product type is ---->  %d", prodType);
    		/* convert description to proper case structure */
    		strcase(desc);
    		printf("\nThe description is ----->  %s", desc);
    		printf("\nThe quantity is -------->  %d", prodQuant);
    		printf("\nThe cost is ------------>  %.2f", prodCost);
    		printf("\nThe price is ----------->  %.2f\n", prodPrice);
    		/* display totals	*/
    		printf("\n\nTotal product price ----> %.2f", total(prodQuant, prodPrice));
    		printf("\nTotal product cost -----> %.2f", total(prodQuant, prodCost));
    		printf("\nTotal product profit ---> %.2f\n\n", (total(prodQuant, prodPrice) - total(prodQuant, prodCost)));
    }
    /*=================total()========================================================*/
    double total(int q, double c)
    {
    	return(q*c);
    }
    /*=====================message()====================================================*/
    void message(char msg[])
    {
    	printf("%s\n", msg);
    }
    /*====================title()=====================================================*/
    void title()
    {
    	/* clear screen */
    	system("cls");
    
    	/* display title */
    	printf("%s\n\n", COMPANY);
    }
    /*====================quit()=====================================================*/
    int quit()
    {
    	system("cls");
    	message("\n\tGoodbye!\n");
    	return 0;
    }
    /*=====================init_costs()==============================================*/
    void init_costs(double *c, int t)
    {
    	int i;
    
    	for (i = 1; i < t; i++)
    	{
    		*(c+i) = 0;
    	}
    }
    /*====================show_costs()===============================================*/
    void show_costs(double *c, int t)
    {
    	int i;
    	double total = 0;
    
    
    	printf("\nProduct Cost by Type\n\n");
    	printf("Type       Dept               Cost\n");
    	printf("----------------------------------\n");
    	for (i = 1; i < t ; i++)
    	{
    		printf("%2d         %-13s %9.2lf\n", i, *(department+i-1), *(c+i));
    		total += *(c+i);
    	}
    	printf("\nTotal%29.2lf\n\n", total);
    }
    /*====================getstring()===========================================*/
    void getstring(char *desc)
    {
    	printf("Enter the description: ");
    	gets(desc);
    }
    /*====================strcase()=============================================*/
    void strcase(char *desc)
    {
    	int i = 1;
    	/* convert first ch to upper */
    	*(desc) = toupper(*desc);
    	while (*(desc+i) != (int)NULL)
    	{
    		*(desc+i) = tolower(*(desc+i)); /* convert to lower */
    		if (*(desc+i) == ' ') /* convert ch after space to upper */
    		{
    			i++;
    			*(desc+i) = toupper(*(desc+i));
    		}
    		i++;
    	}
    }
    /*=========================================================================*/

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    First thing you need a consistent file format... Changing the number of entries per line, more or less randomly is going to make saving and loading the file extremely complex.

    Secondly, I would open and store the content of that file right at program startup, rather than letting an operator spend time with it only to get an error --losing entered data in the process-- half way through.

    You might also want to consider an array or struct that holds your min max file contents... it's much easier to write memory data than a gazillion individual variables.
    Last edited by CommonTater; 10-30-2010 at 08:12 AM.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    29
    CommonTater - Thank you for a quick response. I am a student taking a C Programming course. So, my above problem is one in which the file was defined by the professor and cannot be modified. Likewise, the file must be open and read in the add() function.

    This program was fully functional with the values from the file placed in the min and max variables, in order. The task at hand is to get the data from an external file and have it read into the variables.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    To read the file content, take a look at fscanf() function.

    FWIW... your professor needs to get up to speed on safer data processing methods. If there is a risk of failure, you always want a program to fail *before* someone spends 3 hours typing data into it.

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    29
    I have tried unsuccessfully to use the following in the add() function (using fscanf below the comment):

    Code:
     /* what do I put here to get the values from the text file into the variables min and max? */
    while (fscanf(fp, "%s%s", min, max) != EOF)
    While the code compiles without error, the program crashes upon selecting the 1 menu item.

    Is this the correct code to use? Is it crashing because I need to modify something in the getint() and getreal() functions.

    I've been working this for days.

    BTW, it's an online class and the professor does get back to me within a day, but his answers are as cryptic as the problem. I have yet to ask for his help with this problem. I actually want to learn!

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Futomara View Post
    I have tried unsuccessfully to use the following in the add() function (using fscanf below the comment):

    Code:
     /* what do I put here to get the values from the text file into the variables min and max? */
    while (fscanf(fp, "%s%s", min, max) != EOF)
    While the code compiles without error, the program crashes upon selecting the 1 menu item.

    Is this the correct code to use? Is it crashing because I need to modify something in the getint() and getreal() functions.
    One reason it's crashing, is that you need to add an & (ampersand), before the min and max in your fscanf() line of code. Since scanf() and fscanf(), etc., modify the variables with new values, it needs the *address* of the variable (although string names ARE addresses, but numbers and chars are not, so they need that ampersand).



    I've been working this for days.

    BTW, it's an online class and the professor does get back to me within a day, but his answers are as cryptic as the problem. I have yet to ask for his help with this problem. I actually want to learn!
    Neither is out of the ordinary. Learning C is like learning a foreign language. It takes time and lots of practice.

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    29
    Great! That has me one step closer. My line of code now reads:

    Code:
    while (fscanf(fp, "%s%s", &min, &max) != EOF)
    The program runs, but the data for the validation ranges is not as it should be. I suspect it is returning the actual memory locations and not the numeric values from the file. Here's what it shows:

    Code:
    Enter the Product Number: 49 to 960051513:
    instead of:

    Code:
    Enter the Product Number: 1 to 9999:
    I think perhaps my declaration of min and max is wrong. Instead of:

    Code:
    int min = 0, max = 0;
    Should it be something else?

    Or, is the problem now in the getint() and getreal() functions?
    Last edited by Futomara; 10-30-2010 at 09:11 AM.

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    2,917
    Code:
    while (fscanf(fp, "%s%s", &min, &max) != EOF)
    Check your format specifiers for scanf(). Hint the "%s" is incorrect.


    Jim

  9. #9
    Registered User
    Join Date
    Oct 2010
    Posts
    29
    I tried %f thinking it would pass the int's and the doubles. The numbers were still wrong. If I use %d, then when the value being passed is a double, it truncates the decimal portion. In addition, the program is only prompting for 'Enter the Product Number' and then cycling through the validation numbers. The other prompts are bypassed before the program fails. So, there is more wrong with my code. I suspect my while loop is wrong. And, there are supposed to be changes made to the getint() and getreal() functions. I have no idea what to even change.

    I'm at the point where I am so frustrated, I am shutting down. I really could use some help if someone could point out my mistakes and set me in the right direction. I have come much further by the help from this forum than I did by myself over the past few days. My confidence is slowly declining. Up until now, I've been doing great in this class.

    Here is my current code:

    Code:
    /*	Description:	This program is to accept data entry and then display	*/
    /*					the results for a fictitious company. Multiple data		*/
    /*					entry to be accepted until operator quits. Data			*/
    /*					to be calculated and displayed.							*/
    /*					with validation:	product number (1 - 9999)			*/
    /*										product type (1 - 5)				*/
    /*										quantity (1 -50)					*/
    /*										cost (5.00 - 900.00)				*/
    /*										price (6.00 - 1000.00)				*/
    /*					Add 7 functions:	main(), menu(), add(), getreal(),	*/
    /*										getint(), total(), and  show().		*/
    /*					Include category array: 1 = Camping, 2 = Tennis,		*/
    /*					(with running totals)	3 = Golf, 4 = Snow sports,		*/
    /*					(display totals)		5 = Water sports				*/
    /*					Include product description: create new function		*/
    /*					getstring() that uses gets() for keyboard input,		*/
    /*					new function strcase() to convert desc. first			*/
    /*					letter of each word uppercase + balance lowercase,		*/
    /*					display new desc. in show(), create 2D array for		*/
    /*					showcosts() dept.name									*/
    /*					Convert init-costs() & show-costs() to use pointers		*/
    /*					Also, modify getstring() and all string functions		*/
    /*					Add limits.txt to be used for validation (remove old)	*/
    /*																			*/
    /*	Compiler:		Microsoft Visual C++ 2010 Express (Windows XP)			*/
    /*																			*/
    /*--------------------------------------------------------------------------*/
    
    /* Disable warning messages 4996 - (scanf deprecated; use scanf_s) */
    #pragma warning( disable : 4996 )
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <string.h>
    
    #define COMPANY "Sierra Sporting Goods" /* define company for potential multiple uses */
    #define TRUE 1
    
    
    int menu(void);
    void add(void);
    int getint(int min, int max, char prompt[]);
    double getreal(double min, double max, char prompt[]);
    double total(int q, double c);
    void show(int prodNum, int prodType, int prodQuant, double prodCost, double prodPrice, char desc[]);
    void title(void);
    void message(char msg[]);
    int quit(void);
    void init_costs(double *c, int t);
    void show_costs(double *c, int t);
    void getstring(char *desc);
    void strcase(char *desc);
    char *department[] = {"Camping", "Tennis", "Golf", "Snow Sports", "Water Sports"};
    
    main() /* main function  */
    {
    	int selection, active = TRUE;
    do
    	{
    	selection = menu();
    		switch (selection)
    		{
    			case 1: add(); break;
    			case 2:
    			case 3:
    			case 4:
    			case 5: active = quit(); break;
    		}
    	}
    while(active);
    return 0;
    }
    /*========================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);
    }
    /*=======================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, "%d%d", &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);
    }
    /*===================show()======================================================*/
    void show(int prodNum, int prodType, int prodQuant, double prodCost, double prodPrice, char desc[])
    {
    		printf("\nThe product number is -->  %0.4d", prodNum);
    		printf("\nThe product type is ---->  %d", prodType);
    		/* convert description to proper case structure */
    		strcase(desc);
    		printf("\nThe description is ----->  %s", desc);
    		printf("\nThe quantity is -------->  %d", prodQuant);
    		printf("\nThe cost is ------------>  %.2f", prodCost);
    		printf("\nThe price is ----------->  %.2f\n", prodPrice);
    		/* display totals	*/
    		printf("\n\nTotal product price ----> %.2f", total(prodQuant, prodPrice));
    		printf("\nTotal product cost -----> %.2f", total(prodQuant, prodCost));
    		printf("\nTotal product profit ---> %.2f\n\n", (total(prodQuant, prodPrice) - total(prodQuant, prodCost)));
    }
    /*=================total()========================================================*/
    double total(int q, double c)
    {
    	return(q*c);
    }
    /*=====================message()====================================================*/
    void message(char msg[])
    {
    	printf("%s\n", msg);
    }
    /*====================title()=====================================================*/
    void title()
    {
    	/* clear screen */
    	system("cls");
    
    	/* display title */
    	printf("%s\n\n", COMPANY);
    }
    /*====================quit()=====================================================*/
    int quit()
    {
    	system("cls");
    	message("\n\tGoodbye!\n");
    	return 0;
    }
    /*=====================init_costs()==============================================*/
    void init_costs(double *c, int t)
    {
    	int i;
    
    	for (i = 1; i < t; i++)
    	{
    		*(c+i) = 0;
    	}
    }
    /*====================show_costs()===============================================*/
    void show_costs(double *c, int t)
    {
    	int i;
    	double total = 0;
    
    
    	printf("\nProduct Cost by Type\n\n");
    	printf("Type       Dept               Cost\n");
    	printf("----------------------------------\n");
    	for (i = 1; i < t ; i++)
    	{
    		printf("%2d         %-13s %9.2lf\n", i, *(department+i-1), *(c+i));
    		total += *(c+i);
    	}
    	printf("\nTotal%29.2lf\n\n", total);
    }
    /*====================getstring()===========================================*/
    void getstring(char *desc)
    {
    	printf("Enter the description: ");
    	gets(desc);
    }
    /*====================strcase()=============================================*/
    void strcase(char *desc)
    {
    	int i = 1;
    	/* convert first ch to upper */
    	*(desc) = toupper(*desc);
    	while (*(desc+i) != (int)NULL)
    	{
    		*(desc+i) = tolower(*(desc+i)); /* convert to lower */
    		if (*(desc+i) == ' ') /* convert ch after space to upper */
    		{
    			i++;
    			*(desc+i) = toupper(*(desc+i));
    		}
    		i++;
    	}
    }
    /*=========================================================================*/

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    2,917
    When using scanf() format specifiers must match the actual type of input. If your input is an int the specifier should be "%d". If it is a double then the specifier should be "%lf". If you specify an integer ("%d") and the data is a floating point type then scanf will stop inputting at the decimal point.

    Please see this link scanf().

    Jim

  11. #11
    Registered User
    Join Date
    Oct 2010
    Posts
    29
    I did refer to the link the first time you suggested it There are two data types being called for. So, was I given the wrong advice? I don't see the solution.

    The data in the text file is both int and doubles. How am I to differentiate between the two using fscanf()?

    I'm just getting more confused. Instead of helping me find errors in my code and thus solutions, I feel like I am being directed to something that isn't quite relevant.

  12. #12
    Registered User
    Join Date
    May 2010
    Posts
    2,917
    If your file looks like:
    Code:
    1 9999
    1 5
    1 50
    5.00 900.00
    6.00 1000.00
    You should probably assume the data is type double:

    Code:
    double var1 = 0.0;
    double var2 = 0.0;
    fcanf("%lf%lf", &var1, &var2);
    You can later convert the double to an int.

    Jim.

  13. #13
    Registered User
    Join Date
    Oct 2010
    Posts
    29
    If I change the line to:

    Code:
    while (fscanf(fp, "%lf%lf", &min, &max) != EOF)
    not only does it still not cycle through all inputs, but my output is:

    Code:
    Enter the Product Number: 0 to 0:
    What am I missing? Or, what are YOU missing?

    I don't mean to come off wrong because I AM asking for help, but if your answer is the correct one, shouldn't my results reflect this?

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,265
    I would tweak your use of jimblumberg's suggestion to:
    Code:
    while (fscanf(fp, "%lf %lf", &min, &max) == 2)
    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

  15. #15
    Registered User
    Join Date
    May 2010
    Posts
    2,917
    Please repost your input file.

    Is this the section of code you are having problems with??

    Code:
     
    do
    {
    	title();
     
    	/* display data entry items and take input */
     
    	while (fscanf(fp, "%d%d", &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');
    Notice how this while only calls one line of text.

    Is that what you want?


    Code:
    	while (fscanf(fp, "%d%d", &min, &max) != EOF)
     		prodNum = getint(min,max,"Product Number");

    Also lets look at the getint function:

    Code:
    /*=====================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);
    }
    Look at your scanf().

    Is that correct????


    Jim

Page 1 of 3 123 LastLast
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, 01:52 AM
  2. help me with files
    By xclarkiex5x in forum C++ Programming
    Replies: 27
    Last Post: 11-05-2007, 05:07 PM

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