Boas galera, hoje trago para vocês o código-fonte de um programa de cadastro básico que desenvolvi. Suas funções incluem Cadastrar, Alterar o arquivo, Excluir algo do arquivo e visualizar o arquivo. Utilizei na criação do código-fonte do programa conhecimentos sobre vetores, operações com arquivos, strings, dentre outros. Vou postar o código. Qualquer dúvida, estou à disposição. O programa foi feito em C++.
EDIT: Estou postando uma versão atualizada do código, com funções para colorir separadas e versões em Inglês e Português.
//Coder: Pedro Milreu Cunha
// Tester: Pedro Milreu Cunha
// September, 2012.
// Free-license.
#include <iostream>
#include <fstream>
#include <limits>
#include <string>
#include <string.h>
#include <vector>
#include <locale>
#include <stdio.h>
#include <windows.h>
using namespace std;
#define OLD "Data.txt"
#define NEW "AlteredData.txt"
fstream Data (OLD, ios::app | ios::in);
fstream AlteredData (NEW, ios::app | ios::out | ios::in);
int NumerodePessoas;
string tempstringcadastro, stringcadastrodelete;
static string ColoredText;
struct Program
{
string tempnome;
string tempsexo;
string tempidade;
string tempstringCadaster;
string novonome;
string tempstring[10];
string tempstringdelete[10];
string stringcadastrodelete;
string stringcadastro;
};
void Register (int);
void Register2(int);
void Alter (string);
void Alter2(string);
void Delete(string);
void Delete2(string);
void Visualize();
void Visualize2();
void Menu();
void Menu2();
void LanguageMenu();
void Color(string);
void Open ();
void Close ();
void RenameAndDelete();
void Open ()
{
Data.open(OLD, ios::app | ios::in | ios::out);
AlteredData.open(NEW, ios::app | ios::in | ios::out);
}
void Close()
{
Data.close();
AlteredData.close();
}
void RenameAndDelete()
{
remove (OLD);
rename (NEW, OLD);
}
void Register (int NumerodePessoas) // Function used to cadaster people. Works fine.
{
Close();
Open();
string CadastroS;
struct Program RegisterObj[20];
ColoredText = "---------------REGISTER MENU----------------";
Color(ColoredText);
cout << "\n\n";
Open();
if (Data.is_open())
{
ColoredText = "File is open";
Color(ColoredText);
cout << "\n\n";
Data.seekg(0, ios::end);
if (Data.tellg() != 0)
{
Close();
Open();
cout << "-----------------------------------------------------\n\n";
while (getline(Data, CadastroS))
{
cout << CadastroS << "\n";
}
cout << "\n-----------------------------------------------------\n\n";
cout << "Type the number of people (Max 20): ";
cin.sync();
cin >> NumerodePessoas;
Close();
Open();
for (int i = 0; i < NumerodePessoas; i++)
{
cout << "Type the full name of the person: ";
cin.sync();
getline(cin, RegisterObj[i].tempnome);
Data << RegisterObj[i].tempnome << " ";
cout << "Type the gender of the person (Male/Female): ";
cin.sync();
getline(cin, RegisterObj[i].tempsexo);
Data << RegisterObj[i].tempsexo << " ";
cout << "Type the age of the person: ";
cin.sync();
getline(cin, RegisterObj[i].tempidade);
Data << RegisterObj[i].tempidade << endl;
cout << "\n\n";
}
cout << "\nProcessing...\n";
Close();
Open();
Data.seekg(0, ios::end);
if (Data.tellg() == 0)
{
Close();
cout << "File still empty!\n";
Menu();
}
else if (Data.tellg() != 0)
{
Close();
Open();
ColoredText = "Here is the contents of the file";
Color(ColoredText);
cout << "\n";
cout << "-----------------------------------------------------\n\n";
while (getline(Data, RegisterObj[0].stringcadastro))
{
cout << RegisterObj[0].stringcadastro << "\n";
}
Close();
cout << "\n\n";
cout << "-----------------------------------------------------\n\n";
Menu();
}
}
}
else if (Data.tellg() == 0)
{
Close();
ColoredText = "File is empty, time to register something";
Color(ColoredText);
cout << "\n\n";
Open();
cout << "Type the number of people (Max 20): ";
cin.sync();
cin >> NumerodePessoas;
for (int i = 0; i < NumerodePessoas; i++)
{
cout << "Type the full name of the person: ";
cin.sync();
getline(cin, RegisterObj[i].tempnome);
Data << RegisterObj[i].tempnome << " ";
cout << "Type the gender of the person (Male/Female): ";
cin.sync();
getline(cin, RegisterObj[i].tempsexo);
Data << RegisterObj[i].tempsexo << " ";
cout << "Type the age of the person: ";
cin.sync();
getline(cin, RegisterObj[i].tempidade);
Data << RegisterObj[i].tempidade << endl;
cout << "\n\n";
}
cout << "\nProcessing...\n";
Close();
Open();
Data.seekg(0, ios::end);
if (Data.tellg() == 0)
{
Close();
cout << "File still empty!\n";
Menu();
}
else if (Data.tellg() != 0)
{
Close();
Open();
ColoredText = "Here is the contents of the file";
Color(ColoredText);
cout << "\n";
cout << "-----------------------------------------------------\n\n";
while (getline(Data, RegisterObj[0].stringcadastro))
{
cout << RegisterObj[0].stringcadastro << "\n";
}
Close();
cout << "\n\n";
cout << "-----------------------------------------------------\n\n";
Menu();
}
}
else if (!Data.is_open())
{
cout << "File error!\n";
exit(0);
Close();
}
}
void Alter(string tempstringcadastro) // Function used to alter the cadastered data
{
struct Program AlterObj;
cout << "\n";
ColoredText = "----------------ALTER MENU------------";
Color(ColoredText);
cout << "\n\n";
string newfilestring, CadastroS;
Open();
if (Data.is_open() && AlteredData.is_open())
{
cout << "\n";
ColoredText = "Files opened";
Color(ColoredText);
cout << "\n\n";
}
else if (!Data.is_open() && !AlteredData.is_open())
{
cout << "File error!\n";
exit(0);
}
Data.seekg(0, ios::end);
if (Data.tellg() == 0)
{
ColoredText = "File is empty";
Color(ColoredText);
Close();
Menu();
}
if (Data.tellg() != 0)
{
Close();
Open();
cout << "\n";
cout << "-----------------------------------------------------\n\n";
while(getline(Data, CadastroS))
{
cout << CadastroS << "\n";
}
cout << "\n";
cout << "-----------------------------------------------------\n\n";
Close();
cout << "Type the data you want to change: ";
cin.sync();
getline (cin, AlterObj.stringcadastro);
cout << "Type to what you want it to change: ";
cin.sync();
getline(cin, AlterObj.novonome);
Open();
while (getline(Data, AlterObj.tempstring[0]))
{
if (AlterObj.stringcadastro == AlterObj.tempstring[0] )
{
AlteredData << AlterObj.novonome << "\n";
}
else if (AlterObj.stringcadastro != AlterObj.tempstring[0])
{
AlteredData << AlterObj.tempstring[0] << "\n";
}
}
Close();
cout << "\n\n";
cout << "Processing...\n";
Open();
Data.seekg(0, ios::end);
if (Data.tellg() == 0)
{
cout << "File still empty!\n";
RenameAndDelete();
Menu();
}
else if (Data.tellg() != 0)
{
Close();
ColoredText = "Here is the contents of the file";
Color(ColoredText);
cout << "\n";
cout << "\n\n";
cout << "-----------------------------------------------------\n\n";
Open();
while(getline (AlteredData, newfilestring))
{
cout << newfilestring << "\n";
}
Close();
RenameAndDelete();
cout << "\n\n";
cout << "-----------------------------------------------------\n\n";
}
Menu ();
}
}
void Delete (string stringcadastrodelete)
{
string CadastroSdelete, newfilestringdelete;
struct Program DeleteObj;
cout << "\n";
ColoredText = "----------------DELETE MENU------------";
Color(ColoredText);
cout << "\n\n";
Open();
if (Data.is_open() && AlteredData.is_open())
{
cout << "\n";
ColoredText = "Files opened";
Color(ColoredText);
cout << "\n\n";
}
else if (!Data.is_open() && !AlteredData.is_open())
{
cout << "File error!\n";
exit(0);
}
Data.seekg(0, ios::end);
if (Data.tellg() == 0)
{
ColoredText = "File is empty";
Color(ColoredText);
Close();
Menu();
}
else if (Data.tellg() != 0)
{
Close();
Open();
cout << "\n";
cout << "-----------------------------------------------------\n\n";
while(getline(Data, CadastroSdelete))
{
cout << CadastroSdelete << "\n";
}
cout << "\n";
cout << "-----------------------------------------------------\n\n";
Close();
cout << "Type the data you want to delete: ";
cin.sync();
getline (cin, DeleteObj.stringcadastrodelete);
Open();
Data.seekg(0, ios::beg);
while (getline(Data, DeleteObj.tempstringdelete[0]))
{
if (DeleteObj.stringcadastrodelete != DeleteObj.tempstringdelete[0])
{
AlteredData << DeleteObj.tempstringdelete[0] << "\n";
}
}
Close();
RenameAndDelete();
Open();
cout << "\n\n";
cout << "Processing...\n";
ColoredText = "Here is the contents of the file";
Color(ColoredText);
cout << "\n";
cout << "-----------------------------------------------------\n\n";
while(getline (Data, newfilestringdelete))
{
cout << newfilestringdelete << "\n";
}
Close();
cout << "\n\n";
cout << "-----------------------------------------------------\n\n";
}
Menu ();
}
void Visualize()
{
string file;
struct Program VisualizeObj;
cout << "\n";
ColoredText = "----------------VISUALIZE MENU------------";
Color(ColoredText);
cout << "\n\n";
Open();
if (Data.is_open())
{
cout << "\n";
ColoredText = "File is open\n";
Color(ColoredText);
}
else if (!Data.is_open())
{
cout << "File error!\n";
exit(0);
}
Data.seekg(0, ios::end);
if (Data.tellg() == 0)
{
ColoredText = "File is empty\n";
Color(ColoredText);
cout << "\n\n";
Close();
Menu();
}
else if (Data.tellg() != 0)
{
Close();
Open();
cout << "Processing...\n";
ColoredText = "Here is the contents of the file";
Color(ColoredText);
cout << "\n\n";
cout << "-----------------------------------------------------\n\n";
while (getline (Data, file))
{
cout << file << "\n";
}
cout << "\n\n";
cout << "-----------------------------------------------------\n\n";
Close();
}
Menu();
}
void Menu ()
{
int opcao;
ColoredText = "----------------MAIN MENU------------";
Color(ColoredText);
cout << "\n\n";
cout << "1 - Cadaster\n"
"2 - Alter\n"
"3 - Delete\n"
"4 - Visualize\n"
"5 - Change the language\n"
"6 - Exit\n"
<< endl;
cin >> opcao;
switch (opcao){
case 1:
{
Register(NumerodePessoas);
break;
}
case 2:
{
Alter(tempstringcadastro);
break;
}
case 3:
{
Delete(stringcadastrodelete);
break;
}
case 4:
{
Visualize();
break;
}
case 5:
{
LanguageMenu();
break;
}
case 6:
{
cout << "\n";
ColoredText = "Goodbye! Come again.\n";
Color(ColoredText);
exit(0);
break;
}
default:
{
cout << "\n";
ColoredText = "Goodbye! Come again.\n";
Color(ColoredText);
exit(0);
}
}
}
void Menu2()
{
int opcao;
ColoredText = "----------------MENU PRINCIPAL------------";
Color(ColoredText);
cout << "\n\n";
cout << "1 - Cadastrar\n"
"2 - Alterar\n"
"3 - Deletar\n"
"4 - Visualizar\n"
"5 - Mudar o idioma\n"
"6 - Sair\n"
<< endl;
cin >> opcao;
switch (opcao){
case 1:
{
Register2(NumerodePessoas);
break;
}
case 2:
{
Alter2(tempstringcadastro);
break;
}
case 3:
{
Delete2(stringcadastrodelete);
break;
}
case 4:
{
Visualize2();
break;
}
case 5:
{
LanguageMenu();
break;
}
case 6:
{
cout << "\n";
ColoredText = "Adeus! Volte sempre.\n";
Color(ColoredText);
exit(0);
break;
}
default:
{
cout << "\n";
ColoredText = "Adeus! Volte sempre.\n";
Color(ColoredText);
exit(0);
}
}
}
void Register2 (int NumerodePessoas) // Função usada para cadastrar pessoas.
{
string CadastroS;
struct Program RegisterObj[20];
ColoredText = "---------------MENU DE CADASTRO----------------";
Color(ColoredText);
cout << "\n\n";
Close();
Open();
Open();
if (Data.is_open())
{
Data.seekg(0, ios::end);
ColoredText = "Arquivo abriu";
Color(ColoredText);
cout << "\n\n";
if (Data.tellg() != 0)
{
Close();
Open();
cout << "-----------------------------------------------------\n\n";
while (getline(Data, CadastroS))
{
cout << CadastroS << "\n";
}
cout << "\n-----------------------------------------------------\n\n";
cout << "Digite o número de pessoas (Max 20): ";
cin.sync();
cin >> NumerodePessoas;
Close();
Open();
for (int i = 0; i < NumerodePessoas; i++)
{
cout << "Digite o nome completo da pessoa: ";
cin.sync();
getline(cin, RegisterObj[i].tempnome);
Data << RegisterObj[i].tempnome << " ";
cout << "Digite o sexo da pessoa (Male/Female): ";
cin.sync();
getline(cin, RegisterObj[i].tempsexo);
Data << RegisterObj[i].tempsexo << " ";
cout << "Digite a idade da pessoa: ";
cin.sync();
getline(cin, RegisterObj[i].tempidade);
Data << RegisterObj[i].tempidade << endl;
cout << "\n\n";
}
cout << "\nProcessando...\n";
Close();
Open();
Data.seekg(0, ios::end);
if (Data.tellg() == 0)
{
Close();
cout << "Arquivo está vazio!\n";
Menu2();
}
else if (Data.tellg() != 0)
{
Close();
Open();
ColoredText = "Aqui está o conteúdo do arquivo";
Color(ColoredText);
cout << "\n";
cout << "-----------------------------------------------------\n\n";
while (getline(Data, RegisterObj[0].stringcadastro))
{
cout << RegisterObj[0].stringcadastro << "\n";
}
Close();
cout << "\n\n";
cout << "-----------------------------------------------------\n\n";
Menu2();
}
}
}
else if (Data.tellg() == 0)
{
Close();
ColoredText = "Arquivo está vazio, hora de registrar alguma coisa, :D";
Color(ColoredText);
cout << "\n\n";
Open();
cout << "Digite o número de pessoas (Max 20): ";
cin.sync();
cin >> NumerodePessoas;
for (int i = 0; i < NumerodePessoas; i++)
{
cout << "Digite o nome completo da pessoa: ";
cin.sync();
getline(cin, RegisterObj[i].tempnome);
Data << RegisterObj[i].tempnome << " ";
cout << "Digite o sexo da pessoa (Masculino/Feminino): ";
cin.sync();
getline(cin, RegisterObj[i].tempsexo);
Data << RegisterObj[i].tempsexo << " ";
cout << "Digite a idade da pessoa: ";
cin.sync();
getline(cin, RegisterObj[i].tempidade);
Data << RegisterObj[i].tempidade << endl;
cout << "\n\n";
}
cout << "\nProcessando...\n";
Close();
Open();
Data.seekg(0, ios::end);
if (Data.tellg() == 0)
{
Close();
cout << "Arquivo ainda está vazio!\n";
Menu2();
}
else if (Data.tellg() != 0)
{
Close();
Open();
ColoredText = "Aqui está o conteúdo do arquivo.";
Color(ColoredText);
cout << "\n";
cout << "-----------------------------------------------------\n\n";
while (getline(Data, RegisterObj[0].stringcadastro))
{
cout << RegisterObj[0].stringcadastro << "\n";
}
Close();
cout << "\n\n";
cout << "-----------------------------------------------------\n\n";
Menu2();
}
}
else if (!Data.is_open())
{
cout << "Erro no arquivo!\n";
exit(0);
Close();
}
}
void Alter2(string tempstringcadastro) // Função usada para alterar cadastros
{
struct Program AlterObj;
cout << "\n";
ColoredText = "----------------MENU DE ALTERAÇÕES------------";
Color(ColoredText);
cout << "\n\n";
string newfilestring, CadastroS;
Open();
if (Data.is_open() && AlteredData.is_open())
{
cout << "\n";
ColoredText = "Arquivos abriram";
Color(ColoredText);
cout << "\n\n";
}
else if (!Data.is_open() && !AlteredData.is_open())
{
cout << "Erro no arquivo!\n";
exit(0);
}
Data.seekg(0, ios::end);
if (Data.tellg() == 0)
{
ColoredText = "Arquivo está vazio";
Color(ColoredText);
Close();
Menu2();
}
if (Data.tellg() != 0)
{
Close();
Open();
cout << "\n";
cout << "-----------------------------------------------------\n\n";
while(getline(Data, CadastroS))
{
cout << CadastroS << "\n";
}
cout << "\n";
cout << "-----------------------------------------------------\n\n";
Close();
cout << "Digite o que você deseja mudar: ";
cin.sync();
getline (cin, AlterObj.stringcadastro);
cout << "Digite para o quê deseja mudar: ";
cin.sync();
getline(cin, AlterObj.novonome);
Open();
while (getline(Data, AlterObj.tempstring[0]))
{
if (AlterObj.stringcadastro == AlterObj.tempstring[0] )
{
AlteredData << AlterObj.novonome << "\n";
}
else if (AlterObj.stringcadastro != AlterObj.tempstring[0])
{
AlteredData << AlterObj.tempstring[0] << "\n";
}
}
Close();
cout << "\n\n";
cout << "Processando...\n";
Open();
Data.seekg(0, ios::end);
if (Data.tellg() == 0)
{
cout << "Arquivo ainda está vazio!\n";
RenameAndDelete();
Menu2();
}
else if (Data.tellg() != 0)
{
Close();
Open();
ColoredText = "Aqui está o conteúdo do arquivo";
Color(ColoredText);
cout << "\n";
cout << "\n\n";
cout << "-----------------------------------------------------\n\n";
while(getline (AlteredData, newfilestring))
{
cout << newfilestring << "\n";
}
Close();
RenameAndDelete();
cout << "\n\n";
cout << "-----------------------------------------------------\n\n";
}
Menu2 ();
}
}
void Delete2 (string stringcadastrodelete) //Função usada para deletar cadastros.
{
string CadastroSdelete, newfilestringdelete;
struct Program DeleteObj;
cout << "\n";
ColoredText = "----------------MENU DE EXCLUSÃO------------";
Color(ColoredText);
cout << "\n\n";
Open();
if (Data.is_open() && AlteredData.is_open())
{
cout << "\n";
ColoredText = "Arquivos abriram";
Color(ColoredText);
cout << "\n\n";
}
else if (!Data.is_open() && !AlteredData.is_open())
{
cout << "Erro no arquivo!\n";
exit(0);
}
Data.seekg(0, ios::end);
if (Data.tellg() == 0)
{
ColoredText = "Arquivo está vazio";
Color(ColoredText);
Close();
Menu2();
}
else if (Data.tellg() != 0)
{
Close();
Open();
cout << "\n";
cout << "-----------------------------------------------------\n\n";
while(getline(Data, CadastroSdelete))
{
cout << CadastroSdelete << "\n";
}
cout << "\n";
cout << "-----------------------------------------------------\n\n";
Close();
cout << "Digite o que deseja deletar: ";
cin.sync();
getline (cin, DeleteObj.stringcadastrodelete);
Open();
Data.seekg(0, ios::beg);
while (getline(Data, DeleteObj.tempstringdelete[0]))
{
if (DeleteObj.stringcadastrodelete != DeleteObj.tempstringdelete[0])
{
AlteredData << DeleteObj.tempstringdelete[0] << "\n";
}
}
Close();
RenameAndDelete();
Open();
cout << "\n\n";
cout << "Processando...\n";
ColoredText = "Aqui está o conteúdo do arquivo.";
Color(ColoredText);
cout << "\n";
cout << "-----------------------------------------------------\n\n";
while(getline (Data, newfilestringdelete))
{
cout << newfilestringdelete << "\n";
}
Close();
cout << "\n\n";
cout << "-----------------------------------------------------\n\n";
}
Menu2 ();
}
void Visualize2() //Função usada para visualizar o conteúdo do arquivo.
{
string file;
struct Program VisualizeObj;
cout << "\n";
ColoredText = "----------------MENU DE VISUALIZAÇÃO------------";
Color(ColoredText);
cout << "\n\n";
Open();
if (Data.is_open())
{
cout << "\n";
ColoredText = "Arquivo abriu\n";
Color(ColoredText);
}
else if (!Data.is_open())
{
cout << "Erro no arquivo!\n";
exit(0);
}
Data.seekg(0, ios::end);
if (Data.tellg() == 0)
{
ColoredText = "Arquivo está vazio\n";
Color(ColoredText);
cout << "\n\n";
Close();
Menu2();
}
else if (Data.tellg() != 0)
{
Close();
Open();
cout << "Processando...\n";
ColoredText = "Aqui está o conteúdo do arquivo.";
Color(ColoredText);
cout << "\n\n";
cout << "-----------------------------------------------------\n\n";
while (getline (Data, file))
{
cout << file << "\n";
}
cout << "\n\n";
cout << "-----------------------------------------------------\n\n";
Close();
}
Menu2();
}
void Color (string ColoredText)
{
HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );
WORD wOldColorAttrs;
CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
/*
* First save the current color information
*/
GetConsoleScreenBufferInfo(h, &csbiInfo);
wOldColorAttrs = csbiInfo.wAttributes;
/*
* Set the new color information
*/
SetConsoleTextAttribute ( h, FOREGROUND_RED | FOREGROUND_INTENSITY );
cout << "\n";
cout << ColoredText;
SetConsoleTextAttribute ( h, wOldColorAttrs);
}
void LanguageMenu()
{
int opcaoidioma = 0;
cout << "\n";
cout << "What language do you wish to use?/ Qual idioma você deseja usar?\n" << endl;
cout << "1 - English\n"
"2 - Português\n" << endl;
cin >> opcaoidioma;
switch (opcaoidioma)
{
case 1:
{
Menu();
break;
}
case 2:
{
Menu2();
break;
}
default:
{
cout << "\n";
cout << "Invalid option/Opção inválida\n" << endl;
LanguageMenu();
break;
}
}
}
int main()
{
SetConsoleTitle("Register");
setlocale(LC_ALL, "Portuguese");
LanguageMenu();
}
OBS.: Caso não esteja usando Windows, desconsidere as funções para colorir o texto
HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );
WORD wOldColorAttrs;
CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
/*
* First save the current color information
*/
GetConsoleScreenBufferInfo(h, &csbiInfo);
wOldColorAttrs = csbiInfo.wAttributes;
/*
* Set the new color information
*/
SetConsoleTextAttribute ( h, FOREGROUND_RED | FOREGROUND_INTENSITY );
SetConsoleTextAttribute ( h, wOldColorAttrs);
Pergunta
PedroCunha
Boas galera, hoje trago para vocês o código-fonte de um programa de cadastro básico que desenvolvi. Suas funções incluem Cadastrar, Alterar o arquivo, Excluir algo do arquivo e visualizar o arquivo. Utilizei na criação do código-fonte do programa conhecimentos sobre vetores, operações com arquivos, strings, dentre outros. Vou postar o código. Qualquer dúvida, estou à disposição. O programa foi feito em C++.
EDIT: Estou postando uma versão atualizada do código, com funções para colorir separadas e versões em Inglês e Português.
//Coder: Pedro Milreu Cunha // Tester: Pedro Milreu Cunha // September, 2012. // Free-license. #include <iostream> #include <fstream> #include <limits> #include <string> #include <string.h> #include <vector> #include <locale> #include <stdio.h> #include <windows.h> using namespace std; #define OLD "Data.txt" #define NEW "AlteredData.txt" fstream Data (OLD, ios::app | ios::in); fstream AlteredData (NEW, ios::app | ios::out | ios::in); int NumerodePessoas; string tempstringcadastro, stringcadastrodelete; static string ColoredText; struct Program { string tempnome; string tempsexo; string tempidade; string tempstringCadaster; string novonome; string tempstring[10]; string tempstringdelete[10]; string stringcadastrodelete; string stringcadastro; }; void Register (int); void Register2(int); void Alter (string); void Alter2(string); void Delete(string); void Delete2(string); void Visualize(); void Visualize2(); void Menu(); void Menu2(); void LanguageMenu(); void Color(string); void Open (); void Close (); void RenameAndDelete(); void Open () { Data.open(OLD, ios::app | ios::in | ios::out); AlteredData.open(NEW, ios::app | ios::in | ios::out); } void Close() { Data.close(); AlteredData.close(); } void RenameAndDelete() { remove (OLD); rename (NEW, OLD); } void Register (int NumerodePessoas) // Function used to cadaster people. Works fine. { Close(); Open(); string CadastroS; struct Program RegisterObj[20]; ColoredText = "---------------REGISTER MENU----------------"; Color(ColoredText); cout << "\n\n"; Open(); if (Data.is_open()) { ColoredText = "File is open"; Color(ColoredText); cout << "\n\n"; Data.seekg(0, ios::end); if (Data.tellg() != 0) { Close(); Open(); cout << "-----------------------------------------------------\n\n"; while (getline(Data, CadastroS)) { cout << CadastroS << "\n"; } cout << "\n-----------------------------------------------------\n\n"; cout << "Type the number of people (Max 20): "; cin.sync(); cin >> NumerodePessoas; Close(); Open(); for (int i = 0; i < NumerodePessoas; i++) { cout << "Type the full name of the person: "; cin.sync(); getline(cin, RegisterObj[i].tempnome); Data << RegisterObj[i].tempnome << " "; cout << "Type the gender of the person (Male/Female): "; cin.sync(); getline(cin, RegisterObj[i].tempsexo); Data << RegisterObj[i].tempsexo << " "; cout << "Type the age of the person: "; cin.sync(); getline(cin, RegisterObj[i].tempidade); Data << RegisterObj[i].tempidade << endl; cout << "\n\n"; } cout << "\nProcessing...\n"; Close(); Open(); Data.seekg(0, ios::end); if (Data.tellg() == 0) { Close(); cout << "File still empty!\n"; Menu(); } else if (Data.tellg() != 0) { Close(); Open(); ColoredText = "Here is the contents of the file"; Color(ColoredText); cout << "\n"; cout << "-----------------------------------------------------\n\n"; while (getline(Data, RegisterObj[0].stringcadastro)) { cout << RegisterObj[0].stringcadastro << "\n"; } Close(); cout << "\n\n"; cout << "-----------------------------------------------------\n\n"; Menu(); } } } else if (Data.tellg() == 0) { Close(); ColoredText = "File is empty, time to register something"; Color(ColoredText); cout << "\n\n"; Open(); cout << "Type the number of people (Max 20): "; cin.sync(); cin >> NumerodePessoas; for (int i = 0; i < NumerodePessoas; i++) { cout << "Type the full name of the person: "; cin.sync(); getline(cin, RegisterObj[i].tempnome); Data << RegisterObj[i].tempnome << " "; cout << "Type the gender of the person (Male/Female): "; cin.sync(); getline(cin, RegisterObj[i].tempsexo); Data << RegisterObj[i].tempsexo << " "; cout << "Type the age of the person: "; cin.sync(); getline(cin, RegisterObj[i].tempidade); Data << RegisterObj[i].tempidade << endl; cout << "\n\n"; } cout << "\nProcessing...\n"; Close(); Open(); Data.seekg(0, ios::end); if (Data.tellg() == 0) { Close(); cout << "File still empty!\n"; Menu(); } else if (Data.tellg() != 0) { Close(); Open(); ColoredText = "Here is the contents of the file"; Color(ColoredText); cout << "\n"; cout << "-----------------------------------------------------\n\n"; while (getline(Data, RegisterObj[0].stringcadastro)) { cout << RegisterObj[0].stringcadastro << "\n"; } Close(); cout << "\n\n"; cout << "-----------------------------------------------------\n\n"; Menu(); } } else if (!Data.is_open()) { cout << "File error!\n"; exit(0); Close(); } } void Alter(string tempstringcadastro) // Function used to alter the cadastered data { struct Program AlterObj; cout << "\n"; ColoredText = "----------------ALTER MENU------------"; Color(ColoredText); cout << "\n\n"; string newfilestring, CadastroS; Open(); if (Data.is_open() && AlteredData.is_open()) { cout << "\n"; ColoredText = "Files opened"; Color(ColoredText); cout << "\n\n"; } else if (!Data.is_open() && !AlteredData.is_open()) { cout << "File error!\n"; exit(0); } Data.seekg(0, ios::end); if (Data.tellg() == 0) { ColoredText = "File is empty"; Color(ColoredText); Close(); Menu(); } if (Data.tellg() != 0) { Close(); Open(); cout << "\n"; cout << "-----------------------------------------------------\n\n"; while(getline(Data, CadastroS)) { cout << CadastroS << "\n"; } cout << "\n"; cout << "-----------------------------------------------------\n\n"; Close(); cout << "Type the data you want to change: "; cin.sync(); getline (cin, AlterObj.stringcadastro); cout << "Type to what you want it to change: "; cin.sync(); getline(cin, AlterObj.novonome); Open(); while (getline(Data, AlterObj.tempstring[0])) { if (AlterObj.stringcadastro == AlterObj.tempstring[0] ) { AlteredData << AlterObj.novonome << "\n"; } else if (AlterObj.stringcadastro != AlterObj.tempstring[0]) { AlteredData << AlterObj.tempstring[0] << "\n"; } } Close(); cout << "\n\n"; cout << "Processing...\n"; Open(); Data.seekg(0, ios::end); if (Data.tellg() == 0) { cout << "File still empty!\n"; RenameAndDelete(); Menu(); } else if (Data.tellg() != 0) { Close(); ColoredText = "Here is the contents of the file"; Color(ColoredText); cout << "\n"; cout << "\n\n"; cout << "-----------------------------------------------------\n\n"; Open(); while(getline (AlteredData, newfilestring)) { cout << newfilestring << "\n"; } Close(); RenameAndDelete(); cout << "\n\n"; cout << "-----------------------------------------------------\n\n"; } Menu (); } } void Delete (string stringcadastrodelete) { string CadastroSdelete, newfilestringdelete; struct Program DeleteObj; cout << "\n"; ColoredText = "----------------DELETE MENU------------"; Color(ColoredText); cout << "\n\n"; Open(); if (Data.is_open() && AlteredData.is_open()) { cout << "\n"; ColoredText = "Files opened"; Color(ColoredText); cout << "\n\n"; } else if (!Data.is_open() && !AlteredData.is_open()) { cout << "File error!\n"; exit(0); } Data.seekg(0, ios::end); if (Data.tellg() == 0) { ColoredText = "File is empty"; Color(ColoredText); Close(); Menu(); } else if (Data.tellg() != 0) { Close(); Open(); cout << "\n"; cout << "-----------------------------------------------------\n\n"; while(getline(Data, CadastroSdelete)) { cout << CadastroSdelete << "\n"; } cout << "\n"; cout << "-----------------------------------------------------\n\n"; Close(); cout << "Type the data you want to delete: "; cin.sync(); getline (cin, DeleteObj.stringcadastrodelete); Open(); Data.seekg(0, ios::beg); while (getline(Data, DeleteObj.tempstringdelete[0])) { if (DeleteObj.stringcadastrodelete != DeleteObj.tempstringdelete[0]) { AlteredData << DeleteObj.tempstringdelete[0] << "\n"; } } Close(); RenameAndDelete(); Open(); cout << "\n\n"; cout << "Processing...\n"; ColoredText = "Here is the contents of the file"; Color(ColoredText); cout << "\n"; cout << "-----------------------------------------------------\n\n"; while(getline (Data, newfilestringdelete)) { cout << newfilestringdelete << "\n"; } Close(); cout << "\n\n"; cout << "-----------------------------------------------------\n\n"; } Menu (); } void Visualize() { string file; struct Program VisualizeObj; cout << "\n"; ColoredText = "----------------VISUALIZE MENU------------"; Color(ColoredText); cout << "\n\n"; Open(); if (Data.is_open()) { cout << "\n"; ColoredText = "File is open\n"; Color(ColoredText); } else if (!Data.is_open()) { cout << "File error!\n"; exit(0); } Data.seekg(0, ios::end); if (Data.tellg() == 0) { ColoredText = "File is empty\n"; Color(ColoredText); cout << "\n\n"; Close(); Menu(); } else if (Data.tellg() != 0) { Close(); Open(); cout << "Processing...\n"; ColoredText = "Here is the contents of the file"; Color(ColoredText); cout << "\n\n"; cout << "-----------------------------------------------------\n\n"; while (getline (Data, file)) { cout << file << "\n"; } cout << "\n\n"; cout << "-----------------------------------------------------\n\n"; Close(); } Menu(); } void Menu () { int opcao; ColoredText = "----------------MAIN MENU------------"; Color(ColoredText); cout << "\n\n"; cout << "1 - Cadaster\n" "2 - Alter\n" "3 - Delete\n" "4 - Visualize\n" "5 - Change the language\n" "6 - Exit\n" << endl; cin >> opcao; switch (opcao){ case 1: { Register(NumerodePessoas); break; } case 2: { Alter(tempstringcadastro); break; } case 3: { Delete(stringcadastrodelete); break; } case 4: { Visualize(); break; } case 5: { LanguageMenu(); break; } case 6: { cout << "\n"; ColoredText = "Goodbye! Come again.\n"; Color(ColoredText); exit(0); break; } default: { cout << "\n"; ColoredText = "Goodbye! Come again.\n"; Color(ColoredText); exit(0); } } } void Menu2() { int opcao; ColoredText = "----------------MENU PRINCIPAL------------"; Color(ColoredText); cout << "\n\n"; cout << "1 - Cadastrar\n" "2 - Alterar\n" "3 - Deletar\n" "4 - Visualizar\n" "5 - Mudar o idioma\n" "6 - Sair\n" << endl; cin >> opcao; switch (opcao){ case 1: { Register2(NumerodePessoas); break; } case 2: { Alter2(tempstringcadastro); break; } case 3: { Delete2(stringcadastrodelete); break; } case 4: { Visualize2(); break; } case 5: { LanguageMenu(); break; } case 6: { cout << "\n"; ColoredText = "Adeus! Volte sempre.\n"; Color(ColoredText); exit(0); break; } default: { cout << "\n"; ColoredText = "Adeus! Volte sempre.\n"; Color(ColoredText); exit(0); } } } void Register2 (int NumerodePessoas) // Função usada para cadastrar pessoas. { string CadastroS; struct Program RegisterObj[20]; ColoredText = "---------------MENU DE CADASTRO----------------"; Color(ColoredText); cout << "\n\n"; Close(); Open(); Open(); if (Data.is_open()) { Data.seekg(0, ios::end); ColoredText = "Arquivo abriu"; Color(ColoredText); cout << "\n\n"; if (Data.tellg() != 0) { Close(); Open(); cout << "-----------------------------------------------------\n\n"; while (getline(Data, CadastroS)) { cout << CadastroS << "\n"; } cout << "\n-----------------------------------------------------\n\n"; cout << "Digite o número de pessoas (Max 20): "; cin.sync(); cin >> NumerodePessoas; Close(); Open(); for (int i = 0; i < NumerodePessoas; i++) { cout << "Digite o nome completo da pessoa: "; cin.sync(); getline(cin, RegisterObj[i].tempnome); Data << RegisterObj[i].tempnome << " "; cout << "Digite o sexo da pessoa (Male/Female): "; cin.sync(); getline(cin, RegisterObj[i].tempsexo); Data << RegisterObj[i].tempsexo << " "; cout << "Digite a idade da pessoa: "; cin.sync(); getline(cin, RegisterObj[i].tempidade); Data << RegisterObj[i].tempidade << endl; cout << "\n\n"; } cout << "\nProcessando...\n"; Close(); Open(); Data.seekg(0, ios::end); if (Data.tellg() == 0) { Close(); cout << "Arquivo está vazio!\n"; Menu2(); } else if (Data.tellg() != 0) { Close(); Open(); ColoredText = "Aqui está o conteúdo do arquivo"; Color(ColoredText); cout << "\n"; cout << "-----------------------------------------------------\n\n"; while (getline(Data, RegisterObj[0].stringcadastro)) { cout << RegisterObj[0].stringcadastro << "\n"; } Close(); cout << "\n\n"; cout << "-----------------------------------------------------\n\n"; Menu2(); } } } else if (Data.tellg() == 0) { Close(); ColoredText = "Arquivo está vazio, hora de registrar alguma coisa, :D"; Color(ColoredText); cout << "\n\n"; Open(); cout << "Digite o número de pessoas (Max 20): "; cin.sync(); cin >> NumerodePessoas; for (int i = 0; i < NumerodePessoas; i++) { cout << "Digite o nome completo da pessoa: "; cin.sync(); getline(cin, RegisterObj[i].tempnome); Data << RegisterObj[i].tempnome << " "; cout << "Digite o sexo da pessoa (Masculino/Feminino): "; cin.sync(); getline(cin, RegisterObj[i].tempsexo); Data << RegisterObj[i].tempsexo << " "; cout << "Digite a idade da pessoa: "; cin.sync(); getline(cin, RegisterObj[i].tempidade); Data << RegisterObj[i].tempidade << endl; cout << "\n\n"; } cout << "\nProcessando...\n"; Close(); Open(); Data.seekg(0, ios::end); if (Data.tellg() == 0) { Close(); cout << "Arquivo ainda está vazio!\n"; Menu2(); } else if (Data.tellg() != 0) { Close(); Open(); ColoredText = "Aqui está o conteúdo do arquivo."; Color(ColoredText); cout << "\n"; cout << "-----------------------------------------------------\n\n"; while (getline(Data, RegisterObj[0].stringcadastro)) { cout << RegisterObj[0].stringcadastro << "\n"; } Close(); cout << "\n\n"; cout << "-----------------------------------------------------\n\n"; Menu2(); } } else if (!Data.is_open()) { cout << "Erro no arquivo!\n"; exit(0); Close(); } } void Alter2(string tempstringcadastro) // Função usada para alterar cadastros { struct Program AlterObj; cout << "\n"; ColoredText = "----------------MENU DE ALTERAÇÕES------------"; Color(ColoredText); cout << "\n\n"; string newfilestring, CadastroS; Open(); if (Data.is_open() && AlteredData.is_open()) { cout << "\n"; ColoredText = "Arquivos abriram"; Color(ColoredText); cout << "\n\n"; } else if (!Data.is_open() && !AlteredData.is_open()) { cout << "Erro no arquivo!\n"; exit(0); } Data.seekg(0, ios::end); if (Data.tellg() == 0) { ColoredText = "Arquivo está vazio"; Color(ColoredText); Close(); Menu2(); } if (Data.tellg() != 0) { Close(); Open(); cout << "\n"; cout << "-----------------------------------------------------\n\n"; while(getline(Data, CadastroS)) { cout << CadastroS << "\n"; } cout << "\n"; cout << "-----------------------------------------------------\n\n"; Close(); cout << "Digite o que você deseja mudar: "; cin.sync(); getline (cin, AlterObj.stringcadastro); cout << "Digite para o quê deseja mudar: "; cin.sync(); getline(cin, AlterObj.novonome); Open(); while (getline(Data, AlterObj.tempstring[0])) { if (AlterObj.stringcadastro == AlterObj.tempstring[0] ) { AlteredData << AlterObj.novonome << "\n"; } else if (AlterObj.stringcadastro != AlterObj.tempstring[0]) { AlteredData << AlterObj.tempstring[0] << "\n"; } } Close(); cout << "\n\n"; cout << "Processando...\n"; Open(); Data.seekg(0, ios::end); if (Data.tellg() == 0) { cout << "Arquivo ainda está vazio!\n"; RenameAndDelete(); Menu2(); } else if (Data.tellg() != 0) { Close(); Open(); ColoredText = "Aqui está o conteúdo do arquivo"; Color(ColoredText); cout << "\n"; cout << "\n\n"; cout << "-----------------------------------------------------\n\n"; while(getline (AlteredData, newfilestring)) { cout << newfilestring << "\n"; } Close(); RenameAndDelete(); cout << "\n\n"; cout << "-----------------------------------------------------\n\n"; } Menu2 (); } } void Delete2 (string stringcadastrodelete) //Função usada para deletar cadastros. { string CadastroSdelete, newfilestringdelete; struct Program DeleteObj; cout << "\n"; ColoredText = "----------------MENU DE EXCLUSÃO------------"; Color(ColoredText); cout << "\n\n"; Open(); if (Data.is_open() && AlteredData.is_open()) { cout << "\n"; ColoredText = "Arquivos abriram"; Color(ColoredText); cout << "\n\n"; } else if (!Data.is_open() && !AlteredData.is_open()) { cout << "Erro no arquivo!\n"; exit(0); } Data.seekg(0, ios::end); if (Data.tellg() == 0) { ColoredText = "Arquivo está vazio"; Color(ColoredText); Close(); Menu2(); } else if (Data.tellg() != 0) { Close(); Open(); cout << "\n"; cout << "-----------------------------------------------------\n\n"; while(getline(Data, CadastroSdelete)) { cout << CadastroSdelete << "\n"; } cout << "\n"; cout << "-----------------------------------------------------\n\n"; Close(); cout << "Digite o que deseja deletar: "; cin.sync(); getline (cin, DeleteObj.stringcadastrodelete); Open(); Data.seekg(0, ios::beg); while (getline(Data, DeleteObj.tempstringdelete[0])) { if (DeleteObj.stringcadastrodelete != DeleteObj.tempstringdelete[0]) { AlteredData << DeleteObj.tempstringdelete[0] << "\n"; } } Close(); RenameAndDelete(); Open(); cout << "\n\n"; cout << "Processando...\n"; ColoredText = "Aqui está o conteúdo do arquivo."; Color(ColoredText); cout << "\n"; cout << "-----------------------------------------------------\n\n"; while(getline (Data, newfilestringdelete)) { cout << newfilestringdelete << "\n"; } Close(); cout << "\n\n"; cout << "-----------------------------------------------------\n\n"; } Menu2 (); } void Visualize2() //Função usada para visualizar o conteúdo do arquivo. { string file; struct Program VisualizeObj; cout << "\n"; ColoredText = "----------------MENU DE VISUALIZAÇÃO------------"; Color(ColoredText); cout << "\n\n"; Open(); if (Data.is_open()) { cout << "\n"; ColoredText = "Arquivo abriu\n"; Color(ColoredText); } else if (!Data.is_open()) { cout << "Erro no arquivo!\n"; exit(0); } Data.seekg(0, ios::end); if (Data.tellg() == 0) { ColoredText = "Arquivo está vazio\n"; Color(ColoredText); cout << "\n\n"; Close(); Menu2(); } else if (Data.tellg() != 0) { Close(); Open(); cout << "Processando...\n"; ColoredText = "Aqui está o conteúdo do arquivo."; Color(ColoredText); cout << "\n\n"; cout << "-----------------------------------------------------\n\n"; while (getline (Data, file)) { cout << file << "\n"; } cout << "\n\n"; cout << "-----------------------------------------------------\n\n"; Close(); } Menu2(); } void Color (string ColoredText) { HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE ); WORD wOldColorAttrs; CONSOLE_SCREEN_BUFFER_INFO csbiInfo; /* * First save the current color information */ GetConsoleScreenBufferInfo(h, &csbiInfo); wOldColorAttrs = csbiInfo.wAttributes; /* * Set the new color information */ SetConsoleTextAttribute ( h, FOREGROUND_RED | FOREGROUND_INTENSITY ); cout << "\n"; cout << ColoredText; SetConsoleTextAttribute ( h, wOldColorAttrs); } void LanguageMenu() { int opcaoidioma = 0; cout << "\n"; cout << "What language do you wish to use?/ Qual idioma você deseja usar?\n" << endl; cout << "1 - English\n" "2 - Português\n" << endl; cin >> opcaoidioma; switch (opcaoidioma) { case 1: { Menu(); break; } case 2: { Menu2(); break; } default: { cout << "\n"; cout << "Invalid option/Opção inválida\n" << endl; LanguageMenu(); break; } } } int main() { SetConsoleTitle("Register"); setlocale(LC_ALL, "Portuguese"); LanguageMenu(); }OBS.: Caso não esteja usando Windows, desconsidere as funções para colorir o textoque são da biblioteca Windows.h.
Att.,
Pedro.
Editado por PedroCunhaLink para o comentário
Compartilhar em outros sites
3 respostass a esta questão
Posts Recomendados
Participe da discussão
Você pode postar agora e se registrar depois. Se você já tem uma conta, acesse agora para postar com sua conta.