Created
May 1, 2017 18:08
-
-
Save pablocortez/f5d373ec0d9f5e5fb594c541f5e583e0 to your computer and use it in GitHub Desktop.
Telephone Records
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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