0

I am getting the following error when I compile my helpers file

jharvard@appliance (~/Dropbox/pset3/find): make helpers
clang -ggdb3 -O0 -std=c99 -Wall -Werror    helpers.c  -lcs50 -lm -o helpers
helpers.c:33:13: error: expected expression
        else
        ^
1 error generated.
make: *** [helpers] Error 1
jharvard@appliance (~/Dropbox/pset3/find): 

My helpers file is reproduced below.

/**
 * helpers.c
 *
 * Computer Science 50
 * Problem Set 3
 *
 * Helper functions for Problem Set 3.
 */

#include <cs50.h>

#include "helpers.h"

/**
 * Returns true if value is in array of n values, else false.
 */
bool search(int value, int values[], int n)

{   int i=0;

while (i<n )  

   {
    if (values[i] == value)
        {
        return true;
        }
      else
        {
        return false;
        }
        i++;
        else
        return value[i];

    }





// TODO: implement a searching algorithm



/**
 * Sorts array of n values.
 */
void sort(int values[], int n);

// TODO: implement an O(n^2) sorting algorithm
for (int i = 1; i < n-1; i++)
{
int min = i;

for (int j = i+1; j < n; j++)
{
   if (values[j]<values[min])
   {
        min = j;



   }
 values [min] = values [i];
};

}

return 0;

}

I can get rid off this expected expression error if I remove the code else return value[i]; Then I get another errorundefined reference to `main'

clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [helpers] Error 1. 

There are several other lines associated with this error. But I am unable to keep their format. Hence I have deleted them

While I could understand the foo() example provided by Kareem in connection with may reach end of non-void function” error, I am not having any clue how to get rid off that error. It appears to persist with for or while loops. Any guidance to get rid of this error will be appreciated. It appears a few others had similar errors while reviewing Stack Exchange. uma1966

1
  • Hi. Please take some time to explore the help tab at the top of this page. It contains instructions on how to post code so that it comes out formatted and readable. Once you review that, would you please edit your question and reformat so that it is more readable? It will make it a lot easier for everyone to help you now and in the future. "Teach a man to fish and feed him for a lifetime." – Cliff B Aug 17 '15 at 17:42
1

From what I can see, you have at least one mismatched curly brace that isn't paired up with another. Make sure that all of your curly braces are properly paired. If you highlight a curly brace or a parenthesis in gedit, it will tell you where the matching one is, or if it isn't paired.

Also, I'm wondering about the undefined reference to main. What command are you using to compile your program? If you are using make helpers, that will cause all kinds of problems, most of which you had in your first version of the question. helpers.c and helpers.h are part of the build for find. As stated in the pset instructions, the command you should be using is

make find

If this answers your question, please click the check mark to accept this and remove the question from the unanswered pool. Let's keep up on forum maintenance. ;-)

4
  • jharvard@appliance (~/Dropbox/pset3/find): make find clang -ggdb3 -O0 -std=c99 -Wall -Werror -o find find.c helpers.c -lcs50 -lm helpers.c:36:12: error: expected expression else ^ helpers.c:56:5: error: expected identifier or '(' for (int i = 1; i < n-1; i++) ^ helpers.c:74:1: error: expected identifier or '(' return 0; ^ 3 errors generated. make: *** [find] Error 1 jharvard@appliance (~/Dropbox/pset3/find): I get the above errors after incorporating Air's suggestions in the stack uma1966 – uma1966 Aug 17 '15 at 22:36
  • @Cliffb-details of Air's suggestions-declare int r if (condition-1) r = 1 else if (condition-2) r = -1 else r = 0 return r For those interested in a more detailed examination of the SESE concept, you can find a huge amount of discussion online and in programming texts; I like this question on programmers.SE, which contains a variety of stylistic and historical perspectives in its answers. shareeditflag edited Dec 29 '14 at 17:38 answered Aug 25 '14 at 16:36 Air 1,417521 – uma1966 Aug 17 '15 at 22:43
  • It still looks like mismatched braces. Did you check and verify that they are all correctly paired? – Cliff B Aug 17 '15 at 22:43
  • @CliffB- I checked the curly braces individually. Also usually if the braces are not matched when we make find,it shows up in the errors list also..uma1966 – uma1966 Aug 17 '15 at 22:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .