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:
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.