views:

225

answers:

5

expected unqualified-id before '{'

Where is this error on my code? Thanks everyone!

#include <iostream>

using std::cout;
using std::endl;

//function prototypes
void findsmallest (int[], int &); 
void findsmallest  (int scores [], int & min);

int main()
{
    //declare variables
    int smallest = 0;
    int scores[20] = { 90, 54, 23,  75, 67,
                       89, 99, 100, 34, 99, 
                       97, 76, 73,  72, 56, 
                       73, 72, 65,  86, 90 };
    findsmallest(scores, smallest);                   
    return 0;

    //call function find smallest
    findsmallest (scores, smallest);                   

    cout << "Minimum value in the array is " << smallest << endl;

    system ("pause");
    return 0;
}

//function definition

void findsmallest(int scores [], int & min);
{

    min = scores[0];
    int i;

    for(i = 0; i < 20; i++)
    {
        if(scores[i] < min)
        {
            min = scores[i];
        }
    }
}


//end display findsmallest
system ("pause");
return 0;
A: 

The following code declares an int array of length 5.

int billy [5] = { 16, 2, 77, 40, 12071 };

Here is a great tutorial for learning arrays in C++.

bmargulies
Thank You will check out the link.
Rosemary
A: 

What are you trying to do? The first line looks like it begins with the comment

//declare array

and then goes on to an array declaration. What more are you trying to accomplish?

seh
A: 

Well, you wanted to find the smallest and print it, right?

For that, you will have to consider each member of the array, and keep track of the smallest you have found. After considering all the array values, you will know which one was the lowest, so you can print it. In pseudocode:

var smallestSoFar = aBigNumber

for i in array loop
    if array[i] < smallestSoFar then
        smallestSoFar = array[i]
    end if
end loop
print smallestSoFar

Hope it helps ;)

machielo
Gives me an idea of what is expected. Thanks!
Rosemary
@Rosemary: in that case you should mark this answer as accepted. Just hit the check mark next to the answer.
Goose Bumper
I'm glad you found it helpful!! Another hint: aBigNumber, in C, should be INT_MAX. INT_MAX is the biggest integer possible, and to use that constant just include <limits.h>. Good luck!!
machielo
A: 

To find the smallest value in an array, you would do something like the following:

//declare array:
int scores[20] = {90, 54, 23, 75, 67, 89, 99, 100, 34, 99, 
    97, 76, 73, 72, 56, 73, 72, 65, 86, 90};

/* give the variable 'smallest' a high value -
   higher than any in the array:
*/
int smallest = 9999;

/* inspect each value in the array and if it is greater than
   the value of 'smallest', set smallest to that value:
*/
int i;
for (i = 0; i < 20; ++i)
    if (scores[i] < smallest)
        smallest = scores[i];

Oh look the question is totally changed and now my answer looks completely insane :-/

James Morris
Please see http://meta.stackoverflow.com/questions/10811/homework-on-stackoverflow and http://meta.stackoverflow.com/questions/18242/what-is-the-policy-here-on-homework
Greg Hewgill
+7  A: 

The error is in the first line of the findsmallest() function definition. Get rid of the semicolon and it should work (barring other errors in the code -- I didn't check it for correctness).

void findsmallest(int scores [], int & min); <-------semicolon
{

vs

void findsmallest(int scores [], int & min) <--------no semicolon
{

The error is telling you that the open brace ({) that follows the semicolon is lacking a preceding class/struct/union/function declaration, so the compiler doesn't know what to do with it. Remove the semicolon and now the compiler knows that it's the body of a function definition.

Drew Hall
This is what I initially found as well
Joe Philllips
@d03boy--Nice edit. Much clearer than what I originally wrote!
Drew Hall
@d03boy -Thank you so much for clarifying the error message and yes I have remove the semicolon. Thanks Drew, very helpful and professional bunch of people here!!
Rosemary