So I am making an array of 10 high scores. For now all I want to happen is the user gives input and then it will place the scores from biggest to smallest and display it on the screen. They aren't playing any game yet I just want to be able to see it is getting the correct input and organizing it properly.
was wondering where I would go from here? Any help or links to some tutorial or documentation would be much appreciated. Thank you.
Well first I would switch from a array to a vector for several reasons. One of them being that array's are fixed sized so if you wanted to add more then 10 highscores it would mean a lot more reworking of the code, where as vectors can grow very easily.
Now I think you are having problems with the overall design of how to do this. So I'll give you some framework to work off of.
#include <vector>
#include <iostream>
int main()
{
int score = 0; // Score can be double, string, int or any type you want
vector<int> highscores;
// Runs the loop 10 times
for (int index = 0; index != 10; ++index)
{
// Ask the user to enter their score
cin >> score;
// Now we add the score to the highscore vector
highscores.push_back(score);
// Then the loop runs again until it has ran 10 times
}
// This is where you will sort all the scores in your vector or array. Check out the sort() function
}
"Create a small console application that creates an array that is capable of storing 10 values. These values will represent high score values. Using a while loop initialise the array with some values that are gathered from the user."
Ok so you must use a array then. What part are you having trouble with? The initialization of the array in a while loop? Or the sorting portion that you mentioned earlier?
Here is a project I am putting together about arrays and their common uses since array questions seem to be very common on here. Maybe it can help give you some ideas (Still got to add a lot more things to it but it might help the way it is now).
#include <iostream>
usingnamespace std;
int main()
{
constunsigned arraySize = 5;
// Remember that arrays that are not initialized and that are in a function like int main()
// have all there elements as undefined. So make sure you define them before calling them.
int array1[] = {1, 2, 3, 4, 5};
int array2[arraySize];
int array3[arraySize] = {1, 2, 3, 5, 6};
int arrayLoop[arraySize];
int ptrArray[arraySize];
// Initialize all elements in the array from user input in a while loop
size_t index = 0;
while (index != arraySize)
{
cout << "Enter a number: ";
cin >> arrayLoop[index];
++index;
}
// Copying the elements of array1 into array2
for (size_t index = 0; index != arraySize; ++index)
{
array2[index] = array1[index];
}
// Printing array elements with a subscript
cout << "Printing elements in array2 \n" << endl;
for (size_t index = 0; index != arraySize; ++index)
cout << array2[index] << " ";
// One way of comparing arrays to see if they are the same
cout << " \n Comparing arrays testing \n" << endl;
cout << "Testing array1 and array2" << endl;
for (size_t index = 0; index != arraySize; ++index)
{
if (array1[index] != array2[index])
cout << "Element" << index << "Does not match!" << endl;
}
cout << "Testing array1 and array3" << endl;
for (size_t index = 0; index != arraySize; ++index)
{
if (array1[index] != array3[index])
cout << "Element " << index << "Does not match!" << endl;
}
// Demostrates how pointers can set elements in a array
int *pBegin = ptrArray; // *pBegin points to the beginning of the array
int *pEnd = ptrArray + arraySize; // *pEnd points the just after the last element of the array
while (pBegin != pEnd)
{
// Initialize all elements to 0
*pBegin = 0;
++pBegin;
}
return 0;
}
One of the first ones in there shows you how to initialize a array from user input while using a loop. If you are having trouble with sorting though I would recommend researching the "Bubble Sort algorithm" since that is what most beginners use for sorting.