Need a lot of help!

This is a discussion on Need a lot of help! within the C Programming forums, part of the General Programming Boards category; I need to write a program that reads data from one file ( parts.txt) that contains information on parts sold. ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    4

    Need a lot of help!

    I need to write a program that reads data from one file ( parts.txt) that contains information on parts sold. Each line in the file contains information on one part as follows -
    part number(integer), selling price(double)and the quantity in stock(integer) The data is stored into an array. I need to read from another file (orderinfo.txt) and search for a particular part number. The 2nd file contains information for order requests. Each line in the file contains - an order num (int), part num and quantity requested (int)

    If the part number is there, the order was successful.

    All I have so far is shown below. I'm a newbie, can anyone recommend any good reading materials to help me?

    #include <stdlib.h>
    #include <stdio.h>



    int main () {

    FILE * in=fopen ("partData.txt", "r");
    FILE * out=fopen("report.txt", "w");

    int partNum[100];
    double price [100];
    int quantityS [100];

    for (int j=0;j < 100; j++)
    fscanf(in, "%d", &partNum[j]);

    for (int x=0; x < 100;x++)
    fscanf (in, "%d", &price[x]);

    for (int z=0; z < 100; z++)
    fscanf(in, "%d", quantityS[z]);

    fclose(in);

    in = fopen ("orders.txt","r");
    int a,b,c;

    fscanf (in, "%s", &a);
    fscanf (in, "%s", &b);
    fscanf (in, "%s", &c);


    fclose(in);
    }

    int partNum[100];
    int x,y;

    void selectionSort (int PartNum[], int size) {
    for (x!=0; x <=size; x=x+1){
    int smallest = x;
    for (int y = x+1; y < size, y=y+1;
    if partNum [y] < partNum [smallest]
    smallest=y;

    int temp = partNum[smallest];
    partNum[smallest] = partNum[x];
    partNum[x] = temp;

    }

    int partNum[100];

    int BinarySearch ( int partNum []), int size, int b) {
    int lo = 0, hi=size-1;
    while (lo <= hi) {
    int mid (lo+hi)/2
    if (partNum[mid] == b);
    return mid;

    if (partNum[mid] < b)
    lo = mid + 1;

    else
    hi = mid - 1

    }
    return -1
    }
    }

  2. #2
    Registered User hellork's Avatar
    Join Date
    Nov 2010
    Posts
    39
    Please check out this really cool link

    << !! Posting Code? Read this First !! >>

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    Quote Originally Posted by Kandy757 View Post
    I'm a newbie, can anyone recommend any good reading materials to help me?
    I doubt you will find any books on writing ordering or point-of-sale systems that aren't designed for enterprise level applications, but some recommendations for general books on C can be found here: C Book Recommendations.

    Also, after reading hellork's suggested link and fixing your post, please tell us what is wrong with your program and what you don't get. Be as clear and as specific as possible.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    4
    I'm terribly sorry, I'm new to all of this.

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    
    
    int main () {
        
        FILE * in=fopen ("partData.txt", "r");
        FILE * out=fopen("report.txt", "w");
    
        int partNum[100];
        double price [100];
        int quantityS [100];
        
        for (int j=0;j < 100; j++)
        fscanf(in, "%d", &partNum[j]);
        
        for (int x=0; x < 100;x++)
        fscanf (in, "%d", &price[x]);
        
        for (int z=0; z < 100; z++)
        fscanf(in, "%d", quantityS[z]);
    
        fclose(in);
    
       in = fopen ("orders.txt","r");
       int a,b,c; 
    
       fscanf (in, "%s", &a);
       fscanf (in, "%s", &b);
       fscanf (in, "%s", &c);
       
       
       fclose(in);
    }
    
    int partNum[100];
    int x,y;
    
      void selectionSort (int PartNum[], int size) {
      for (x!=0; x <=size; x=x+1){
      int smallest = x;
      for (int y = x+1; y < size, y=y+1;);
      if partNum [y] < partNum [smallest]
      smallest=y;
                
      int temp = partNum[smallest];
      partNum[smallest] = partNum[x];
      partNum[x] = temp;
                
      }
      
      int partNum[100];
              
      int BinarySearch ( int partNum []), int size, int b) {
      int lo = 0, hi=size-1;
      while (lo <= hi) {
     int mid (lo+hi)/2
     if (partNum[mid] == b);
     return mid;
                 
     if (partNum[mid] < b)
     lo = mid + 1;
                 
        else
        hi = mid - 1
                 
                 }
                 return -1
                 }
    }

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    4
    I keep getting "expected unqualified-id before int" and "expected , or ; before 'int'"

    I'm at a complete lost.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    Basically, your code is rife with syntax errors.

    As for your two errors you mentioned, line 44 needs () around the conditional and you need to remove a closing ) from the middle of your parameter list in BinarySearch.

    Other errors I noticed:
    1. Line 43 seems to have an unnecessary ; at the end
    2. Several other lines are missing a ;
    3. You're missing an = in the following line: int mid (lo+hi)/2
    4. You've got some brackets out of whack, and consequently some strange variable declarations causing "unknown variable" stuff and all kinds of other problems. You may be missing a closing } for your selectionSort function and have an extra one at the bottom of the file (unless you meant for the BinarySearch function to only be callable from within selectionSort, which I'm doubt you do).

    Clear, well formatted code with ample spacing around everything, and an editor that can do brace/bracket/parentheses matching, auto-indent and syntax highlighting are really helpful. Try sticking to a coding standard (Indent style - Wikipedia, the free encyclopedia). I prefer K&R, but Allman and BSD KNF are popular and easy to read. As for editors, I like vi, but almost any IDE, as well as some advanced text editors have the features I mention.

    Also, I recommend beginners to turn on all compiler warnings to help you catch other errors that may be grammatically correct, but produce bizarre results. Take a look at what my compiler produced:

    main.c: In function ‘main’:
    main.c:19: warning: format ‘%d’ expects type ‘int *’, but argument 3 has type ‘double *’
    main.c:22: warning: format ‘%d’ expects type ‘int *’, but argument 3 has type ‘int’
    main.c:29: warning: format ‘%s’ expects type ‘char *’, but argument 3 has type ‘int *’
    main.c:30: warning: format ‘%s’ expects type ‘char *’, but argument 3 has type ‘int *’
    main.c:31: warning: format ‘%s’ expects type ‘char *’, but argument 3 has type ‘int *’
    main.c:9: warning: unused variable ‘out’
    main.c: In function ‘selectionSort’:
    main.c:41: warning: statement with no effect
    main.c:43: warning: left-hand operand of comma expression has no effect
    main.c:44: error: expected ‘(’ before ‘partNum’
    main.c:44: warning: statement with no effect
    main.c:55: error: expected identifier or ‘(’ before ‘int’
    main.c:53: warning: unused variable ‘partNum’

    You need to read up on the scanf documentation to understand the different format specifiers and which go with what data types.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Posts
    4
    thank you so much !

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting a file with a lot of words.
    By samus250 in forum C Programming
    Replies: 28
    Last Post: 04-27-2008, 02:36 PM
  2. C(xx), Brag and Flame a lot!
    By xuftugulus in forum A Brief History of Cprogramming.com
    Replies: 62
    Last Post: 03-21-2008, 12:15 AM
  3. A lot can happen in 50 years
    By Salem in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 10-04-2007, 05:44 AM
  4. using fopen getting a lot of warnings
    By netiad in forum C Programming
    Replies: 1
    Last Post: 04-28-2007, 09:44 PM
  5. Still get error even if i changed a lot..
    By icefire99 in forum C++ Programming
    Replies: 9
    Last Post: 04-26-2007, 04:31 AM

Tags for this Thread


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