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©)
{
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©);
~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