Heya, could use some help from any nice fellah around.
This is a highscore that sorts by score[index] and then name[index] follows.
There's some more features I would like to add to it like a menu e t c, but for now I want the basic thing working asap. Help with a solution please :confused:
Basically, my intention of the output is this:
Intended Output
Namn Score
Name[1] 213
Name[4] 174
Name[3] 68
Name[5] 26
Name[2] 1
Sorted with whatever is highest and name following. Compiling error is at bottom
Code
#include <iostream>
#include <string>
using namespace std;
struct highscore
{
string name;
int score[5];
};
// declare functions
highscore getHighscore();
void fill(highscore [], int);
void display(const highscore [], int);
void sort(highscore [], int);
int main()
{
// declare new struct
highscore HS[5];
// functions loaded
fill(HS, 5);
sort(HS, 5);
display(HS, 5);
}
highscore getHighscore()
{
highscore HS;
cout << "Skriv in namn: ";
getline(cin, HS.name);
cout << endl;
cout << "Skriv in score: ";
for(int i = 0; i < 5; i++)
{
cin >> HS.score[i];
}
cout << endl;
return HS;
}
void fill(highscore h[], int n)
{
for(int i = 0; i < n; i++)
h[i] = getHighscore();
}
void display(const highscore h[], int n)
{
cout << "Namn \t \t \t \t Scores" << endl;
for(int index = 0; index < n; index++)
{
cout << h[index].name << " \t \t \t \t ";
for(int i = 0; i< 5; i++)
{
cout << h[index].score[i];
}
cout << endl << endl << endl;
}
}
void sort(highscore h[], int n)
{
// om nuvarande plats < plats + 1
if(h[n].score < h[n+1].score)
{
int tempScore[5];
string tempName[5];
// spara temporärt + swap
for(int i=0; i < n; i++)
{
// SCORE
tempScore[i] = h[i].score;
h[i].score = h[i+1].score;
h[i+1].score = tempScore[i];
// NAME
strcpy(tempName+i, h->name);
swap(h[i].name, tempName[i]);
}
}
}
Compiling error
1>------ Build started: Project: Highscore, Configuration: Debug Win32 ------
1>Compiling...
1>mainhigh.cpp
1>%PATHDIR\highscore\highscore\mainhigh.cpp(84) : error C2440: '=' : cannot convert from 'int [5]' to 'int'
1> There is no context in which this conversion is possible
1>%PATHDIR\highscore\highscore\mainhigh.cpp(85) : error C2106: '=' : left operand must be l-value
1>%PATHDIR\highscore\highscore\mainhigh.cpp(87) : error C2440: '=' : cannot convert from 'int' to 'int [5]'
1> There are no conversions to array types, although there are conversions to references or pointers to arrays
1>%PATHDIR\Highscore\highscore\highscore\mainhigh.cpp(90) : error C2664: 'strcpy' : cannot convert parameter 1 from 'std::string *' to 'char *'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>Build log was saved at "file://%PATHDIR\highscore\highscore\Debug\BuildLog.htm"
1>Highscore - 4 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========