HELP!! testscores code

hey guys, im pretty much done with this code, i just have one error thats not letting my code compile.. anyhelp would be GREATLY appreciated, im new to programming and this is due tomorrow.. thanks! the error im getting is: error C2065: 'lowest' : undeclared identifier
but i have it declared, so idk what the problem is..HELP PLEASE


#include <iostream>
#include <iomanip>

using namespace std;

void arrSelectSort(float *, int);
void showArrPtr(float *, int);
void showAverage(float, int);

int main()
{
float *scores, //To dynamically allocate an array
total=0.0, //Accumulator
average=0, //To hold the averge scores


lowest=100;
int numScores; //To hold the number of test scores
//Get the number of test scores.
cout << "How many test scores would you like to process?";
cin >> numScores;

//Make an array large enough to hold that many
//test scores
scores = new float[numScores];
if(scores==NULL)
return 0;

//Get the test score for each test
cout << "Enter the test scores below.\n";
for (int count = 0; count < numScores; count++)
{
do{
cout << "Enter Test Score: ";
cin >> scores[count];
if (scores[count]<0)
cout << "Scores cannot be negative. Please retry again.\n";
}while(scores[count]<0);
}

//Calculate the total scores
for (int count = 0; count < numScores; count++)
{
total += scores[count];
}

//sort the elements of the array pointers
arrSelectSort ( scores, numScores );

//Will display them in sorted order.
cout << "The test scores in ascending order are: \n";
showArrPtr ( scores, numScores );

showAverage( total, numScores );

//Free memory.
delete [] scores;
system ("pause");
return 0;
}
void arrSelectSort(float *array, int size)
{
int startScan, minIndex;
float minElem;

for (startScan = 0; startScan < ( size - 1 ); startScan++)
{
minIndex = startScan;
minElem = array[startScan];
for (int index = startScan + 1; index < size; index++)
{
if ( array[index] < minElem)
{
minElem = array[index];
minIndex = index;
}
}
array[minIndex] = array[startScan];
array[startScan] = minElem;
}
}

void showArrPtr(float *array, int size)
{
for (int count=0; count< size; count++)
cout << array[count] << " ";
cout << endl;
}

void showAverage(float total, int numScores)
{
float average;

average=average-lowest;
average=average/4.0;
cout << setw(4);
cout << fixed << showpoint << setprecision(2);
cout<<"Average is"<<average<<endl;
}





Last edited on
The code you present doesn't declare the identifier 'lowest' anywhere.

Also note that this forum can greatly enhance code posts if you use the code tags.
yes i defined it in beginning.. lowest=100;

and i dont know how to use code tags :[
[code]
1
2
3
//This is not a declaration of a variable.
//It is missing the data type:
lowest = 100;

[/code]
how should i declare the variable 'lowest' so it can function normally?
Well it is your program so you should know which data type you need/want. In general, a declaration has the data type followed by the identifier, followed optionally by constructor arguments or initialization (also a constructor call but using the equals sign).

So if your 'lowest' variable needed to be of type int, it would be int lowest = 100;. Refer to http://www.cplusplus.com/doc/tutorial/.
@webJose - lowest is declared correctly in main(), it's just formatted in a way that makes the comma list of intfloat decls less obvious ;-)

drick13, the problem is in function showAverage(), which is trying to use the 'lowest' declared in main() which isn't visible (lowest is local scope to main()).

You either need to pass lowest into showAverage() as a parameter, or make lowest a global variable - then all code will be able to see it.

Cheers,
Jim
Last edited on
Ah look at that. I see now. This is why I never declare variables like that. It is confusing. One line per variable, type included. It produces much more readable code, IMO.

@OP: Compilers usually give you the line number where the error appears. Next time have a closer look so you can at least tell us which line is giving you grief. Also use code tags so your code posting comes out with line numbers in this forum.
thanks a lot guys, i made lowest a global variable, but everytime i run the program. it tells me average is being used without being initialized... im pretty i have it initialized in the beginning ..any thoughts please??
1
2
3
4
5
void showAverage(float total, int numScores)
{
float average;

average=average-lowest;


You're setting average to itself minus lowest, without giving average an initial value.

Jim
Topic archived. No new replies allowed.