Hello allisonOs,
Actually we are in the same time zone. Just Pennsylvania between us.
You are welcome. I am glad that you understand it better.
I am not sure about Java, but I would think that the concepts of a function is much like it is in C++.
In C++ a function is a way to break up the program into smaller pieces, (functions), that do 1 task. This is a way you keep from having the "main" function being 1000 lines or more. Later you will learn about putting functions in a separate ".cpp" file, so that the "main" file is not cluttered with code that it does not need right away.
Although I did have an idea of putting the sort in a function I was waiting to see if you know how to use them yet. The function you are referring to is from
seeplus. A nice idea for the function, but I did it a bit differently.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
int tempInt{};
double tempDouble{};
for (int idx = 0; idx < numOfStudents - 1; idx++)
{
for (int idxp1 = idx + 1; idxp1 < numOfStudents; idxp1++)
{
if (scoresArray[idx] > scoresArray[idxp1])
{
tempDouble = scoresArray[idx];
tempInt = studentID[idx];
scoresArray[idx] = scoresArray[idxp1];
studentID[idx] = studentID[idxp1];
scoresArray[idxp1] = tempDouble;
studentID[idxp1] = tempInt;
}
}
//std::cout << std::endl; // <--- Used as a break point for testing. Comment out delete when finished.
}
|
This code is used in "main", but I also put this in a function. Either way it works. This may not be as fancy as what
seeplus did, but it should give you a better understanding of how it works.
Lastly, I just had a genuine question about a header file you used.
Why is the #include <utility> used above? Is it for the swap function to work properly? If so, I deleted it just to test it out, but it seemed to work fine with just the basic, #include <iostream> . I added it back to my final code regardless to avoid any potential errors.
|
Yes the "std::swap" is defined in <utilities> header file. According to cppreference.com "std::swap" is also defined in the <algorithm> and <string_view> header files. Choosing which header file to use, (<utilities> or <algorithm>) may depend on what else you may need to use in 1 of these header files or it may depend on what you want to avoid.
Not knowing what IDE/compiler you are using it is very possible that <iostream> may include a <utility> header file without you even knowing about it. It may also come from 1 of the other header files that are automatically included into your program.
As to the last part. Since different compilers use different it is a good practice to include a header file even if your compiler does not need it. Someone else might.
An example I can use the line
|
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
|
without including the header file <limits> because <iostream> will include the header file <xlocnum> and in that it includes
1 2 3 4 5
|
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <streambuf>
|
Which covers what I need for the ignore line, but it is still better to include the proper <limits> header file.
Again
seelus has the more complex while loop. A good approach, but something you are not quite read for yet. This concept might be a little easier to understand:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
while (!std::cin || (scoresArray[i] > 100 || scoresArray[i] < 0))
{
if (!std::cin)
{
std::cerr << "\n Invalid Entry! Must be a number.\n\n";
std::cin.clear(); // <--- Resets the streams' state bits.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>. Clears the input buffer.
}
else if (scoresArray[i] > 100 || scoresArray[i] < 0)
{
cout << "\n Invalid entry\n\n";
}
cout << "Input test score for student " << i + 1 << " again: ";
cin >> scoresArray[i];
}
|
Using formatted input
cin >> scoresArray[i];
cin is expecting a number to be entered. If not the stream fails and is unusable until it is fixed.
This version of the while loop will catch both problems and give you a change to understand
seepluss' version.
The only warning I had when I compiled the had to do with using "size_t". The only guarantee is that "size_t" is an "unsigned something" The something is defined by the header files that you are using. This could be an "int" or a "long". Maybe even a "long long". The problem is in
for (size_t i = 1; i < n; ++i)
. Here "i", an unsigned something, is being compared to a signed int. Not a big problem, but a potential problem. It should at least be a compiler warning of of a signed unsigned mismatch. In the for loop I changed "size_t" to an "int" and the warning went away.
Andy