need some help with computer science project...

This is a discussion on need some help with computer science project... within the C Programming forums, part of the General Programming Boards category; I'm trying to write a simply inventory program for my project. I've just started learning C. I'm in a real ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    4

    need some help with computer science project...

    I'm trying to write a simply inventory program for my project. I've just started learning C. I'm in a real jam here as I did my additions for my program one way and every solution I'm finding for modifying records does seem to work with my file. Here is the code for adding items to the inventory file...I need some help with writing a module/function that will access the file and allow me to modify items and one that will allow me to delete entire records from the inventory.....I need it fast cause I'm facing a deadline...here is some of my code
    Code:
    /*
    * ADDS CHEMICALS TO INVENTORY
    */
    
    int add(void){
        fflush(stdin);
        FILE *Inventory;
        char name[40]={'\0'};
        char grade[6]={'\0'}, notes[50]={'\0'},units[3]={'\0'};
        float addchem[7],delchem[7];
    
        sys_time();
    
    /*** For additions and deletions */  
        if ((Inventory=fopen("/Inventory.txt", "a"))==NULL){
           printf("Cannot open the file!\n");
           exit(EXIT_FAILURE);
        }
           while (getchar()!='\n');
           fflush(stdin);
           /*** Prompts for input */
           
           printf("Chemical(39 characters): ");
           fgets(name,sizeof(name),stdin);
           fflush(stdin);
           /*** no need to validate type for a chemical name may be alphanumeric */
           
           printf("Enter Grade: ");
           fgets(grade,sizeof(grade),stdin);
           fflush(stdin);
        //some kinda validation here//
        
           printf("Enter amount added: ");
           scanf("%f",&addchem[7]);
        /*** some kinda validation here */
        
           printf("Enter amount for Deleted: ");
           scanf("%f",&delchem[7]);
        /*some kinda validation here*/
           
           while (getchar()!='\n');
           printf("Enter Units(g,kg,ml,l): ");
           fgets(units,sizeof(units),stdin);
           fflush(stdin);
    
           printf("Enter Notes in no more than 50 characters: ");
           fgets(notes,sizeof(notes),stdin);
           fflush(stdin);
        /*some kinda validation here*/
    
    
        /*** writes values to file */
           fprintf(Inventory, "%s,%s/%f/%f/%s/%s;",name,grade,addchem[7],delchem[7],units,notes);
           fclose(Inventory);
        /*** Displays user entry */
           printf("\n\nYou entered the following information: \n");
           printf("Chemical:    %s\n", name);
           printf("Grade:   %s\n", grade);
           printf("Added:   %.2f%s\n", addchem[7],units);
           printf("Deleted: %.2f%s\n", delchem[7],units);
           printf("Notes:   %s\n\n", notes);
           
           printf("\nYour Inventory Entry was successful!\n");
           printf("Press any 'Enter' to continue...");
           getchar();
           while(getchar()!='\n');
           main();
          
    return 0;      
    }

  2. #2
    ---
    Join Date
    May 2004
    Posts
    1,379
    did you ever notice that fflush(stdin); doesnt do anything?

    check here

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    4
    Someone just brought it to my attention that I did not post my delete module...well here it is...
    Code:
    int dump(void){
    	FILE *temp, *Inventory;
    	char chem[40]={'\0'},name[40]={'\0'},grade[6]={'\0'},units[3]={'\0'},notes[50]={'\0'};
    	float addchem[7],delchem[7];
    	char selection;
    	int found;
    	
    	if((Inventory=fopen("/Inventory.txt","r"))==NULL) {
    		printf("Cannot open file!\n");
    		exit(EXIT_FAILURE);
    	}
    	if((temp=fopen("/temp.$TM","w"))==NULL) {
    		printf("Cannot open file!\n");
    		exit(EXIT_FAILURE);
    	}
    	
    	while(!getchar()=='\n');
    	printf("Enter Chemical to delete: ");
    	fgets(chem,sizeof(chem),stdin);
    	rewind(Inventory);
    	found=0;
    	
    	
    	fscanf(Inventory, "%[^,]%*c%[^/]%*c%f%*c%f%*c%[^/]%*c%[^;]%*c", name,grade,&addchem[7],&delchem[7],units,notes);
    	while (!feof(Inventory)){
    		if(strcmp(chem,name)==0)
    			printf("Chemical:	%s\n", name);
    			printf("Grade:   %s\n", grade);
    			printf("Added:   %.2f%s\n", addchem[7],units);
    			printf("Deleted: %.2f%s\n", delchem[7],units);
    			printf("Notes:   %s\n\n", notes);
    			/*** This bit of code cofirms deletion of item from inventory */
    			printf("\nEnter 'Y' to %s or 'N' to Cancel : ",chem);
    			selection=toupper(getchar());
    			fflush(stdin);
    			switch(selection){
    			
    				case 'Y':
    					if(strcmp(chem,name))
    					fprintf(temp,"%s,%s/%f/%f/%s/%s;",name,grade,addchem[7],delchem[7],units,notes);
    					
    					printf("You have successfully deleted %s",chem);/* test statement. Replace with file writes */
    					fclose(Inventory);
    					fclose(temp);
    					unlink("/Inventory.txt");
    					rename("/temp.$TM","/Inventory.txt");
    					printf("Press any 'Enter' to continue...");
    					getchar();
    					while(getchar()!='\n');
    					main();
    					break;
    				case 'N':
    					printf("Press any 'Enter' to continue...");
    					getchar();
    					while(getchar()!='\n');
    					main();
    					break;
    			}
    		}
    	printf("'%.*s' not found in Inventory!!!",strlen(chem)-1,chem);
    	printf("\nPress any 'Enter' to continue...");
    	getchar();
    	while(getchar()!='\n');
    	main();
    
    
    return 0;
    }
    I want to only delete the entry i searched for however this function is clearing my entire inventory file.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    > float addchem[7],delchem[7];
    Why are these arrays?

    > fscanf(Inventory, "%[^,]%*c%[^/]%*c%f%*c%f%*c%[^/]%*c%[^;]%*c", name,grade,&addchem[7]
    UGH, that is one god-awful use of fscanf
    1. Use fgets to read the line into memory, then use sscanf to pick it apart
    2. Don't try and process the line in one call, especially one as complex as that
    3. scanf functions return a result (the number of conversion-assignments), so check it.
    4. &addchem[7] is out of bounds of the array - you weren't by any chance attempting to read 7 floats at the same time were you (because this isn't it).

    > fflush(stdin);
    Read the FAQ - this is undefined.

    > if(strcmp(chem,name))
    So is this comparing for equal, or not equal.

    > fprintf(temp,"%s,%s/%f/%f/%s/%s;",name,grade,addchem[7],delchem[7],units,notes);
    Yeah, I'm thinking your [7] notation is an attempt to deal with 7 numbers all at the same time.
    Both input and output need a for loop - eg
    for ( i = 0 ; i < 7 ; i++ ) fprintf( temp, "%f,", addchem[i] );


    > unlink("/Inventory.txt");
    Yet another function returning a status which you ignore.
    Now tell me you're not programming as a super-user, because that's the only person who should be able to write to the root directory.
    One programming error on your part, and it's bye-bye file system.
    Try
    unlink("./Inventory.txt");
    for files in the current directory.

    > main();
    How about a simple return; instead if you just want to get back to the calling function.
    main() calls dump() calls main() calls dump() calls ....
    See the problem here?
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    4

    thanks

    Thank you... i will work on these things....

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    4

    return?

    You said instead of calling main() from dump() to use a return. How do I use a return? I'm only been learning C for a few months now..I'm still a newbie...

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    Code:
    main();
    remove that at the end of your program
    When no one helps you out. Call google();

  8. #8
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    He mean:
    Code:
    printf("'%.*s' not found in Inventory!!!",strlen(chem)-1,chem);
        printf("\nPress any 'Enter' to continue...");
        getchar();
        while(getchar()!='\n');
        /*main();*/
    return;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Computer Science Major
    By Dark_Oppressor in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 11-20-2006, 08:18 AM
  2. Computer Science question
    By dxfoo in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 10-18-2005, 02:09 PM
  3. Regarding Undergraduate Computer Majors
    By UnregdRegd in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 10-04-2003, 11:55 AM
  4. computer science majors..read!
    By nextus in forum C++ Programming
    Replies: 12
    Last Post: 02-25-2003, 11:32 PM

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