需要直接掌握语言和文件处理方面的知识。因此,让我们讨论文件处理和循环中的C++语言概念。我们知道,对于代码的编译, 需要一个用于编译C++语言的IDE,例如代码块,Visual Studio代码,Dev C++等,才能运行我们的程序。
该软件旨在使用文件处理功能以C++的形式添加联系人簿。在C++中,文件主要通过使用fstream头文件中可用的三个类fstream,ifstream和ofstream来处理。
- ofstream:要写入文件的流类
- ifstream:流类以从文件读取
- fstream:流类以读取和写入文件。
执行:
联系人将被保存到文件中。联系人簿将为用户提供以下功能:
1. Add contact.
2. Search Contact.
3. Help.
4. Exit.
将保存的联系方式如下:
1. First name.
2. Last name.
3. Phone Number.
例子
C++
// Importing input output operations file
#include
// Importing file class
#include
// Importing standard library file
#include
using namespace std;
// Variables declared outside any function
// hence scope is global, hence global variables
string fname, lname, phone_num;
// Methods
// Helper Methods followed by
// Main driver method
void addContact();
void searchContact();
void help();
void self_exit();
bool check_digits(string);
bool check_numbers(string);
// Method 1
// Helper method
void self_exit()
{
system("cls");
cout << "\n\n\n\t\tThank You for using Contact-Saver!";
exit(1);
}
// Method 2
// Helper method
void help()
{
cout << "Help Center";
cout << endl << endl;
system("pause");
system("cls");
}
// Method 3
// Helper method
void addContact()
{
ofstream phone("number.txt", ios::app);
system("cls");
cout << "\n\n\tEnter First Name : ";
cin >> fname;
cout << "\n\tEnter Last Name : ";
cin >> lname;
cout << "\n\tEnter Phone Number : ";
cin >> phone_num;
if (check_digits(phone_num) == true) {
if (check_numbers(phone_num) == true) {
if (phone.is_open()) {
phone << fname << " " << lname << " "
<< phone_num << endl;
cout << "\n\tContact saved successfully !";
}
else {
cout << "\n\tError in opening record!";
}
}
else {
cout << "\n\tOnly numbers are allowed!";
}
}
else {
cout << "\n\tPhone number should be of 10 digits "
"only.";
}
cout << endl << endl;
system("pause");
system("cls");
phone.close();
}
// Method 4
// Helper method
void searchContact()
{
bool found = false;
ifstream myfile("number.txt");
string keyword;
cout << "\n\tEnter Name to search : ";
cin >> keyword;
while (myfile >> fname >> lname >> phone_num) {
if (keyword == fname || keyword == lname) {
system("cls");
cout << "\n\n\n\t\tCONTACT DETAILS";
cout << "\n\nFirst Name : " << fname;
cout << "\nLast Name : " << lname;
cout << "\nPhone Number : " << phone_num;
found = true;
break;
}
}
if (found == false)
cout << "\nNo such contact is found!";
cout << endl << endl;
system("pause");
system("cls");
}
// Method 5
// Helper method
bool check_digits(string x)
{
if (x.length() == 10)
return true;
else
return false;
}
// Method 6
// Helper method
bool check_numbers(string x)
{
bool check = true;
for (int i = 0; i < x.length(); i++) {
if (!(int(x[i]) >= 48 && int(x[i]) <= 57)) {
check = false;
break;
}
}
if (check == true)
return true;
if (check == false)
return false;
cout << endl << endl;
system("pause");
system("cls");
}
// Method 7
// Main driver method
int main()
{
int choice;
system("cls");
system("color 0A");
while (1) {
cout << "\n\n\n\t\t\tCONTACT SAVER";
cout << "\n\n\t1. Add Contact\n\t2. Search "
"Contact\n\t3. Help\n\t4. Exit\n\t> ";
cin >> choice;
// Switch case
switch (choice) {
case 1:
addContact();
break;
case 2:
searchContact();
break;
case 3:
help();
break;
case 4:
self_exit();
break;
default:
cout << "\n\n\tInvalid Input!";
}
}
return 0;
}
输出
想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。