Problem - Wskaźniki, Proste klasy

0

Cześć. Dopiero zacząłem naukę z klasami i mam problem z tym kodem. Kompilator wysypuje się przy wpisywaniu imienia, Gdzie może leżeć błąd ?
Program ma służyć do pobierania imienia nazwiska koloru skóry, grupy etnicznej -> przypisywaniu tego do tablicy i zapisu tej tablicy z poszczególnymi indeksami do pliku txt
Main:

#include "stdafx.h"
#include "human.h"

int main()
{
	human pearson1;
	human results[8];
	cout << " What is your name ? " << endl;
	pearson1.GetName();

	cout << " what is your second name ? " << endl;
	pearson1.GetSecName();

	cout << " what is your id  " << endl;
	pearson1.GetId();

	cout << " What is your skin color and ethnic group to which they belong ?" << endl;
	cout << "colour "; pearson1.GetSkinColour(); cout << endl;
	cout << " Ethnic group: "; pearson1.GetEtnicGroup(); cout << endl;

	char sign;
	cout << " do you want to save this ? Y-yes N-no " << endl;
	cin >> sign;
	if ((sign = 'Y') || (sign = 'y'))
	{
		pearson1.SaveToFile("file.txt");
		cout << " the file is saved ";
	}
	else
	{
		cout << " goodbye " << endl;
	}
    return 0;
}

metody:

#include "stdafx.h"
#include "human.h"


human::human()
{
	name = "";
	secondname = "";
	eg = "";
	skin = "";
	id=0;
        table = NULL;

}

human::human(const human&copy)
{
	name = copy.name;
	secondname = copy.secondname;
	eg = copy.eg;
	skin = copy.skin;
	id = copy.id;
	table = copy.table;

}


human::~human()
{
	delete [] name;
	delete [] secondname;
	delete [] eg;
	delete [] skin;
	delete [] id;
	delete [] table;
}

//void human::PrintToScr()
//{

//}

void human::GetName() 
{
	cin >> name;
	cout << endl;
	//cout << name; 
}
void human::GetSecName()
{
	cin >> secondname;
	cout << endl;
	//cout << secondname;
}
void human::GetSkinColour()
{
	cin >> skin;
	cout << endl;
	//cout << skin;
}
void human::GetEtnicGroup()
{
	cin >> eg;
	cout << endl;
	//cout << eg; 
}

void human::GetId()
{
	id = new int[11];
	
	for (int i = 0; i <= 11; i++)
	{
		cin >> id[i];
	}
	for (int i = 0; i <= 11; i++)
	{
		cout << " The id is" << id[i];
		if (id[10] % 2 == 0)
		{
			cout << "person is a woman" << endl;
		}
		else
		{
			cout << "person is a man" << endl;
		}
	}
}


void human::SaveToFile(const char*filename){
	fstream plik;
	plik.open(filename, ios::out);

	for (int i = 0; i < 9; i++)
	{
		plik << table[i];
	}

}
void human::SaveToTable(char *table)
{
	table = new char[4];
	table[0] = *name; table[1] = *secondname; table[2] = *id; table[3] = *skin; table[4] = *eg;	
}


nagłowek klasy

#include <fstream>
#include <iostream>
#ifndef human_h
#define human_h


using namespace std;
class human
{
public:
	human();
	human(const human&copy);
	~human();


	void GetName();
	void GetSecName();
	void GetId();
	void GetSkinColour();
	void GetEtnicGroup();
	void SaveToFile(const char*filename);
	void SaveToTable(char *table);

private:
	char * name;
	char * secondname;
	int  * id;
	char * skin;
	char * eg;
	char * table;

};


#endif
1

Nigdy nie inicjujesz prawidłowo pól char*, przez co wychodzisz poza przydzieloną im pamięć - wykorzystaj std::string.

Btw, klasa nie powinna sama z siebie wczytywać danych - akcesory poprawnie tworzy się tak, że getXyz zwraca wartość pola xyz, a setXyz ustawia wartość pola na tę z parametru (ale nie wczytuje żadnej wartości sama z siebie), czyli:

