Skip to content

Instantly share code, notes, and snippets.

@pablocortez
Created May 1, 2017 18:08
Show Gist options
  • Save pablocortez/f5d373ec0d9f5e5fb594c541f5e583e0 to your computer and use it in GitHub Desktop.
Save pablocortez/f5d373ec0d9f5e5fb594c541f5e583e0 to your computer and use it in GitHub Desktop.
Telephone Records
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
string names[10];
long long numbers[10];
int namesLength = (sizeof(names) / sizeof(*names));
int numbersLength = (sizeof(numbers) / sizeof(*numbers));
int lastIndex = namesLength - 1;
int readNames()
{
ifstream File("TelephoneNames.txt");
int i = 0;
while(!File.eof())
{
File >> names[i]; // get data from file and populate onto array
i++;
}
File.close();
}
int readNumbers()
{
ifstream File("TelephoneNumbers.txt");
int i = 0;
while(!File.eof())
{
File >> numbers[i];
i++;
}
File.close();
}
int displayRecords()
{
cout << "\nPhone Numbers: " << endl;
for (int x = 0; x < namesLength; x++)
{
if(names[x] != "")
cout << names[x] << " ........ " << numbers[x] << endl;
}
}
int saveRecords()
{
ofstream File("TelephoneRecords.txt");
for (int i = 0; i < namesLength; i++)
{
if(names[i] != "")
File << names[i] << " =============== " << numbers[i] << endl;
}
cout << "\nPhone records have been saved to TelephoneRecords.txt" << endl;
}
int addRecords()
{
string name;
long long number;
cout << "Enter new name:\t";
cin >> name;
cout << "Enter new phone number:\t";
cin >> number;
for (int i = 0; i <= namesLength; i++)
{
if (names[i] == "")
{
names[i] = name;
numbers[i] = number;
break;
}
}
}
int main()
{
readNames();
readNumbers();
displayRecords();
string answer;
cout << "\nWould you like to add a new record? (y or n)" << endl;
cin >> answer;
if (answer == "y")
addRecords();
saveRecords();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment