I'm new in c++ programming.

I have to do a program to save data from classes (Student, Teacher and Statistics).

What may i use to save that values? (File, sql...) and how to use.
If some one show me a litle program....:icon_smile:

Just a simple text file will do -- I doubt your instructor wants something as complex as an SQL database.. Use ofstream to output the data and ifstream to read it back. Both are declared in the header file <fstream>.

If you don't yet know how to use those two c++ classes then I'd suggest you read your textbook because it most certainly covers that topic. Also look in the Code Snippets board (link at the top of every DaniWeb page).

thanks.

I will read the textbook.

thanks.

I will read the textbook.

Oh what a great idea! :icon_idea: Someone going to actually read his textbook :)

Hi,
I am Rammohan from Bangalore and working as a Technical lead in big IT firm .
Solution for your answer is follows:

Based on my experinace this small problem you can use it normal ASCII file(either plain text file or xml file). Or else if you have a knowledge in database and if your application wants the backend and big style then you go for the SQL.
Depends on your requirement. You can use either one is solve your problem.

Wish You Good luck...
<snip false signature>

This is a litle bit of my program and i have some problems. Pessoa = Person BI=Identification Number
MostrarPessoa = Show Person
If some one help me to solve this problem...I still don't know how to save data.

#include <iostream>
#include <iomanip>
#include <string>
#include <ctype.h>

using namespace std;

//DEFINICAO DA SUPERCLASSE PESSOA
class Pessoa{
private:
	char *nome;
	char *bi;
public:
	Pessoa (char* nome, char *bi);
	~Pessoa();
	const char* carregaNome() const;
	const char* carregaBi()const;
	void lerDadoPessoa( char *nome, char *bi);
	
};
//fim da definicao da classe

Pessoa::Pessoa (char * nome_p, char *n_bi)
{
	bi = new char[strlen(n_bi)+1];//criar espaço para o nome
	nome = new char[strlen(nome_p)+1];//criar espaço 
	nome=nome_p;
	bi=n_bi;
	
}
Pessoa::~Pessoa(){
	cout << "\nPessoa " << bi<< "encerrada.";
	delete [] nome;
}

inline const char* Pessoa::carregaNome() const{
	return nome;
}

inline const char* Pessoa::carregaBi() const{
	return bi;
}

void MostraPessoa(Pessoa &);//função prototipo
Pessoa * criaPessoa();

void MostraPessoa(Pessoa &pess)
{
	cout << "\n\nNome da Pessoa: " << pess.carregaNome();
	cout << "\n\nNumero de Bilhete de Identidade: " << pess.carregaBi();
}

int main()
{
	cout<< setprecision(2)
		 << setiosflags(ios::fixed)
		 << setiosflags(ios::showpoint);
	Pessoa* pess_tab[10];

	int count=0, i;
	char resposta;

	cout <<"\n\nDeseja inserir uma pessoa? (S/N): ";
	resposta=cin.get();
	cin.get();

	while (toupper(resposta)=='S' && count < 10 )
	{
		pess_tab[count]=criaPessoa();
		++count;
		cout <<"\n\nDesja cria mais uma Pessoa? (S/N): ";
		resposta=cin.get();
		cin.get();
	}

	for (int i=0; i<count; ++i)
		MostraPessoa(*pess_tab[i]);

	/*for (i=0;i<count;++i)
		delete pess_tab[i];
	*/
	return 0;
}

Pessoa * criaPessoa()
{
	char buffer[81];
	char id[10];
	Pessoa* p_ptr;

	cout << "\n\n Insira o Nome: ";
	cin.getline (buffer, 81);

	cout<<"\n\n Insira o Nu'mero de BI: ";
	cin.getline (id, 10);

	cin.get();//limpa o buffer de input de dados

	p_ptr = new Pessoa(buffer, id);

	return p_ptr;
}

You can save the entire structure as binary, heres an exmaple of how to:

#include <iostream>
#include <fstream>
using namespace std;

template<typename type>
void SaveStruct(ofstream &out, type &obj) {
   out.write(reinterpret_cast<char*>( &obj ), sizeof type);
}

template<typename type>
void LoadStruct(ifstream &in, type &targetObj) {
   in.read(reinterpret_cast<char*>( &targetObj ), sizeof type);
}

struct A {
   char str[20];
   size_t str_len;
};

int main() {

   // Make Structure
   A a;
   strcpy_s(a.str, 20, "Hello World");
   a.str_len = strlen(a.str);

   // Save structure to file using SaveStruct(...)
   ofstream out("savefile.txt", ios::out | ios::binary);
   SaveStruct(out, a);
   out.close();

   // Load structure from file into the variable b
   A b;
   ifstream in("savefile.txt", ios::in | ios::binary);
   LoadStruct(in, b);

   // Display struct b
   cout << "str: " << b.str << "\nstr_len: " << b.str_len;
   
   cin.ignore();
   return 0;
}

