Rosemary
Rosemary

Reputation: 11

Just Want to know what this error message really means!

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;

Upvotes: 1

Views: 278

Answers (5)

James Morris
James Morris

Reputation: 4955

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

Upvotes: 0

Drew Hall
Drew Hall

Reputation: 29065

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.

Upvotes: 7

AntonioMO
AntonioMO

Reputation: 896

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 ;)

Upvotes: 0

seh
seh

Reputation: 15269

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?

Upvotes: 0

bmargulies
bmargulies

Reputation: 100161

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

Upvotes: 0

Related Questions