array of highscore

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.

#include <iostream>

using namespace std;

char ihighscores[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
int iscore = 0;

int main()
{

while (ihighscores)
{
cin >> ihighscores;
cout << ihighscores << endl;
}
return 0;
}
*Sigh* Please use CODE TAGS! If you don't know how to do it, click the <> button on the editor.
Revized with code tags:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

using namespace std;

char ihighscores[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
int iscore = 0;

int main()
{

while (ihighscores)
{
cin >> ihighscores;
cout << ihighscores << endl;
}
return 0;
}

However, I don't exactly understand your question. Explain it a bit more?
//Bman
closed account (3qX21hU5)
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#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
    
}
Thank you and sorry i will add the code properly now.

I will get started on that :)
Bman,

I am doing a question for class. Here it is:

"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."
closed account (3qX21hU5)
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).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>

using namespace std;

int main()
{
    const unsigned 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.
Topic archived. No new replies allowed.