C++ Problem - Passing Parameters

Soldato
Joined
4 Mar 2006
Posts
3,712
Location
Wales
Hi all, I'm working on the below piece of code, and am having a lot of trouble passing an array through to another function. I've highlighted in red where I called the function, but I was wondering if you could take a look and perhaps point out why it won't let me pass it through :)

First, sorry for the lack of comments in the code, so I'll include my mission statement so you know what's what.

Mission Statement said:
Using the struct mechanism, create a structure with the name ATopPlayer. The structure should have two members: PlayerName and PlayerScore. Set up an array called HSL (short for High Score List) of four of these structure elements. The array should initially contain no names and no scores.

Write the code that allows the HSL table to be updated from the keyboard. The code should be set up so that it terminates when a score of -1 is entered.

The HSL should be ordered so that the highest score is in position 0 and the next lowest at position 1 etc. Each time a player’s score is entered the program should work out whether it is high enough to be included in the HSL. If it is, then the player’s name should also be requested by the program and entered by the user.

Each time a new score is added to the HSL other elements might have to move. For example, say the first score entered was 42 then, as it is the highest score to date, this would be put into position 0. Now if the next score was 59, the value 42 and the name of the player that obtained the score would be moved from position 0 to position 1 while the value 59 and the name of the player that obtained the score would be put in position 0. Obviously as more scores get entered names and values that were originally stored in HSL might be removed from the table altogether.

After each score/name is entered an updated HSL should be displayed on the screen.

Broken Code said:
#include "stdafx.h"
#include <string.h>
#include <iostream.h>
int CompareScores(float, float) ;
void TableOrder (struct, char, float, const) ;

struct ATopPlayer {
char PlayerName[50] ;
float PlayerScore ;
} ;



int main(int argc, char* argv[])
{
char Name[50] = "" ;
float Score = 0 ;
ATopPlayer HighScore1, HighScore2, HighScore3, HighScore4 ;
HighScore1.PlayerScore = 0 ;
HighScore2.PlayerScore = 0 ;
HighScore3.PlayerScore = 0 ;
HighScore4.PlayerScore = 0 ;

ATopPlayer HSL [4] ;
HSL[0]=HighScore1 ; HSL[1]=HighScore2 ; HSL[2]=HighScore3 ; HSL[3]=HighScore4 ;

const ArrayPlaces = 4 ;

while (Score != -1)
{
cout << "Please input your name and score. Input a score of -1 to terminate the program.\n" ;
cout << "Player Name : " ;
cin >> Name ;
cout << "Score : " ;
cout << Score ;

int ScoreCompare = CompareScores (Score, HighScore4.PlayerScore) ;

if (ScoreCompare == 1)
TableOrder (HSL, Name, Score, ArrayPlaces) ;
else
cout << "You score wasn't good enough! Try again." ;

cout << "HIGH SCORES\n-----------\n"
<< "First Place : "
<< HighScore1.PlayerName
<< " with a score of "
<< HighScore1.PlayerScore << endl
<< "Second Place : "
<< HighScore2.PlayerName
<< " with a score of "
<< HighScore2.PlayerScore << endl
<< "Third Place : "
<< HighScore3.PlayerName
<< " with a score of "
<< HighScore3.PlayerScore << endl
<< "Fourth Place : "
<< HighScore4.PlayerName
<< " with a score of "
<< HighScore4.PlayerScore << endl ;
}


return 0;
}


int CompareScores (float PlayerScore, float LowestScore) ;
{
int TableEntry = 0 ;

if (Playerscore > LowestScore)
Table Entry = 1 ;

return TableEntry ;
}

void TableOrder (HSL[], char NewName[50], float NewScore, const ArrayPlaces)
{
NameHolder = "", NameHolder2 = "" ;
float ScoreHolder = 0, ScoreHolder2 = 0 ;
int i = 0 ;

ScoreHolder2 = NewScore ; NameHolder2 = NameHolder ;

for (int counter=0 ; counter<4 ; counter++)
{
if (NewScore > ATopPlayer[counter].PlayerScore)
{
int PlayerPosition = counter ;
break ;
}
}

while (PlayerPosition > 5)
{
NameHolder = ATopPlayer[PlayerPosition].PlayerName ;
ScoreHolder = ATopPlayer[PlayerPosition].PlayerScore ;

ATopPlayer[PlayerPosition].PlayerName = NameHolder2 ;
ATopPlayer[PlayerPosition].PlayerScore = ScoreHolder2 ;

NameHolder2 = NameHolder ;
ScoreHolder2 = ScoreHolder ;

PlayerPosition + 1 ;
}
}
 
Ok I now have the following compiling and seemingly working code, having worked out a lot of stupid errors in the original.

Code:
#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;

int CompareScores(float, float) ;

struct ATopPlayer {
	char PlayerName[] ;
	float PlayerScore ;
} ;
	
void TableOrder (ATopPlayer HSL[], char NewName[], float NewScore, const int ArrayPlaces) ;


