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 ==========

The first error on line 84 means that you are trying to treat hiscore.score as if it were a simple integer -- it isn't because its an array. My suggestion is to make it a simple integer. Do you really need it to be an array?

struct highscore
{
	string name;
	int score;
};

If you really do need that to be an array, then you will probably have to write another function to search it for hightest score so that it can be added on line 84.

I already tried it and well, I didn't know how I would come around this compiling error:

1>------ Build started: Project: Highscore, Configuration: Debug Win32 ------
1>Compiling...
1>mainhigh.cpp

1>%dir\highscore\highscore\mainhigh.cpp(90) : error C2664: 'strcpy' : cannot convert parameter 1 from 'std::string' to 'char *'
1>        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>Build log was saved at "file://%dir\Highscore\Highscore\Debug\BuildLog.htm"
1>Highscore - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Basically because of the string function strcpy only taking char pointers, right?
char *strcpy(char *_Dest, const char *_Source)

How can I use this for copying strings to each other instead? Changing the strings into chars doesnt help either I would guess, because that would be something like "cannot convert parameter 1 from char to char * ".


Switching score into an integer and switching according code:
CODE

struct highscore
{
	string name;
	int score;
};
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;		
	}

	cout << endl;

	return HS;
}
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;
		}

		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;
		string tempName;

		// spara temporärt + swap
		for(int i=0; i < n; i++)
		{
			// SCORE
			tempScore = h[i].score;
			h[i].score = h[i+1].score;

			h[i+1].score =  tempScore;

			// NAME
			strcpy(tempName, h->name);

			swap(h[i].name, tempName);
		}
	}
}

>>How can I use this for copying strings to each other instead?
You don't need strcpy() to copy one std::string to another. Just simple assignment operator = will do the trick

std::string s1 = "Hello World";
std::string s2 = s1;

Cheers for the tip, can't believe I didn't try it the simple/logical way. Somehow I thought that it wasn't possible with a simple assignment with strings. Cleaned up the code a bit and have no more compiling errors.

There's still one problem though, the sorting is all wrong. It sorts by the input order.
Any tips on how to make it work? I really thought it was all good in my void sort();
Oh and, I am also adding a choice for the fill(); later so that it prompts me before typing in a new name/score if I want to continue filling in scores(Yes) or (No) to display current inputted data then pause and exit program. But that one is quite easy (I think? :$ ). Lets worry about the sorting for now :-/

Current code:

#include <iostream>
#include <string>

using namespace std;

struct highscore
{
	string name;
	int score;
};

// 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);

	system("PAUSE");
	return 0;
}

highscore getHighscore()
{
	highscore HS;
	
	cout << "Skriv in namn: ";
	getline(cin, HS.name);

	cout << "Skriv in score: ";
	//for(int i = 0; i < 5; i++)
	//{	
		cin >> HS.score;
		cin.ignore();
	//}


	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 << "=======================================" << 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;
		//}

		cout << endl;
	}
}

void sort(highscore h[], int n)
{
	// IF CURRENT < CURRENT + 1
	if(h[n].score < h[n+1].score)
	{
		int tempScore;
		string tempName;

		// spara temporärt + swap
		for(int i=0; i < n; i++)
		{
			// SCORE
			tempScore = h[i].score;
			h[i].score = h[i+1].score;

			h[i+1].score =  tempScore;

			// NAME
			tempName = h->name;

			swap(h[i].name, tempName);
		}
	}
}

When doing the swap you have to swap the entire structure, not just the score. The name has to be swapped along with the score.

line 77 makes no sense -- you are comparing non-existant array elements because I assume parameter n is the number of structures in the array, which counts from 0 to (not including) n.

For small values of n I suggest using the bubble sort, because its the easiest to code. You will find lots of examples on the net.

Once again, thanks for the great help.

Brings me to next question(I tried all of the alternatives, not sure which one is best/which one I can use, all giving compiling error):
Just hollar if you want compiling errors for all of them

Can I declare like this and use it like I am with highscore temp?

void bubbleSort(highscore h[], int n)
{
  int i, j;
  highscore temp;

  for(i =(n - 1); i >= 0; i--)
  {
    for(j = 1; j <= i; j++)
    {
      if(h[j - 1] > h[j])
      {
        temp = h[j-1];
        h[j-1] = h[j];
        h[j] = temp;
      }
    }
  }
}

or using it like this:

if(h[j - 1] > h[j])
      {
        temp.score = h[j-1];
        h[j-1] = h[j];
        h[j] = temp.score;

	temp.name = h[j-1].name;
	swap(h[i].name, temp.name);
      }

or should I simply just do something similar to the old like this inside the bubblesort?

int tempScore;
string tempName;
void bubbleSort(highscore h[], int n)
{
  int i, j, tempScore;
  string tempName;

  for(i =(n - 1); i >= 0; i--)
  {
    for(j = 1; j <= i; j++)
    {
      if(h[j - 1] > h[j])
      {
        tempScore = h[j-1];
        h[j-1] = h[j];
        h[j] = tempScore;

		tempName = h[j-1].name;

		swap(h[i].name, tempName);
      }
    }
  }
}

Some update on this mess and stupid questions.

Compiled without problem and entered in 5 values, and crashing when it reached

bubbleSort(HS, 5);

I'd guess. I now feel kind of stuck

Crashed with errormessage:

Unhandled exception at 0x1026eef0 (msvcr90d.dll) in Highscore.exe: 0xC0000005: Access violation reading location 0x0041fffc.

Code as of now:

#include <iostream>
#include <string>

using namespace std;

struct highscore
{
	string name;
	int score;

	void operator=(highscore& highscore)
	{
       name = highscore.name;
	   score = highscore.score;
	}
};

// declare functions
highscore getHighscore();
void fill(highscore [], int);
void display(const highscore [], int);
void bubbleSort(highscore [], int);

// void sort(highscore [], int);

int main()
{
	// declare new struct
	highscore HS[5];

	// functions loaded
	fill(HS, 5);
	bubbleSort(HS, 5);
	display(HS, 5);


	cout << "=======================================" << endl;
	system("PAUSE");
	return 0;
}

highscore getHighscore()
{
	highscore HS;
	
	cout << "Skriv in namn: ";
	getline(cin, HS.name);

	cout << "Skriv in score: ";

	cin >> HS.score;
	cin.ignore();

	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 << "=======================================" << endl;

	for(int index = 0; index < n; index++)
	{
		cout << h[index].name << " \t \t \t \t ";

		cout << h[index].score;


		cout << endl;
	}
}

void bubbleSort(highscore h[], int n)
{
  int i, j;
  highscore temp[5];

   for (i=0; i < n; i++)
  {
	  for(j=0; j < n; j++)
	  {
		  int s1 = h[j].score;
		  int s2 = h[j+1].score;
		  string n1 = h[j].name;
		  string n2 = h[j+1].name;


		  int tempS = temp[i].score;
		  string tempN = temp[i].name;

		  if(s1 == s2)
		  {	  
			  tempS = s2;
			  s2 = s1;
			  s1 = tempS;
		  }
	  }
  }
}
This article has been dead for over six months. Start a new discussion instead.