std::string MyClass::getName() {
  return this->name;
}

void MyClass::setName(std::string name) {
  this->name = name;
}

/* ... */
int main() {
  MyClass mc;

  std::string name;
  std::cin >> name;

  mc.setName(name);
}
0

@Patryk27: Dziękuje ci za twoją pomoc. Skorzystałem z rady i zamieniłem char na string, doszedłem do momentu gdy trzeba wprowadzić kilka pól do jednego pliku i debuger przerywa pracę na w destruktorze. Zapewne popełniłem jeszcze kilka błędów. Gdzie teraz moze tkwić błąd ?
nagłówki:

#include <fstream>
#include <iostream>
#include <string>

#ifndef human_h
#define human_h


using namespace std;
class human
{
public:
	human();
	human(string name, string secondname, string skin, string eg,int id);
	human(const human&copy);
	~human();

	string GetName(string name);
	string GetSecName(string secondname);
	string Skin(string skin);
	string EthnicG(string eg);
	void GetId();
	void SaveToFile(const char*filename);
	void SaveToTable(char *table);
	string name, secondname, skin, eg;
	

private:
	void CreateId();
	int * id;

};


#endif

metody:

#include "stdafx.h"
#include "human.h"
#include <string>

using namespace std;

human::human()
{	
	id = NULL;
}
human::human(string name, string secondname, string skin, string eg,int id)
{
	this->name = name;
	this->secondname = secondname;
	this->skin = skin;
	this->eg = eg;
	GetId();
}

human::human(const human&copy)
{
	name = copy.name;
	secondname = copy.secondname;
	eg = copy.eg;
	skin = copy.skin;
	for (int i = 0; i<11; i++) {
		id= copy.id;
	}
}



human::~human()
{
	delete []id;

}


string human::GetName(string name)
{
	this->name = name;
	return name;	
}
string human::GetSecName(string name)
{
	this->secondname = secondname;
	return secondname;
}
string human::Skin(string name)
{
	this->skin = skin;
	return skin;
}
string human::EthnicG(string name)
{
	this->eg = eg;
	return eg;
}

void human::GetId()
{
	id = new int[10];
	
	for (int i = 0; i <= 10; i++)
	{
		cout << " Enter " << i + 1 << " element: ";
		cin >> id[i];
	}
	cout << "your id is: "; 
	for (int i = 0; i <= 10;i++)
	{
		cout <<(int)id[i];
	}

	if (id[10] % 2 == 0)
	{
		cout << " the pearson is a woman" << endl;
	}
	else
	{
		cout << " the pearson is a man" << endl;
	}

}


void human::SaveToFile(const char*filename){
	ofstream file;
	file.open(filename, ios::out);
	/*
	for (int i = 0; i < 9; i++)
	{
		plik << table[i];
	}*/
	 file << name;
	 file << secondname;
	 file << skin;
	 file << eg;
	 
	for (int i = 0; i<10; i++) {
		
		file << id[i];

	}
	file.close();

}

main:

// ConsoleApplication16.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "human.h"
#include <iostream>

using namespace std;

int main()
{
	human pearson1;
	human results[8];
	
	string name, secondname, skin, eg;
	cout << "what is your name ?" << endl;
	getline(cin, name);
	pearson1.GetName(name);
	cout << "whati is your second name ? " << endl;
	getline(cin, secondname);
	pearson1.GetSecName(secondname);
	cout << " what is your skin colour ? " << endl;
	getline(cin, skin);
	pearson1.Skin(skin);
	cout << " what is your ethnic group ? " << endl;
	getline(cin, eg);
	pearson1.EthnicG(eg);
	cout << " insert your ID:11 characters " << endl;
	pearson1.GetId();
	
	
	char sign;
	cout << " do you want to save this ? Y-yes N-no " << endl;
	cin >> sign;
	if ((sign = 'Y') || (sign = 'y'))
	{
		pearson1.SaveToFile("file.txt");
		cout << " the file is saved ";
	}
	else
	{
		cout << " goodbye " << endl;
	}
	
	system("pause");
    return 0;
}

1 użytkowników online, w tym zalogowanych: 0, gości: 1