Created
September 1, 2018 07:13
-
-
Save arsalanses/458481555bbf201b7fe60fec05388ec2 to your computer and use it in GitHub Desktop.
simple address Phone in C++
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 <string> | |
using namespace std; | |
int counter = 0; | |
void insert(string names[], string phoneNumbers[], string name, string number) { | |
names[counter] = name; | |
phoneNumbers[counter] = number; | |
counter += 1; | |
} | |
void display(string names[], string phoneNumbers[]) { | |
if (counter == 0) { | |
cout << "Empty array!"; | |
return; | |
} | |
for (int i = 0; i < counter; i++) { | |
cout << i + 1 << "_ " << names[i] << ": " << phoneNumbers[i] << endl; | |
} | |
} | |
string search(string names[], string phoneNumbers[], string name) { | |
for (int i = 0; i < counter; i++) | |
if (names[i] == name) | |
return phoneNumbers[i]; | |
return "not found"; | |
} | |
void remove(string names[], string phoneNumbers[], string name) { | |
for (int i = 0; i < counter; i++) | |
if (names[i] == name) { | |
for (i; i < counter; i++) { | |
names[i] = names[i + 1]; | |
phoneNumbers[i] = phoneNumbers[i + 1]; | |
} | |
cout << name << " deleted\n"; | |
counter -= 1; | |
} | |
else { | |
cout << "name not found!\n"; | |
} | |
} | |
void main() { | |
string names[100]; // Local var worked! | |
string phoneNumbers[100]; | |
string name, number; | |
int choose; | |
while (true) { | |
system("cls"); | |
cout << "1. Insert \n" | |
<< "2. Dispaly \n" | |
<< "3. Search \n" | |
<< "4. Remove \n" | |
<< "5. Exit \n" | |
<< "=> "; | |
cin >> choose; | |
switch (choose) { | |
case 1: | |
system("cls"); | |
cout << "Enter name: "; | |
cin >> name; | |
cout << "Enter phoneNumber: "; | |
cin >> number; | |
insert(names, phoneNumbers, name, number); | |
cout << name << " added\n"; | |
system("pause"); | |
break; | |
case 2: | |
system("cls"); | |
display(names, phoneNumbers); | |
system("pause"); | |
break; | |
case 3: | |
system("cls"); | |
cout << "Search name: "; | |
cin >> name; | |
cout << search(names, phoneNumbers, name) << endl; | |
system("pause"); | |
break; | |
case 4: | |
system("cls"); | |
cout << "Remove name: "; | |
cin >> name; | |
remove(names, phoneNumbers, name); | |
system("pause"); | |
break; | |
case 5: | |
return; | |
default: | |
cout << "invalid input \n"; | |
system("pause"); | |
break; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment