I'm doing a hw assignment and my professor uses this code to test our program:
int main()
{
const int SZ1 = 10;
const int SZ2 = 7;
const int SZ3 = 5;
float array1[SZ1];
float array2[SZ2];
float array3[SZ3];
DisplayValues(SortValues(GetValues(array1, SZ1), SZ1), SZ1);
DisplayValues(SortValues(GetValues(array2, SZ2), SZ2), SZ2);
DisplayValues(SortValues(GetValues(array3, SZ3), SZ3), SZ3);
return EXIT_SUCCESS;
}
float *DisplayValues(float *p, size_t n)
{
float previous = *p, *ptr, *end = p + n;
setiosflags(ios_base::fixed);
for (ptr = p; ptr < end; ++ptr) // get each element
{
cout << *ptr << '\n';
if (ptr != p) // if not first element...
{
if (previous < *ptr) // ...check sort order
{
cerr << "Error - Array sorted incorrectly\n";
return NULL;
}
}
previous = *ptr; // save this element
}
resetiosflags(ios_base::fixed);
return p;
}
#endif
I use
float *GetValues(float *p, size_t n)
{
float input;
float *start = p;
cout << "Enter " << n << " float values separated by whitespace: \n";
while (scanf("%f", &input) == 1) {
*p++ = input;
}
return start;
}
to get input from the terminal window as he instructed, and use ctrl + d to input an EOF character in order for the first call to DisplayValues(SortValues(GetValues(array1, SZ1), SZ1), SZ1) to happen. However, the rest of the program just finishes without letting me enter values again when DisplayValues(SortValues(GetValues(array2, SZ2), SZ2), SZ2); is called. Is there a reason or workaround for this? Thanks.