0

number of grades a user inputs. I am working with dynamic array allocation. I feel confident in my code, but Xcode is giving me an error in my sort function.I thought that I was doing it right, but apparently something is wrong, and I'm not entirely sure where. I am still trying to figure out dynamic memory allocation, so I'm sure that's where my error is generating from, I just don't know where the cause is. Here is my full program:

// This program demonstrates the use of dynamic arrays
#include <iostream>
#include <algorithm>
#include <iomanip>
using namespace std;

//Function Prototypes
void sort(float *score[], int numOfScores);

int main()
{
float  *scores;
int    total = 0;
float  average;
float  numOfScores;
int    count;

cout << fixed << showpoint << setprecision(2);

cout << "Enter the number of scores to be averaged and sorted.";
cin >> numOfScores;


scores = new float(numOfScores);

for ( count = 0; count < numOfScores;  count++)
{
    cout << "Please enter a score:" << endl;
    cin >> scores[count];         }

for (count = 0; count < numOfScores; count++)
{
    total = total + scores[count];
}

average = total / numOfScores;

cout << "The average score is " << average << endl;

sort(*scores, numOfScores);

delete [] scores;
return 0;
}

//*******************************************
//              Sort Function
// Bubble sort is used to sort the scores
//*******************************************
void sort(float *score[], int numOfScores)
{
   do
{
    bool swap = false;
    for (int count = 0; count < (numOfScores -1); count++)
    {
        if (*score[count] > *score[count+1])
        {
            float *temp = score[count];
            score[count] = score[count+1];
            score[count+1] = temp;
            swap = true;
        }
    }
}while(swap); //This is where I'm receiving the error. 
}

Thank you!

deathcat05
  • 429
  • 1
  • 5
  • 18
  • 1
    `scores = new float(numOfScores);` I seriously doubt that does what you think it does. Pretty sure you wanted `scores = new float[numOfScores];`. And `float *score[]` is certainly not correct for your parameter type. You want `float score[]`. – WhozCraig Apr 25 '16 at 16:39
  • Possible duplicate of [Use variables declared inside do-while loop in the condition](http://stackoverflow.com/questions/18541304/use-variables-declared-inside-do-while-loop-in-the-condition) – LogicStuff Apr 25 '16 at 16:42
  • That fixed that error message, but now I'm getting an error message related to "algorithm." I even tried taking out `#include ` and I still get an error message. Any ideas? – deathcat05 Apr 25 '16 at 16:49

1 Answers1

0

swap is local to the do...while loop so it cannot be used in the while condition. One would expect an error related to that but since you have using namespace std; and #include <algorithm> you have now introduced the std::swap function into the scope of the program

while(swap);

Is trying to convert std::swap to a function pointer but it can't as it is overloaded and it does not know which overload to use.

For further reading on why to avoid using using namespace std; see: Why is “using namespace std” in C++ considered bad practice?

Community
  • 1
  • 1
NathanOliver
  • 171,901
  • 28
  • 288
  • 402