File .txt with structs
Nov 3, 2014 at 10:44pm UTC
Hello!!
I want to know how can I get the information of each contact, I add a contact and save in a file.txt but I don't know how to get the information of contacts when I open my program and select option 2 (show contact).
I do not using Vector or classes or pointers. Thanks a lot.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
#include <iostream>
#include <fstream> // read and write outfile .txt
#include <cstring>
#include <cstdlib> // To use system("clear")
using std::ofstream; // write outfile .txt
using std::ifstream; // read outfile .txt
using std::ios;
using std::cout;
using std::cin;
using std::endl;
using std::string;
const int LEN = 2;
void Addd(struct Data people[], int reg); // add_contact_funtion
struct Data
{
string Naame;
unsigned int age;
};
int main()
{
Data people[LEN]; //struct
cout << endl;
cout << "\t //////// //////// // // ///////// ////////// //////// " << endl;
cout << "\t // // // //// // // // // // " << endl;
cout << "\t // // // // // // // // // // " << endl;
cout << "\t // // // // // // // ////////// // " << endl;
cout << "\t // // // // //// // // // // " << endl;
cout << "\t //////// //////// // // // // // //////// " << endl;
cout << endl;
cout << "CONTACTS" << endl << endl;
cout << "1. Add contact." << endl << endl;
cout << "2. Show contact." << endl << endl;
cout << "3. Exit" << endl << endl;
int regi_ter = 0;
int option;
cout << "CHOOSE AN OPTION: " ;
cin >> option;
cin.ignore();
cout << endl;
switch (option)
{
case 1:
system("clear" );
Addd(people,regi_ter);
break ;
case 2:
cout << "Show contact" ;
break ;
case 3:
cout << "EXIT" ;
exit(0);
break ;
default :
cout << "Operation invalid." ;
system("clear" );
main();
break ;
}
return 0;
}
/// ADD_CONTACT_FUNTION ///
void Addd(struct Data people[], int reg)
{
cout << "Write the contact information." ;
cout << endl << endl;
cout << "1. Name: " ;
getline(cin,people[reg].Naame);
cout << endl;
cout << "6. Age: " ;
cin >> people[reg].age;
cin.ignore();
cout << endl;
char sn[5];
do
{
cout << "Add contact in the agenda? [S/N]: " ;
cin >> sn;
if ((strcmp(sn,"s" )==0)||(strcmp(sn,"S" )==0))
{
ofstream archivo;
archivo.open("ContactsAgenda.txt" , ios::app);
archivo << "Name: " << people[reg].Naame << endl;
archivo << "Age: " << people[reg].age << endl;
archivo << endl << "---------------------------------------------" << endl << endl;
archivo.close();
reg++;
cout << endl << "Contacto anadido." << endl;
system("clear" );
main();
}
if ((strcmp(sn,"n" )==0)||(strcmp(sn,"N" )==0))
{
cout << endl << "Contact added." << endl;
system("clear" );
main();
}
}while ((sn!="S" )||(sn!="s" )||(sn!="N" )||(sn!="n" ));
}
Topic archived. No new replies allowed.