Expected expression error in if statement of function

SOLVED: I had a function defined inside of main...of course I only discover this AFTER I type up my post to the forum!


I am having some issues with a very basic program. The following function is a very incomplete version of a function that will eventually sort an array of integers. However, I found some errors before even getting into the actual sorting so I thought that it would be best to resolve the issues that I am having first.

The first error I am having occur at the line:

 
        if (arr[0] > arr[1])


where I am being told that there is an expected expression. I have tried putting parentheses around both values and moves the curly braces around but I have had no success. This is the error that concerns me more.

The second error occurs at the last curly brace of the function below. It says that it expects a semi-colon. I am very new to C++ (some C experience) and I did not think that a semi-colon was required here. Is it just my compiler being picky or do I need a semi-colon here? This is more of a question than an actual error.

1
2
3
4
5
6
7
8
9
    int sort(int arr[], int length)
    {
        if (arr[0] > arr[1])
        {
            arr_temp = arr[0];
            arr[0] = arr[1];
            arr[1] = arr_temp;
        }
    }


For reference, here is the rest of my code. I don't think any of the errors originate from here, but you never know. I am well aware that it is not well implemented right now and as an outsider, this code probably makes very little sense. I am just trying to get what I have now to build properly.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53

#include <stdio.h>
#include <string>
#include <iostream>

using namespace std;
#define LEN 8

int main(int argc, const char * argv[])
{

    // insert code here...
    FILE * file;
    file = fopen("I am not posting the directory of the file on a forum, but rest assured that this part of my code works :)", "r");
    if (file == NULL)
    {
        printf("File not found!\n");
    }
    int arr[LEN];
    int temp = 0 ;
    int arr_temp = 1;
    for (int x = 0; (x < 8) && !feof(file); x++)
    {
        fscanf(file, "%d", &temp);
        arr[x] = temp;
    }
    fclose(file);
    for (int x = 0; x < 8; x++)
    {
        std::cout << arr[x] << "\n";
    }
    
    int sort(int arr[], int length)
    {
        if (arr_temp > temp)
        {
            arr_temp = arr[0];
            arr[0] = arr[1];
            arr[1] = arr_temp;
        }
    }
    
    
    sort(arr, 2);
    
    for(int x = 0; x < 8; x++)
    {
        std::cout << arr[x] << "\n";
    }
    
    
    return 0;
}


Please correct me if any of the language I used was incorrect. I am not a CS student and I do not have a CS background (which is why I am here in the first place). I am using Xcode 4.6.3 if it makes any difference.

EDITS: Add further info and the IDE I am using.
Last edited on
closed account (Dy7SLyTq)
sorry.. i know you solved it but two things:
1) thank you for not deleting your post after solving it so that others may use it
2)i have to laugh that you wrote all of that and then figured it out
1) Yeah, I figured it was best practices to leave it just incase someone was responding to it as I deleted it. I'll leave it if that's what's generally done around here.

2) I felt the same way, but it wasn't too bad. A lot of copy-paste really.
Topic archived. No new replies allowed.