Search code examples
csyntaxfunction-definition

Unsure why "function definition is not allowed here"


So this code is obviously incomplete and messed up but when I try to format my code I get an error here which I wasn't expecting (on the bracket after int getNumberFromSingleLine(char * singleLine)

 int getNumberFromSingleLine(char * singleLine)
        {
            char number[10];
            int i = 0;
            while (isDigit(singleLine[i]))
            {
                number[i] = singleLine[i];
                i++
            }
            number[i] = '\0'; // null term character

            return atoi(number);
        }

My full code is

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
//first, read text opening the file
//then, sort the numbers in order
//then find out how high/how many rows there will be given the highest number
//then, chronologically isolate the numbers before /n
//then print the words that correspond to the numbers

void merge(int arr[], int l, int m, int r);
void mergeSort(int arr[], int l, int r);

int main(int argc, char *argv[])
{
    // Check for command line args
    if (argc != 2)
    {
        printf("Usage: ./pyramidcode filename.txt\n");
        return 1;
    }
    typedef struct
    {
        int buffer;
        char phrase;
    }
    nameandnum;
    //give array a size
    int b = 1;
    nameandnum nameandnumber[b];



    FILE * fPointer = fopen(argv[1], "r");



    char singleLine[150];
    //read through lines until end
    while(!feof(fPointer))
    {
        //extract text from line
        fgets(singleLine, 150, fPointer);
        //get the number from the text
        int getNumberFromSingleLine(char * singleLine)
        {
            char number[10];
            int i = 0;
            while (isDigit(singleLine[i]))
            {
                number[i] = singleLine[i];
                i++
            }
            number[i] = '\0'; // null term character

            return atoi(number);
        }
        char getPhraseFromSingleLine(char * singleLine)
        {
            char string[50];
            while (singleLine[i] != '\n')
            {

                if(!isDigit(singleLine[i]))
                {
                    i++

                }
                else
                {
                    singleLine[i] = string[i]
                    i++
                }
            }
        //puts number into buffer array
        number = nameandnumber[b].buffer;
        //puts string into phrase array
        string = nameandnumber[b].phrase;
        //expands size of all arrays
        b++;
        }


    }



//merge sorts the buffer so that it's in chronological order
merge_sort(nameandnumber[z].buffer)

//Pyramidsortation
    int z = 0;
    while (z < b);
    {
        int x = 1 + z;
        z + x = z;
        printf("%s, string[z])
    }





    fclose(fPointer);
    return 0;

}


void merge(int arr[], int l, int m, int r)
{
    int i, j, k;
    int n1 = m - l + 1;
    int n2 = r - m;

    // Create temp arrays
    int L[n1], R[n2];

    // Copy data to temp arrays
    // L[] and R[]
    for (i = 0; i < n1; i++)
        L[i] = arr[l + i];
    for (j = 0; j < n2; j++)
        R[j] = arr[m + 1 + j];

    // Merge the temp arrays back
    // into arr[l..r]
    // Initial index of first subarray
    i = 0;

    // Initial index of second subarray
    j = 0;

    // Initial index of merged subarray
    k = l;
    while (i < n1 && j < n2) {
        if (L[i] <= R[j]) {
            arr[k] = L[i];
            i++;
        }
        else {
            arr[k] = R[j];
            j++;
        }
        k++;
    }

    // Copy the remaining elements
    // of L[], if there are any
    while (i < n1) {
        arr[k] = L[i];
        i++;
        k++;
    }

    // Copy the remaining elements of
    // R[], if there are any
    while (j < n2) {
        arr[k] = R[j];
        j++;
        k++;
    }
}

// l is for left index and r is
// right index of the sub-array
// of arr to be sorted
void mergeSort(int arr[], int l, int r)
{
    if (l < r) {
        // Same as (l+r)/2, but avoids
        // overflow for large l and h
        int m = l + (r - l) / 2;

        // Sort first and second halves
        mergeSort(arr, l, m);
        mergeSort(arr, m + 1, r);

        merge(arr, l, m, r);
    }
}

Okay, so obviously I know there's lots of problems other than this but I'm specifically looking for why I am getting that error on that line.

The point of this code is to open a text file with numbers on the left side of each line followed by a phrase and sort the numbers in ascending order through merge sort. Then I want to only print the phrases that correspond with the bold numbers in this order: 1, 2 3, 4 5 6, 7 8 9 10, ...etc I know that my code is not complete in terms of "linking" the two elements of my struct in the merge sort and has problems with the giving the array its size. If you feel like giving advice on that, I'd be extremely appreciative but I'm not expecting it. I'd just like to understand why I'm getting the function definition error.

Sorry if this is a basic question, I looked through the forum already and tried to make sure I properly tabbed my brackets and compared it with other issues people have had but I couldn't find a solution.

Thanks and apologies in advance- I'm still very much a beginner!


Solution

  • There are multiple problems in the code:

    • functions cannot be defined inside the scope of function bodies. Some compilers support this as an extension, such as gcc, but the C Standard does not define the precise semantics of such definitions and most compilers refuse them. Your C formater reports this as an error.

    • there is a missing ; at the end of merge_sort(nameandnumber[z].buffer) causing the remainder of the function to be misaligned.

    • there is an extra ; at the end if while (z < b); causing the next block to be always executed once if z >= b and never reached otherwise.

    • there is a missing closing quote in printf("%s, string[z]) causing the rest of the source code to appear as a string constant.

    • multiple i++ statements appear without a ;