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 ;
}
}
 
What is the error given when you try and compile it?

Oh, and if you paste it in again in
Code:
tags it will preserve indentation making it clearer to read.
 
Last edited:
A bit rusty at C++ but I always used pointers to pass arrays around

so when calling the function use

TableOrder (&HSL, Name, Score, ArrayPlaces) ;


This will pass the address of the array to the function and at the top of your function change HSL to *HSL

edit: whoops just realised the HSL is the array not the array places variable LOL

ChuKKi
 
Last edited:
A few thibngs at first glance:

Change:
const ArrayPlaces = 4 ;
void TableOrder (HSL[], char NewName[50], float NewScore, const ArrayPlaces)
int CompareScores (float PlayerScore, float LowestScore) ;

to:
const int ArrayPlaces = 4 ;
void TableOrder (ATopPlayer HSL[], char NewName[50], float NewScore, const int ArrayPlaces)
int CompareScores (float PlayerScore, float LowestScore)

Also:
You have no declaration for NameHolder
PlayerPosition is declared inside an if statement thus you should not try and use it outside of that..
 
I have left a number of things not working, but it compiles.


Code:
//#include "stdafx.h"
#include <string>
#include <iostream>


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 ;
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)
{
	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::cout << Score ;

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

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

std::cout << "HIGH SCORES\n-----------\n"
<< "First Place : "
<< HighScore1.PlayerName
<< " with a score of "
<< HighScore1.PlayerScore << std::endl 
<< "Second Place : "
<< HighScore2.PlayerName
<< " with a score of "
<< HighScore2.PlayerScore << std::endl 
<< "Third Place : "
<< HighScore3.PlayerName
<< " with a score of "
<< HighScore3.PlayerScore << std::endl 
<< "Fourth Place : "
<< HighScore4.PlayerName
<< " with a score of "
<< HighScore4.PlayerScore << 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 = "", NameHolder2 = "" ;
	float ScoreHolder = 0, ScoreHolder2 = 0 ;
	int i = 0 ;

ScoreHolder2 = NewScore ;

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

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

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

NameHolder2 = NameHolder ;
ScoreHolder2 = ScoreHolder ;

PlayerPosition ++ ;
}
}
 
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



:(
 
First have a look at the variables you create:

Code:
ATopPlayer HighScore1, HighScore2, HighScore3, HighScore4 ;
	HighScore1.PlayerScore = 0 ;
 	HighScore2.PlayerScore = 0 ;
	HighScore3.PlayerScore = 0 ;
	HighScore4.PlayerScore = 0 ;
	
	ATopPlayer HSL [] = {HighScore1, HighScore2, HighScore3, HighScore4} ;

So you're creating 5 highscore's and initialising the playerscores to 0, then you create another 5 highscores in the HSL array and initialise it with the previous highscores. So in total you now have ten instances of ATopPlayer.

After that you pass the HSL array to your compare() function and then output the highscores via the HighScore1,2,3,4 variables - so do you see the fact that your working off two sets of the five variables?

In general if your doing var1,var2,var3,etc your doing something wrong that could use an array.

Your main loop can be rewritten to use just the HSL array like:

Code:
int main(int argc, char* argv[])
{
	char Name[50] ;
	float Score = 0 ;

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

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

	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, HSL[3].PlayerScore) ;

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

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

Another big problem in your code is the fact that your players names have no memory associated with them:

Code:
struct ATopPlayer {
	[b]char PlayerName[] ;[/b]
	float PlayerScore ;
} ;

PlayerName here doesnt have any memory and so when later your doing:

Code:
strcpy(HSL[PlayerPosition].PlayerName, NameHolder2) ;

It will exhibit undefined behaviour (anything can happen) - if your lucky it will crash, if your unlucky it will corrupt data (as in your case).

(This is abit of a simplistic statement - its actually being compiled via the c99 flexible array extension - but my point is still true)



You should follow FerretBoy's code example and change from char arrays (c-style strings) to c++'s std::string. Then you wont have to worry about the memory.

Last thing system("PAUSE"); can be replaced by the more portable std::cin.get(); function.
 
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? :) :(
 
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 ;

       // strcpy(NameHolder2, NewName) ;
	ScoreHolder2 = NewScore;
	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) ;
		NameHolder = HSL[PlayerPosition].PlayerName;

		ScoreHolder = HSL[PlayerPosition].PlayerScore ;
	
//    	strcpy(HSL[PlayerPosition].PlayerName, NameHolder2) ;
		HSL[PlayerPosition].PlayerName = NameHolder2;
		HSL[PlayerPosition].PlayerScore = ScoreHolder2 ;
		
//		strcpy(NameHolder2, NameHolder);
		NameHolder2 = NameHolder;

		ScoreHolder2 = ScoreHolder ;

		PlayerPosition++ ;
	}
}


You don't need to use strcpy with strings (or even strcat, just use NameHolder2.append sSomeVar etc :)).
 
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? :(
 
My version appeared to work for me. Maybe email him quickly and submit it ;).

When using chars strcat is used to concatenate (add together :p) variables. I.e.:

Code:
/* strcat example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str[80];
  strcpy (str,"these ");     // initially set str to these
  strcat (str,"strings ");    // append strings to str
  strcat (str,"are ");        // append are
  strcat (str,"concatenated.");  // etc
  puts (str);
  return 0;
}

(http://www.cplusplus.com/reference/clibrary/cstring/strcat.html)

By the end the str variable will be 'these strings are concatenated.'.

However when you use strings you don't need to use those commands, you can simply use someString.append(" extra.");

If you use Microsoft Visual Studio try typing the name of the string variable with a dot after it, you should get a little menu popup with various things you can do to it.

For reference: http://www.cppreference.com/cppstring/index.html



Now time for my rant:
I hate variables in C++, it seems to take so much work to setup and manipulate them, especially if you want to convert them to a different type. I know it's more efficient that way but I mainly program PHP where variables are so much better.

Code:
<?php
$variable = "It's ";
$variable .= " much easier to add text to PHP variables";
?>

Simple :D.
 
Last edited:
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 :)
 
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 = :(
 
You use strcpy for chars.

Strings have their own built in functions to let you easily manipulate them :)
 
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)
 
heh yeah :).

I think chars are generally prefered but strings are so much easier for things like this.
 
Back
Top Bottom