int main(int argc, char* argv[])
{
	char Name[50] ;
	float Score = 0 ;
	ATopPlayer HighScore1, HighScore2, HighScore3, HighScore4 ;
	HighScore1.PlayerScore = 0 ;
 	HighScore2.PlayerScore = 0 ;
	HighScore3.PlayerScore = 0 ;
	HighScore4.PlayerScore = 0 ;
	
	ATopPlayer HSL [] = {HighScore1, HighScore2, HighScore3, HighScore4} ;
	
	const int ArrayPlaces = 4 ;

	while (Score != -1)
	{
		cout << "Please input your name and score. Input a score of -1 to terminate the program.\n" ;
		cout << "Player Name : " ;
		cin >> Name ;
		cout << "Score : " ;
		cin >> Score ;

		int ScoreCompare = CompareScores (Score, HighScore4.PlayerScore) ;

		if (ScoreCompare == 1)
			TableOrder (HSL, Name, Score, ArrayPlaces) ;
		else
			cout << "You score wasn't good enough! Try again.\n\n" ;

		cout << "HIGH SCORES\n-----------\n"
			 << "First Place : "
			 << HighScore1.PlayerName
			 << " with a score of  "
			 << HighScore1.PlayerScore << endl 
			 << "Second Place : "
			 << HighScore2.PlayerName
			 << " with a score of  "
			 << HighScore2.PlayerScore << endl 
			 << "Third Place : "
			 << HighScore3.PlayerName
			 << " with a score of  "
			 << HighScore3.PlayerScore << endl 
			 << "Fourth Place : "
			 << HighScore4.PlayerName
			 << " with a score of  "
			 << HighScore4.PlayerScore << endl << endl ;
	}
	
  
  system("PAUSE");	
  return 0;
}


int CompareScores (float PlayerScore, float LowestScore)
{
	int TableEntry = 0 ;

	if (PlayerScore > LowestScore)
		TableEntry = 1 ;

	return TableEntry ;
}

void TableOrder (ATopPlayer HSL[], char NewName[], float NewScore, const int ArrayPlaces)
{
	char NameHolder[50] ; char NameHolder2[50] ;
	float ScoreHolder = 0 ; float ScoreHolder2 = 0 ;
	int i = 0 ; int PlayerPosition = 0 ;

	ScoreHolder2 = NewScore ; strcpy(NameHolder2, NewName) ;

	for (int counter=0 ; counter<4 ; counter++)
	{
		if (NewScore > HSL[counter].PlayerScore)
		{
			PlayerPosition = counter ;
			break ;
		}
	}

	while (PlayerPosition < 5)
	{	
        strcpy(NameHolder, HSL[PlayerPosition].PlayerName) ;
		ScoreHolder = HSL[PlayerPosition].PlayerScore ;
	
    	strcpy(HSL[PlayerPosition].PlayerName, NameHolder2) ;
		HSL[PlayerPosition].PlayerScore = ScoreHolder2 ;
		
		strcpy(NameHolder2, NameHolder);
		ScoreHolder2 = ScoreHolder ;

		PlayerPosition++ ;
	}
}

However there still seems to be some kind of error in the final function, which I believe lies somewhere in this bit of coding


Code:
	while (PlayerPosition < 5)
	{	
        strcpy(NameHolder, HSL[PlayerPosition].PlayerName) ;
		ScoreHolder = HSL[PlayerPosition].PlayerScore ;
	
    	strcpy(HSL[PlayerPosition].PlayerName, NameHolder2) ;
		HSL[PlayerPosition].PlayerScore = ScoreHolder2 ;
		
		strcpy(NameHolder2, NameHolder);
		ScoreHolder2 = ScoreHolder ;

		PlayerPosition++ ;
	}

But I'm not entirely sure.

Basically, what happens is the program runs, I can input name and score etc, I know it goes into the functions because I've added a few cout lines here and there in testing, and yet it still return a blank High Score Table. This is really getting to me, as I've checked over the various holding variables and it SHOULD all swap how it's meant to. See my output below

cerror.jpg



