Thread: Purchase item file processing

  1. #1
    Registered User
    Join Date
    Jun 2016
    Posts
    2

    Purchase item file processing

    i have a problem with inputting data files and outputting them in the codes..
    Here's the question: The format of the text file is:
    (item code);(item name);(item price);(quantity of item in inventory)
    Purchase item file processing-image-png


    The system allows the user to enter the item codes and quantity per item and then show the item’s price and the subtotal on the screen. Prompt an error message “Invalid item.” if the item code cannot be found in both text files. User will enter a sentinel value “-1” to indicate there is no item to be purchased. Lastly, prompt the receipt for customer on the screen.
    I do not know how to use the file scanner in c language.
    This is my code and i literally can only do for the first line.. which is code AG001

    Code:
    #include <stdio.h>
    Code:
    #include <string.h>
    
    
    int main()
    {
    	char itemCode[6];
    	char purchaseCode[6];
    	char itemName[30];
    	double price = 0, tprice = 0 ;
    	int stock, quantity, check, x, c, y;
    
    
    	FILE *fp;
    	FILE *fp2;
    
    
    	fp2 = fopen("ngst.txt", "r");
    
    
    	printf("\nEnter item codes: ");
    	scanf("%s", &itemCode);
    
    
    	printf("\nEnter quantity(-1 to exit): ");
    	scanf("%d", &quantity);
    	while(quantity != -1){
    		fp = fopen("gst.txt", "r");
    		fp2 = fopen("ngst.txt", "r");
    		do{
    			fscanf(fp,"%5s;%[^;];%lf;%d", purchaseCode, itemName, &price, &stock);
    			check = strcmp(itemCode, purchaseCode);
    			if (check == 0){
    				tprice = price * quantity;
    				printf("Item Code: %s\nItem Name: %s\nPrice:RM %.2lf\nTotal:RM %.2lf\n", purchaseCode,itemName, price, tprice);
    				x=0;
    				y=0;
    
    
    			}
    			else{
                x = 0;
                y =1;}
    
    
    		fclose(fp);
    		}while((c=fgetc(fp))!=EOF && x!=0);
    
    
    		do{
    			fscanf(fp2,"%5s;%[^;];%lf;%d", purchaseCode, itemName, &price, &stock);
    			check = strcmp(itemCode, purchaseCode);
    			if (check == 0){
    				tprice = price * quantity;
    				printf("Item Code: %s\nItem Name: %s\nPrice:RM %.2lf\nTotal:RM %.2lf\n", purchaseCode,itemName, price, tprice);
    				x=0;
    				y=0;
    			}
    			else{
                    if (y==1){printf("Invalid item.");}
    				x=0;}
    		fclose(fp2);
    		}while((c=fgetc(fp2))!=EOF && x!=0);
            y=0;
    	printf("\nEnter item codes: ");
    	scanf("%s", &itemCode);
    	printf("\nEnter quantity(-1 to exit): ");
    	scanf("%d", &quantity);
    	x = 1;
    	}
    	system("pause");
    }


  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > fscanf(fp,"%5s;%[^;];%lf;%d", purchaseCode, itemName, &price, &stock);
    The first %s will gobble the whole line, or at least until the first space.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2016
    Posts
    2
    What should i do to fix that? i really have no idea

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You don't know?

    Very strange - is this your code?

    I ask, because you seem to have figured out that you needed "%[^;]" for the 2nd field all by yourself. So saying "you have no idea" just seems wrong somehow.


    You also open ngst.txt twice.

    Closing the file INSIDE the loop is a bad idea.
    Better indentation would go a long way to helping you understand the program flow.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem on - Selection on which item to purchase
    By karipap in forum C Programming
    Replies: 5
    Last Post: 04-07-2009, 06:19 AM
  2. reading the nth item from a txt file
    By Opel_Corsa in forum C++ Programming
    Replies: 1
    Last Post: 11-18-2006, 05:58 PM
  3. How to know if a listed file item is a dir
    By Mariano L Gappa in forum C++ Programming
    Replies: 6
    Last Post: 12-02-2005, 12:09 AM
  4. going about opening a .chm file from a menu item
    By DarkViper in forum Windows Programming
    Replies: 4
    Last Post: 02-20-2004, 04:16 PM
  5. searching for a specific item in a C++ file
    By stanleyw in forum C++ Programming
    Replies: 1
    Last Post: 06-03-2002, 04:54 PM

Tags for this Thread