Write a program that prints a sorted phone list from a database of names and pho
ID: 3634868 • Letter: W
Question
Write a program that prints a sorted phone list from a database of names and phonenumbers. The data is contained in two files named "phoneNames.txt" and
"phoneNums.txt". The files may contain up to 2000 names and phone numbers. The
files have one name or one phone number per line. To save paper, only print the first 50
lines of the output. Note: The first phone number in the phone number file corresponds to
the first name in the name file. The second phone number in the phone number file
corresponds to the second name in the name file. etc
this is what i have so far
int main()
{
using namespace std;
char num[2000][16];
char name[2000][26];
char temp[26];
const int size= 30;
int maxentries;
ifstream namestream;
ifstream numstream;
namestream.open("F:\phoneNames.txt");
if (namestream.fail())
{
cout<< "Input file opening failed.";
exit(1);
}
numstream.open("F:\phoneNums.txt");
if (numstream.fail())
{
cout<< "Input file opening failed.";
exit(1);
}
int j=0;
while (!namestream.eof())
{
namestream.getline (name[j],26) ;
if (namestream.eof())
break;
j++;
}
int i = 0;
while (!numstream.eof())
{
numstream.getline(num[i],16);
if (numstream.eof())
break;
i++;
}
for (int i = 0; i < maxentries; i++)
{
namestream.getline (name[i],size);
}
for (int i = 0; i < maxentries - 1; i++)
{
if (strcmp( name[i], name[i + 1]) > 0)
{
strcpy_s(temp, name[i]);
strcpy_s(name[i], name[i + 1]);
strcpy_s(name[i + 1], temp);
cout << "Here are the words in order." << endl;
for (int i = 0; i < maxentries; i++)
{
cout << name[i] << endl;
namestream.close();
numstream.close();
return 0;
}
Explanation / Answer
#include 02 using namespace std; 03 04 void getData(char names[][30], int& sizeOfArray); 05 void BubbleSort (char names[][30], int size); 06 void showArray(char names[][30], int size); 07 void strcpy(char x[], char y[]); 08 09 int main() 10 { 11 const char TOTALNAMES = 20; 12 char names[TOTALNAMES]; 13 int numberOfNames = 0; 14 15 getData(names, numberOfNames); 16 17 // Display the values 18 coutRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.