:(
 
Ok, I have the following code, that appears to be working but for 4 errors that won't let it compile:

Code:
#include "stdafx.h"
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;


int CompareScores(float, float) ;

struct ATopPlayer {
	std::string PlayerName;
	float PlayerScore ;
} ;
	
void TableOrder (ATopPlayer HSL[], std::string NewName, float NewScore, const int ArrayPlaces) ;


int main(int argc, char* argv[])
{
	std::string Name = "" ;
	float Score = 0 ;

	const int NumberOfHighScores = 5;
	ATopPlayer HSL[NumberOfHighScores];

	for(int i=0;i<NumberOfHighScores;i++ )
	{
		HSL[i].PlayerScore = 0;
	}

	while (Score != -1)
	{
		std::cout << "Please input your name and score. Input a score of -1 to terminate the program.\n" ;
		std::cout << "Player Name : " ;
		std::cin >> Name ;
		std::cout << "Score : " ;
		std::cin >> Score ;

		int ScoreCompare = CompareScores (Score, HSL[3].PlayerScore) ;

		if (ScoreCompare == 1)
			TableOrder (HSL, Name, Score, NumberOfHighScores) ;
		else
			std::cout << "You score wasn't good enough! Try again.\n\n" ;

		std::cout << "HIGH SCORES\n-----------\n"
			 << "First Place : "
			 << HSL[0].PlayerName
			 << " with a score of  "
			 << HSL[0].PlayerScore << std::endl 
			 << "Second Place : "
			 << HSL[1].PlayerName
			 << " with a score of  "
			 << HSL[1].PlayerScore << std::endl 
			 << "Third Place : "
			 << HSL[2].PlayerName
			 << " with a score of  "
			 << HSL[2].PlayerScore << std::endl 
			 << "Fourth Place : "
			 << HSL[3].PlayerName
			 << " with a score of  "
			 << HSL[3].PlayerScore << std::endl << std::endl ;
	}
	
  
  return 0;
}


int CompareScores (float PlayerScore, float LowestScore)
{
	int TableEntry = 0 ;

	if (PlayerScore > LowestScore)
		TableEntry = 1 ;

	return TableEntry ;
}

void TableOrder (ATopPlayer HSL[], std::string NewName, float NewScore, const int ArrayPlaces)
{
	std::string NameHolder; std::string NameHolder2;
	float ScoreHolder = 0 ; float ScoreHolder2 = 0 ;
	int i = 0 ; int PlayerPosition = 0 ;

	ScoreHolder2 = NewScore ; strcpy(NameHolder2, NewName) ;

	for (int counter=0 ; counter<4 ; counter++)
	{
		if (NewScore > HSL[counter].PlayerScore)
		{
			PlayerPosition = counter ;
			break ;
		}
	}

	while (PlayerPosition < 5)
	{	
        strcpy(NameHolder, HSL[PlayerPosition].PlayerName) ;
		ScoreHolder = HSL[PlayerPosition].PlayerScore ;
	
    	strcpy(HSL[PlayerPosition].PlayerName, NameHolder2) ;
		HSL[PlayerPosition].PlayerScore = ScoreHolder2 ;
		
		strcpy(NameHolder2, NameHolder);
		ScoreHolder2 = ScoreHolder ;

		PlayerPosition++ ;
	}
}

I am getting the error, for the bottom piece of code (in my strcopy uses)

error C2664: 'strcpy' : cannot convert parameter 1 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'char *'

I have no idea what this one means. Could anyone possibly give me one last piece of help? :) :(
 
Gah I wish I was better at programming :p

Pho said:
You don't need to use strcpy with strings (or even strcat, just use NameHolder2.append sSomeVar etc ).

I have no idea what that command does sorry :( Only know strcpy right now.

Just to clarify, I have submitted a non working piece of code to a lecturer, I am only looking to fix this to help my own knowledge.

Is the above thing from Pho what is causing my error perchance? :(
 
Pho said:
Well where you had say:
strcpy(NameHolder2, NewName) ;

which copies the value of NewName into NameHolder2 I changed it to:
NameHolder2 = NewName;

The other lines are exactly the same, just using different variables.

The only lines I changed were the ones which were erroring :)


I thought you were supposed to use strcpy and not = though? :(

I see how it works, I just don't understand why he'd lie to use and tell us to copy one string into another location you need strcpy instead of = :(
 
Pho said:
You use strcpy for chars.

Strings have their own built in functions to let you easily manipulate them :)


I'm going to slap my lecturer for not teaching us fully then :p

Had to do my own research etc :o


Since he's primarily a C programmer, part of the problem could be that half of what hes teaching us is him mixing C and C++... meaning we miss the bigger features of C++ (Like string manipulation)
 
FerretBoy said:
Code:
string variable="It's";
variable += " not so hard in C++ either";

badgermonkey - your original code was almost 100% C, so yes your lecturer is not teaching you good C++, or even good C, just a mix of both. Explains your surprise with string, namespaces etc..

for some structured learning check out:
Accelerated C++: Practical Programming by Example
by Andrew Koenig and Barbara Moo.


I'll have to look at that then, thanks :)

(At the moment the 2 books i have are C++ for Dummies and Teach yourself C++ in 21 days. Haven't really loked at them though)
 
phykell said:
Hardly what an experienced programmer would do, but why don't you just make your array global (i.e. declared outside the scope of all your functions). I base this suggestion on the fact that you've declared your structure globally and I also get the impression that your lecturer may have intended for you to do this as well - I imagine he's not even trying to get you using pointers and so on yet?


We've been told structures must be global (fow now anyway). Also been told that we will be shot if we use global arrays or variables. :(


Starting a proper look at classes and objects now... let's see him teach us basic C for that! :p
 
Back
Top Bottom