Thread: need some help!!

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    56

    Question need some help!!

    A parts company has information on its parts in a data file "parts.dat". The data in the data file is structured as follows:



    The data file consists of 2 parts. The first part has information on the parts. Each line of data contains the part number ( an integer), a description of the part, the quantity in stock and the unit price separated by one or more spaces. The description of the part is contained within two '*' ( the asterisks are not part of the description). This section of data is terminated by a line with a single 0.



    The next half of the data file consists of transactions performed on the data. Each line consists of two letters indicating the transaction to be performed and this is followed by data relevant for the transaction. The different kinds of transactions are as follows:



    1. To add a new part:

    ap partNumber *description* quantityInStock unitPrice



    2. To sell a part:

    sp partNumber quantitySold



    3. To change the price of a part:

    cp partNumber newPrice



    4. To add stock of a particular part:

    cq partNumber quantityToAdd



    5. To list all information on all parts:

    ls



    Your program must store the part information using appropriate structures in ascending order by the part id number.

    Your program must cater for cases where there is not enough quantity in stock for sale, or the part number does not exist. In these cases, appropriate error messages must be printed.

    Your program must also print after processing all the transactions the total sales figure.



    An example of the format of the data file is given below.



    2345 *Stanley Bearing* 178 12.56

    3456 *Silver Rims* 4 500.00

    7862 *Castrol GTX 50 Body Engine Oil* 29 20

    0

    ap 9872 *BrakeShoes* 50 2241.00

    ls

    sp 2345 58

    sp 7862 15

    cq 2345 100

    cp 7862 19.59

    ls

    ANY ONE WANT TO HELP ME ??MY CODE HAS ALOT OF ERRORS HOW TO DO IT?

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by nefsan View Post
    ANY ONE WANT TO HELP ME ??MY CODE HAS ALOT OF ERRORS HOW TO DO IT?
    Post your code.

    Forum Guidelines. Read before posting

    Homework
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    56

    wish i knew wat to do

    well i did not get very far ...not far at all.i understand the part about creating a structure and reading from a file.however i just dont understand how to fscanf the data into the structure as required. i've only jst started structurs n stuff n this is really new to me. here is how far i have reached if i can understand how to read the values in properly ,i think i'd be able to understand how to write tthe functions.i would really like to know what to do!!!!

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #define MAX 100
             int main()
    {
               FILE*in=fopen("parts.dat","r");
        
               int i=0;
                typedef struct{
                    int partnum;
                    char partdescription[50];
                    int qty;
                    float price;
                                     }part;
        
        
        part item[MAX];
        
        fscanf("%d %s %d %lf",&item[i].partnum,&item[i].partdescription,&item  i].qty,&item[i].price);
        
        
        while(item!=0){
                       item[i].partnum =
                 
        
        
        
        
                           system("pause");
                           return 0;
    }

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    First of all, what you should learn is to - how to indent your code-. It is very poor. If you don't indent your code, you wont get much help. Any way, just with the quick glance of you code I found few things

    1. Always make sure to check the return value of the fopen function.
    2. Its always a food practice to declare the structure outside the main.
    3. You could sort out your fscanf function
    Code:
    fscanf("&#37;d %s %d %lf", &item[i].partnum, item[i].partdescription, &item[i].qty, &item[i].price );
    You don't have to specify the '&" for strings. since the string name itself holds the address of the first element in the string.
    4. And followed by the code is incomplete
    5. Forgot to close the file (Eg: close(in); )
    6. Don't use system("PAUSE"); instead use getchar();

    Follow procedure which s given to initially start with

    First Step
    1. First open a file (Don't forget to do some error checking here)
    2. Opened opened read each line from the file using fgets
    3. And then display them.

    Second step
    1. Once you got working the first step
    2. You read each line from the file now use sscanf function to read each elements from that line
    3. And store the read element to the structure.
    4. And printf the struct variable, to make sure it has worked.

    Third Step
    1. Once you got second step working
    2. Now your last step should have to sort out the records with are there in the struct.

    ssharish

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    For reading lines from a file, I usually start with a shell like this.
    Code:
    #include <stdio.h>
    
    int main()
    {
       static const char filename[] = "file.txt"; /* "parts.dat" */
       FILE *file = fopen(filename, "r");
       if ( file != NULL )
       {
          char line[BUFSIZ];
          while ( fgets(line, sizeof line, file) != NULL )
          {
             /* ... */
          }
       }
       else
       {
          perror(filename);
       }
       return 0;
    }
    Then you can add stuff and make adjustments. For example, reading elements of a structure -- and as proof that things are working, just print them out.
    Code:
    #include <stdio.h>
    
    #define MAX 100
    
    typedef struct
    {
       int partnum;
       char partdescription[50];
       int qty;
       float price;
    } part;
    
    int main()
    {
       static const char filename[] = "file.txt"; /* "parts.dat" */
       FILE *file = fopen(filename, "r");
       if ( file != NULL )
       {
          char line[80];
          part item[MAX];
          int i = 0;
          while ( fgets(line, sizeof line, file) != NULL )
          {
             if ( sscanf(line, "%d *%79[^*]* %d %f", 
                         &item[i].partnum,
                          item[i].partdescription,
                         &item[i].qty,
                         &item[i].price) == 4 )
             {
                printf("partnum = %d, description = %s, qty = %d, price = %.2f\n",
                       item[i].partnum,
                       item[i].partdescription,
                       item[i].qty,  
                       item[i].price);
                ++i;
             }
          }
       }
       else
       {
          perror(filename);
       }
       return 0;
    }
    Then continue adding and modifying as necessary.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    56

    Smile thanks in progress

    i really didnt realize id get this much help.its a little clearer to me now.i see that you use different calls like the fgets and the perror stuff.my textbook doesnot have anything as such..i dont understand y but the fgets usage is simpler and much easier to rem!!thanks people i'm still tryin to do it here.thanks again for the posts it really helps!!

  7. #7
    Registered User
    Join Date
    Oct 2007
    Posts
    56
    what is the purpose of the sscanf?whats the difference from just scanf? and also what is >static const char filename[] = "file.txt"; <does the static call allow it to be the same for all function calls or so?

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    The fgets reads a line, the sscanf attempts to parse it. It is much like fscanf, but it's easier to control line-by-line handling.

    The use of static is merely a habit of mine, feel free to ignore it in this instance. I wanted filename for later use with perror.

    In other functions in which a variable is const and need not be reinitialized each time when the function is called, the static qualification has more meaning for me. But here it's merely a habit perpetuated.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    Registered User
    Join Date
    Oct 2007
    Posts
    56
    dave_sinkula where did you learn to program in c?can you recommend me a site where i can find good tutorials?my course this semester entails sorting,structures,pointers,linked lists,recursions,stacks and queues and random numbers.I NEED to know these things ,you seem very well versed in this language any advise?

  10. #10
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    http://www.cprogramming.com/tutorial.html with followup questions here in this forum?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  11. #11
    Registered User
    Join Date
    Oct 2007
    Posts
    56
    CAN YOU explain how this piece of code works and also how coome you used &#37;79[^*]* and no %s to get the description?
    Code:
    if ( sscanf(line, "%d *%79[^*]* %d %f",

  12. #12
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    It's considered an extension, and isn't required by the standard.

    You can replace &#37;s with %[], the string will read anything in the brackets, the ^ means it will stop at anything in the brackets. The 79 dictates you only want a max of 79 characters.

    Ie, it will read a string in until 79 characters or a until no '*' is found.

  13. #13
    Registered User
    Join Date
    Oct 2007
    Posts
    56
    how do i write the parameters to the function call for a structure?this is the sorting code i wrote to sort the given data by part number.is it correctly structured?it is not working the compiler says "declaration for paameter "sort" but no such declaration found.
    this is my code
    Code:
                                      
    void sort(part list[],int n){
         part temp;
         int j,k;
         for (j=1;j<n;j++){
             temp=list[j];
             k=j-1;
             while(k>=0 && temp <list[k]){
                        list[k+1]=list[k];
                        k=k-1;
                        }
                        list[k+1]=temp;
                        }
                        }
    i declared void sort(part list[],int n); under main also but it stil does not work.whats is wrong with it?i dont know how to do the other functions if i cant do this one.FRUSTRATION help PLEASE PLEASE PLEASE

  14. #14
    Registered User
    Join Date
    Oct 2007
    Posts
    56
    okay i call the sort function with sort(item[i].partnum,i);
    the program crashes and the compiler says ::[Warning] passing arg 1 of `sort' makes pointer from integer without a cast

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by nefsan View Post
    okay i call the sort function with sort(item[i].partnum,i);
    the program crashes and the compiler says ::[Warning] passing arg 1 of `sort' makes pointer from integer without a cast
    Yes, that's clearly wrong.

    Code:
             while(k>=0 && temp <list[k]){
    This doesn't look quite right either. The array coming in is an array of part structure. temp is a part structure, and you can't really just compare those as "<" and ">". You probably want to compare the partnumber WITHIN the structure, as that would make a lot more sense.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed