returning a character array

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 1181 Views - Last Post: 09 January 2010 - 11:03 AM Rate Topic: -----

#1 monckey100  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 27-November 09

returning a character array

Posted 07 January 2010 - 11:11 PM

well i been having this annoying problem that doesnt seem to go away like a boil that keeps on getting popped
int loadfile(short choice,int temp)															
{
  int Health;
  int Experience;
  char digimonname[50];
  char *username = new char[50];
  int intelligence;
  int strength;
  int luck;
  int dexterity;
  ifstream myfile ("SaveFile.dat");																//Open up savefile.dat, coulda been a variable but then i thought having only 1 save file is well enough, multiples would not be necessary for this game
  if (myfile.is_open())																			//opens the file "Savefile.dat" in real time so there is no jumps when they check their statistics etc
  {	
	while (! myfile.eof() )
	{
		myfile >> Health >> Experience >> digimonname >> username >> dexterity >> intelligence >> strength >> luck; //Read the file, put it in to a value then go to the next line.
	}
	myfile.close();																		//Close the file!
  }

  else cout << "Unable to open file, file either corrupted or deleted...\n:("; 
 switch (choice)																			//Returns the read file, data and gives myself an option as to either return something or cout out the statistics
	 {
  case 0:cout << "Username: " << username << "\nDigimon Name: " << digimonname << "\nHealth: " << Health << "\nExperience: " << Experience <<"\nDexterity:  " << dexterity << "\nIntelligence: " << intelligence <<  "\nStrength: " << strength << "\nLuck: " << luck; break;
  case 1:return Health; break;
  case 2:return Experience; break;
  case 3:return digimonname[50]; break;
  case 4:return username[50]; break;
  case 5:return dexterity; break;
  case 6:return intelligence; break;
  case 7:return strength; break;
  case 8:return luck; break;
  case 9:Health -= temp; break;
  case 10:Health += temp; break;
  case 11:Experience += temp; break;
  case 12:intelligence += temp; break;
  case 13:strength += temp; break;
  case 14:luck += temp; break;
  case 15: dexterity += temp; break;
};


first part of code
second part calling it
char Name[50];
 switch (choice)
  {
		case 1:
			Name = loadfile(4,0);
						break;
}



Whats been busting my hump is how do i return an character array properly?
i can return single chars but not multiple ._.

This post has been edited by monckey100: 07 January 2010 - 11:12 PM


Is This A Good Question/Topic? 0
  • +

Replies To: returning a character array

#2 seeP+  Icon User is offline

  • D.I.C Addict

Reputation: 55
  • View blog
  • Posts: 601
  • Joined: 20-July 09

Re: returning a character array

Posted 07 January 2010 - 11:22 PM

Your function is an int. It needs to be a char.
Was This Post Helpful? 1
  • +
  • -

#3 jimblumberg  Icon User is online

  • member icon


Reputation: 4235
  • View blog
  • Posts: 13,304
  • Joined: 25-December 09

Re: returning a character array

Posted 07 January 2010 - 11:33 PM

A function may only have one return type, and you defined your function as
int loadfile(short choice,int temp) meaning it will only return an INT.

Jim
Was This Post Helpful? 1
  • +
  • -

#4 maybnxtseasn  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 87
  • Joined: 04-April 09

Re: returning a character array

Posted 07 January 2010 - 11:33 PM

and your switch statement doesn't make sense....how is that a menu? .....explain what your trying to do and i can help u...

This post has been edited by maybnxtseasn: 07 January 2010 - 11:34 PM

Was This Post Helpful? 0
  • +
  • -

#5 monckey100  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 27-November 09

Re: returning a character array

Posted 08 January 2010 - 05:30 PM

View Postmaybnxtseasn, on 7 Jan, 2010 - 10:33 PM, said:

and your switch statement doesn't make sense....how is that a menu? .....explain what your trying to do and i can help u...

its not an menu its just a load file for my own game debugging usage, i kept it cleaner by adding loss of hp/statistics in the load file for auto saving/loading. which is why it's like that.
don't worry the switch statement is exactly how i want it, thanks everyone for pointing out my problem.
Was This Post Helpful? 0
  • +
  • -

#6 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1102
  • View blog
  • Posts: 4,597
  • Joined: 09-June 09

Re: returning a character array

Posted 08 January 2010 - 06:21 PM

take a look, return a char pointer
#include<iostream>

using namespace std;

char *return_array(void)
{
	char *test = new char[50];

	///do something

	//return char pointer
	return test;
}



int main()
{
	char *tester = return_array();
	
	

	cin.ignore();
	cin.get();

	return 0;	
}
	 


	


This post has been edited by ImaSexy: 08 January 2010 - 06:22 PM

Was This Post Helpful? 1
  • +
  • -

#7 monckey100  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 27-November 09

Re: returning a character array

Posted 08 January 2010 - 11:06 PM

View PostImaSexy, on 8 Jan, 2010 - 05:21 PM, said:

take a look, return a char pointer
#include<iostream>

using namespace std;

char *return_array(void)
{
	char *test = new char[50];

	///do something

	//return char pointer
	return test;
}



int main()
{
	char *tester = return_array();
	
	

	cin.ignore();
	cin.get();

	return 0;	
}
	 


	


after reading your code and comparing it with my version a few times. i realized that this method errors with the load file :|
char *loadfile(short choice,int temp)															
{
  int Health;
  int Experience;
  char digimonname[50];
  char *username = new char[50];
  int intelligence;
  int strength;
  int luck;
  int dexterity;
  ifstream myfile ("SaveFile.dat");																//Open up savefile.dat, coulda been a variable but then i thought having only 1 save file is well enough, multiples would not be necessary for this game
  if (myfile.is_open())																			//opens the file "Savefile.dat" in real time so there is no jumps when they check their statistics etc
  {	
	while (! myfile.eof() )
	{
		myfile >> Health >> Experience >> digimonname >> username >> dexterity >> intelligence >> strength >> luck; //Read the file, put it in to a value then go to the next line.
	}
	myfile.close();																		//Close the file!
  }

  else cout << "Unable to open file, file either corrupted or deleted...\n:("; 
 switch (choice)																			//Returns the read file, data and gives myself an option as to either return something or cout out the statistics
	 {
  case 0:cout << "Username: " << username << "\nDigimon Name: " << digimonname << "\nHealth: " << Health << "\nExperience: " << Experience <<"\nDexterity:  " << dexterity << "\nIntelligence: " << intelligence <<  "\nStrength: " << strength << "\nLuck: " << luck; break;
  case 1:return Health; break;
  case 2:return Experience; break;
  case 3:return digimonname[50]; break;
  case 4:return username; break;
  case 5:return dexterity; break;
  case 6:return intelligence; break;
  case 7:return strength; break;
  case 8:return luck; break;
  case 9:Health -= temp; break;
  case 10:Health += temp; break;
  case 11:Experience += temp; break;
  case 12:intelligence += temp; break;
  case 13:strength += temp; break;
  case 14:luck += temp; break;
  case 15: dexterity += temp; break;
};


so im back to base A where i can only return a single character .-.
(using the methods stated in the first 2-3 posts)

This post has been edited by monckey100: 08 January 2010 - 11:14 PM

Was This Post Helpful? 0
  • +
  • -

#8 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1102
  • View blog
  • Posts: 4,597
  • Joined: 09-June 09

Re: returning a character array

Posted 09 January 2010 - 01:11 AM

What are you even trying to return?? Is the problem trying to retrieve all of the attributes from your function? I think you need to start looking at the OOP part of C++ my friend

#include <iostream>
#include <fstream>

using namespace std;

 struct player
 {
	 int Health;
	 int Experience;
	 char *digimonname;
	 char *username;
	 int intelligence;
	 int strength;
	 int luck;
	 int dexterity;

	 player(void)
	 {
		 digimonname = new char[50];
		 username = new char[50];
	 }

 };

player *loadfile(ifstream &myfile)															
{				 
  player *newPlayer = new player();
  if (!myfile.is_open())	 
	  return NULL;
 
	while (!myfile.eof())
	{
		myfile >> newPlayer->Health >> newPlayer->Experience >> newPlayer->digimonname >> newPlayer->username 
			   >> newPlayer->dexterity >> newPlayer->intelligence >> newPlayer->strength >>newPlayer->luck; 
	}
	return newPlayer;
}

int main()
{
	ifstream file("SaveFile.dat");
	player *newPlayer = loadfile(file);

	if(newPlayer == NULL)
	{
		cout<<"FILE COULD NOT OPEN"<<endl;
		cin.get();
		exit(0);
	}

	cout<<"Player Attributes"<<endl
		<<newPlayer->username<<endl
		<<newPlayer->digimonname<<endl
		<<newPlayer->dexterity<<endl
		<<newPlayer->Experience<<endl
		<<newPlayer->Health<<endl
		<<newPlayer->intelligence<<endl
		<<newPlayer->luck<<endl
		<<newPlayer->strength<<endl;



	


	cin.ignore();
	cin.get();
	return 0;
}
	
 




Was This Post Helpful? 0
  • +
  • -

#9 monckey100  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 27-November 09

Re: returning a character array

Posted 09 January 2010 - 01:18 AM

View PostImaSexy, on 9 Jan, 2010 - 12:11 AM, said:

What are you even trying to return?? Is the problem trying to retrieve all of the attributes from your function? I think you need to start looking at the OOP part of C++ my friend

#include <iostream>
#include <fstream>

using namespace std;

 struct player
 {
	 int Health;
	 int Experience;
	 char *digimonname;
	 char *username;
	 int intelligence;
	 int strength;
	 int luck;
	 int dexterity;

	 player(void)
	 {
		 digimonname = new char[50];
		 username = new char[50];
	 }

 };

player *loadfile(ifstream &myfile)															
{				 
  player *newPlayer = new player();
  if (!myfile.is_open())	 
	  return NULL;
 
	while (!myfile.eof())
	{
		myfile >> newPlayer->Health >> newPlayer->Experience >> newPlayer->digimonname >> newPlayer->username 
			   >> newPlayer->dexterity >> newPlayer->intelligence >> newPlayer->strength >>newPlayer->luck; 
	}
	return newPlayer;
}

int main()
{
	ifstream file("SaveFile.dat");
	player *newPlayer = loadfile(file);

	if(newPlayer == NULL)
	{
		cout<<"FILE COULD NOT OPEN"<<endl;
		cin.get();
		exit(0);
	}

	cout<<"Player Attributes"<<endl
		<<newPlayer->username<<endl
		<<newPlayer->digimonname<<endl
		<<newPlayer->dexterity<<endl
		<<newPlayer->Experience<<endl
		<<newPlayer->Health<<endl
		<<newPlayer->intelligence<<endl
		<<newPlayer->luck<<endl
		<<newPlayer->strength<<endl;



	


	cin.ignore();
	cin.get();
	return 0;
}
	
 




im trying to return the full username from the file, which is why theres that many returns(i don't want to return the intelligence etc just the name) :|

This post has been edited by monckey100: 09 January 2010 - 01:20 AM

Was This Post Helpful? 0
  • +
  • -

#10 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: returning a character array

Posted 09 January 2010 - 01:20 AM

char *loadfile(short choice,int temp)  


This function must return a char *. However, take a look at this line inside the function:
 int intelligence;


Take a look here too (this is inside the function too):
case 6:return intelligence; break;


This means that the function will return an integer (intelligence is an int). So, this is the problem. Maybe set the function to return an int?
Was This Post Helpful? 0
  • +
  • -

#11 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1102
  • View blog
  • Posts: 4,597
  • Joined: 09-June 09

Re: returning a character array

Posted 09 January 2010 - 01:27 AM

either use a structure like the one i created for you, or pass a pointer for the username to your function

int loadfile(short choice,int temp, char *username)		



you really should program your players around objects though, its pointless to have all of that data in your function and then just return a single value from it. All of those attributes die once you reach the end of your function
Was This Post Helpful? 0
  • +
  • -

#12 monckey100  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 27-November 09

Re: returning a character array

Posted 09 January 2010 - 01:37 AM

View Postsarmanu, on 9 Jan, 2010 - 12:20 AM, said:

char *loadfile(short choice,int temp)  


This function must return a char *. However, take a look at this line inside the function:
 int intelligence;


Take a look here too (this is inside the function too):
case 6:return intelligence; break;


This means that the function will return an integer (intelligence is an int). So, this is the problem. Maybe set the function to return an int?

yeah i figured that the returning of ints was glitching, thanks everyone![p.s. i know they die but i just wanted a more real time feeling which is why i didn't use a structure]

This post has been edited by monckey100: 09 January 2010 - 01:38 AM

Was This Post Helpful? 0
  • +
  • -

#13 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1102
  • View blog
  • Posts: 4,597
  • Joined: 09-June 09

Re: returning a character array

Posted 09 January 2010 - 01:39 AM

whats a real time feeling?
Was This Post Helpful? 0
  • +
  • -

#14 monckey100  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 27-November 09

Re: returning a character array

Posted 09 January 2010 - 01:45 AM

View PostImaSexy, on 9 Jan, 2010 - 12:39 AM, said:

whats a real time feeling?

like if they edit their health as soon as it pops up, it shows the new hp, or whenever i may need to have stats changed and quickly displayed
Was This Post Helpful? 0
  • +
  • -

#15 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1102
  • View blog
  • Posts: 4,597
  • Joined: 09-June 09

Re: returning a character array

Posted 09 January 2010 - 08:02 AM

and explain to me why you cant use a structure for this?
class player
{
public:
	 int Health;
	 int Experience;
	 char *digimonname;
	 char *username;
	 int intelligence;
	 int strength;
	 int luck;
	 int dexterity;


	 player(void)
	 {
		 digimonname = new char[50];
		 username = new char[50];
	 }

	 void edit_health(int &Health);

};

void player::edit_health(int &Health)
{
	this->Health = Health;
	cout<<this->Health<<endl;
}




Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2