Thread: Someone willing to help a beginner figure out why my insertion sort isn't working!

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    4

    Someone willing to help a beginner figure out why my insertion sort isn't working!

    Hi! This board has helped me tremendously on previous projects. However, I am kind of at a loss as to why I keep getting the compile error when my insertion sort isn't commented out:
    error: subscripted value is neither array nor pointer nor vector.

    I am using gcc compiler and am running this on Linux. I am not very familiar with programming, so please don't be to harsh on me, although any help is GREATLY appreciated!


    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #define MAX = 30;
    
        
        struct employees{
            char surname[30];
            char firstName[30];
            char deptName[30];
            char payRate[30];
            char eyeColor[30];        
        };
    
    
        
    int main(){
        char surname[30];
        char firstName[30];
        char deptName[30];
        char payRate[30];
        char eyeColor[30];
        int i = 0;
        
        FILE *inputFile;
        FILE *outputFile;
        
        char inputFileName[20];
        char outputFileName[20]; 
        
        int numRecs;
    
        printf("Enter the name of the file you wish to read employee data from: \n");
        scanf(" %s" ,inputFileName);
        inputFile = fopen(inputFileName, "r");
        
        
        printf("What is the name of the output file you wish to output your report to? \n");
        scanf(" %s" ,outputFileName);
        outputFile = fopen(outputFileName, "w");
        
        struct employees structure;
        struct employees *empData;
        
        //employees data[30];
        
        fprintf(outputFile, "%-15s", "Surname");
        fprintf(outputFile, "%-15s", "First Name");
        fprintf(outputFile, "%-19s", "Dept");
        fprintf(outputFile, "%-15s", "Pay Rate");
        fprintf(outputFile, "%-15s", "Eye Color");
        
        while((fscanf(inputFile, "%s", surname)) != EOF){
            fscanf(inputFile, "%s %s %s %s", firstName, deptName, payRate, eyeColor);    
            
            strcpy(structure.surname, surname);  //all of these are getting stored in the struct.
            strcpy(structure.firstName, firstName);
            strcpy(structure.deptName, deptName);
            strcpy(structure.payRate, payRate);
            strcpy(structure.eyeColor, eyeColor);
        
        empData = &structure;
        
    //insertion sort**************************************************************************************************************************
    /*int i;
    int x;
    int n;
     
        for (i=0; i < n; i++)
        {
            int j, v = structure[i].deptName;
            x = structure[i];
              
            for (j = i - 1; j >= 0; j--)
            {
                if (structure[j].deptName >= v) break;
                structure[j + 1] = structure[j];
                 
            }
           
            structure[j + 1].a = v;
            structure[j + 1] = x;       
        }*/
    
        
        
        fprintf(outputFile, "\n%-15s %-15s %-19s %-15s %-15s", empData -> surname, empData -> firstName, empData -> deptName, empData -> payRate, empData -> eyeColor);
        }
        
        numRecs++;    
        
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This is most likely wrong:
    Code:
    #define MAX = 30;
    The C preprocessor directives differ from "normal" C. You probably intended:
    Code:
    #define MAX 30
    I notice this in main:
    Code:
    char surname[30];
    char firstName[30];
    char deptName[30];
    char payRate[30];
    char eyeColor[30];
    You might as well just make use of:
    Code:
    struct employees structure;
    Also, struct employees appears to model a single employee, so it should probably be struct employee instead. Then, "structure" is not a very good name for that variable. I suspect that you wanted to write:
    Code:
    struct employee employees[MAX];
    Then your loop would read each employee data set into each struct employee object. Likewise, you would then sort the array named employees.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    4
    I am still getting the same error of:
    error: subscripted value is neither array nor pointer nor vector on lines 71, 72, 76, 77, 81 & 82

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #define MAX 30;
    
        
        struct employee{
            char surname[30];
            char firstName[30];
            char deptName[30];
            char payRate[30];
            char eyeColor[30];        
        };
    
    
        
    int main(){
        char surname[30];
        char firstName[30];
        char deptName[30];
        char payRate[30];
        char eyeColor[30];
        int i = 0;
        
        FILE *inputFile;
        FILE *outputFile;
        
        char inputFileName[20];
        char outputFileName[20]; 
        
        int numRecs;
    
        printf("Enter the name of the file you wish to read employee data from: \n");
        scanf(" %s" ,inputFileName);
        inputFile = fopen(inputFileName, "r");
        
        
        printf("What is the name of the output file you wish to output your report to? \n");
        scanf(" %s" ,outputFileName);
        outputFile = fopen(outputFileName, "w");
        
        struct employee employees;
        struct employee *empData;
        
        //employees data[30];
        
        fprintf(outputFile, "%-15s", "Surname");
        fprintf(outputFile, "%-15s", "First Name");
        fprintf(outputFile, "%-19s", "Dept");
        fprintf(outputFile, "%-15s", "Pay Rate");
        fprintf(outputFile, "%-15s", "Eye Color");
        
        while((fscanf(inputFile, "%s", surname)) != EOF){
            fscanf(inputFile, "%s %s %s %s", firstName, deptName, payRate, eyeColor);    
            
            strcpy(employees.surname, surname);  //all of these are getting stored in the struct.
            strcpy(employees.firstName, firstName);
            strcpy(employees.deptName, deptName);
            strcpy(employees.payRate, payRate);
            strcpy(employees.eyeColor, eyeColor);
        
        empData = &employees;
        
    //insertion  sort**************************************************************************************************************************
    int i;
    int x;
    int n;
     
        for (i=0; i < n; i++)
        {
            int j, v = employees[i].deptName;
            x = employees[i];
              
            for (j = i - 1; j >= 0; j--)
            {
                if (employees[j].deptName >= v) break;
                employees[j + 1] = employees[j];
                 
            }
           
            employees[j + 1].deptName = v;
            employees[j + 1] = x;       
        }
    
        
        
        fprintf(outputFile, "\n%-15s %-15s %-19s %-15s %-15s", empData ->  surname, empData -> firstName, empData -> deptName, empData ->  payRate, empData -> eyeColor);
        }
        
        numRecs++;    
        
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This is not an array declaration:
    Code:
    struct employee employees;
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by laserlight View Post
    This is not an array declaration:
    Code:
    struct employee employees;
    To fix that, I'll introduce him to typedef!
    Code:
    employee employees;

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Apr 2012
    Posts
    4

    confused

    I guess what is confusing me the most is that code compiles fine with everything as is until I add in the loop, then the compiler complains about the
    [code]
    struct employee employees;
    [\code]

    line. Any clarification of why this is happening would be very helpful, and of course, thank you guys for all your help!

  7. #7
    Registered User
    Join Date
    Apr 2012
    Posts
    4
    and not that it really matters, but I am her, not a him!

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > int j, v = employees[i].deptName;
    Since deptName is a char array, perhaps you should look again at what operations are permitted on arrays, and perhaps say strcmp and strcpy.
    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.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by gibs7121
    I guess what is confusing me the most is that code compiles fine with everything as is until I add in the loop, then the compiler complains
    Notice that I suggested:
    Code:
    struct employee employees[MAX];
    Of course, as Salem notes, that is not the end of the story.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Delete lines 18-22. Those declarations are only confusing you. You actually want to read the data straight into an array, not into a bunch of unrelated random variables.

    Next you need to actually have an array. Right now, employees is not an array. An array would have some place for specifying it's size within its declaration. Perhaps somewhat like line 8, where you've specified a length of 30.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Insertion sort
    By Fatima Rizwan in forum C++ Programming
    Replies: 5
    Last Post: 02-26-2010, 02:24 PM
  2. Replies: 1
    Last Post: 01-26-2010, 09:02 AM
  3. Insertion Sort Help
    By Odinwar in forum C++ Programming
    Replies: 8
    Last Post: 12-09-2009, 11:27 AM
  4. Insertion sort... Please help!
    By xMEGANx in forum C++ Programming
    Replies: 11
    Last Post: 11-30-2007, 03:30 AM
  5. Insertion sort
    By Hypercase in forum C Programming
    Replies: 9
    Last Post: 08-31-2004, 04:02 AM

Tags for this Thread