Regrettably, sonicmas can't follow williamhemsworth's advice without serious troubles: it's a big mistake to save Pessoa class objects as binaries with proposed SaveStruct template!

What can i do to save it? I can't find nothing that help me saving that class files. Exist a lot of information explain how to save data using structs but nothing about class. It's something whith me?

I clean the code but i still can't save data.

#include <iostream>
#include <iomanip>
#include <string>
#include <ctype.h>

using namespace std;

class Aluno
{
  protected:
	 char   id_no[5];
	 char * nome;
	 static int numero_Alunos;

  public:
	 Aluno(char id[], char* n_p);
	 ~Aluno();
	 const char * Get_Id() const;
	 const char * Get_Nome() const;
	 static int Total_Alunos();
};


//Definição Static
int Aluno::numero_Alunos = 0;


Aluno::Aluno(char id[], char* n_p)
{
  strcpy(id_no, id);  //Copia o primeiro argumento para id_no[]

  nome = new char[strlen(n_p) + 1]; //Cria espaço para o nome

  strcpy(nome, n_p);  //copia o segundo argumento

  ++numero_Alunos;
}

Aluno::~Aluno()
{
  cout << "\n\nClass Aluno " << id_no << " encerrada.";
  delete [] nome;

  --numero_Alunos;

  cout << "\nNumero de Alunos inseridos: " << numero_Alunos;
}

inline const char * Aluno::Get_Id() const
{
  return id_no;
}

inline const char * Aluno::Get_Nome() const
{
  return nome;
}


int Aluno::Total_Alunos()
{
  return numero_Alunos;
}


void Display_Aluno(Aluno &);    //Funcão prototipo
Aluno * Cria_Aluno();

int main()
{
  cout << setprecision(2)
		 << setiosflags(ios::fixed)
		 << setiosflags(ios::showpoint);

  Aluno * tab_aluno[10];

  int count = 0;
  char resposta;

  cout << "\nDeseja criar um Aluno?(S/N): ";
  resposta = cin.get();
  cin.get();		//Limpa o buffer de entrada

  while (toupper(resposta) == 'S' && count < 10)
	 {
		tab_aluno[count] = Cria_Aluno();

		++count;

		cout << "\nDo you want to Cria an Aluno?(S/N): ";
		resposta = cin.get();
		cin.get();		//Limpa o buffer de entrada
	 }

  //Mostra Alunos

  for (int i = 0; i < count; ++i)
	 Display_Aluno(*tab_aluno[i]);

  //Limpa

  for (int i = 0; i < count; ++i)
	 delete tab_aluno[i];

  cout << endl;
  return 0;
}

void Display_Aluno(Aluno& novoAluno)
{
  cout << "\n\n\nData for Aluno# " << novoAluno.Get_Id();

  cout << "\n\nOwner's Name: " << novoAluno.Get_Nome();

}

Aluno * Cria_Aluno()
{
  char id[5];
  char buffer[81];

  Aluno * ptr_aluno;

  cout << "\nEnter Aluno ID: ";
  cin.getline(id, 5);

  cin.get();    //Limpa o buffer de entrada

  cout << "\nEnter Aluno Holder's Name: ";
  cin.getline(buffer, 81);

  cin.get();    //Limpa o buffer de entrada

  ptr_aluno = new Aluno (id, buffer);

  cout << "\n\nNumero de alunos existente: "
		 << Aluno::Total_Alunos() << endl;

  return ptr_aluno;
}

Don't worry, it's not so hard. Look at, for example:
http://www.parashift.com/c++-faq-lite/serialization.html
http://www.functionx.com/cpp/articles/serialization.htm
Better think about your class design defects:
1. You have char id_no[5] member then call strcpy(id_no, id) in the constructor. What happens if strlen(id) > 4 ? Right, you overwrite your memory and get a crash or bad data or undefined behaviour (or all three trubles together).
2. You allocate a memory for name by hands (without check up if n_p parameter is 0) then use strcpy to fill it. Why? You include <string> header in the program - use it! Try to declare all text variables in your program as std::string (it's possible and comfort solution in that case).
3. Make obvious "getter" member functions inline (declare them in the class declaration body):

class Aluno
{
  protected:
	 std::string id_no; //char   id_no[5];
	 std::string nome; //char * nome;
	 static int numero_Alunos;

  public:
          // add const modifier:
	 Aluno(const char id[], const char* n_p);
          // add for your convinience:
                 Aluno(const std::string& id, const std::string& nom):
                        id_no(id), nome(nom)
                 {}
	 ~Aluno(); // may be trivial in that case.
	 const char * Get_Id() const { return id_no.c_str(); }
	 const char * Get_Nome() const 
                 {
                       return nome.c_str(); 
                 }
	 static int Total_Alunos()
                 {
                       return Aluno::numero_Alunas;
                 }
};

Thank You. Is real simple, but my only library is " C++ for Business Programming"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.