Question

In C++, Write a program that retrieves a phone number from a database of names and...

In C++,

Write a program that retrieves a phone number from a database of names and phone numbers. The program should ask the user to enter a name and then print the phone number of that person (or vice versa, number into name) The program should allow the user to repeat the operation as often as they would like to. The data is contained in two text files named "phoneNames.txt" and "phoneNums.txt". The files have one name or one phone number per line.

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.

In depth Comments in code would be greatly appreciated to help follow along

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream>
#include <fstream>
#include <string>
#include<bits/stdc++.h>
using namespace std;

int main()
{
   int ch;
   cout<<"1.enter name and retrieve phone number\n2.enter number and retrieve name\nenter your choice:\t";
   cin>>ch;
   ifstream pnumber("phoneNums.txt");
   ifstream pname("phoneNames.txt");
   string temp;
   getline(cin, temp);
   int flag = 0;
   if(ch == 1)
   {
       string name;
       cout<<"enter name:\t";
       getline(cin, name);
       transform(name.begin(), name.end(), name.begin(), ::tolower);
       string line1;
       int count1 = 0;
       while(getline(pname,line1))
       {
           count1++;
           transform(line1.begin(), line1.end(), line1.begin(), ::tolower);
           if(name == line1)
           {
               flag = 1;
               break;
           }
       }
       if(flag == 1)
       {
           string line2;
           int count2 = 0;
           while(getline(pnumber,line2))
           {
               count2++;
               if(count1 == count2)
               {
                   cout<<name<<"\t"<<line2<<endl;
                   break;
               }
           }
       }
       else
       {
           cout<<"Phone number doesn't exist"<<endl;
       }
  
   }
   else if(ch == 2)
   {
       string phonenumber;
       cout<<"enter phone number:\t";
       getline(cin, phonenumber);
       string line1;
       int count1 = 0;
       int flag = 0;
       while(getline(pnumber,line1))
       {
           count1++;
           if(phonenumber == line1)
           {
               flag = 1;
               break;
           }
       }
       if(flag == 1)
       {
           string line2;
           int count2 = 0;
           while(getline(pname,line2))
           {
               count2++;
               if(count1 == count2)
               {
                   cout<<line2<<"\t"<<phonenumber<<endl;
                   break;
               }
           }
       }
       else
       {
           cout<<"Phone number doesn't exist"<<endl;
       }
   }
}

If you have any doubts please comment and please don't dislike.

Z Text Editor - Sat 17:45 Activities phoneNames.txt -/Desktop Open Save phoneNames.txt phonenumber.cpp student1.cpp phoneNums.txt name1 name2 name3 name4 Plain Text - Ln 4, Col 6 Tab Width: 8 - INS

Z Text Editor - Sat 17:45 Activities phoneNums.txt -/Desktop Open Save phoneNums.txt phoneNames.txt phonenumber.cpp student1.cpp 8500802007 9640558993 8096136398 9908210455 Plain Text - Tab Width: 8 - Ln 4, Col 11 INS

A Terminal - Activities Sat 17:45 deepika@deepika-TravelMate-P243-M: ~/Desktop File Edit View Search Terminal Help deepika@deepika-TravelMate-P243-M:~/Desktop$ ./a.out 1.enter name and retrieve phone number 2.enter number and retrieve name enter your choice: enter name: name1 name1 8500802007 deepika@deepika-TravelMate-P243-M:~/Desktop$ g++ phonenumber.cpp deepika@deepika-TravelMate-P243-M:~/Desktop$ ./a.out 1.enter name and retrieve phone number 2. enter number and retrieve name enter your choice: enter phone number: 2 9640558993 name2 9640558993 deepika@deepika-TravelMate-P243-M:~/Desktop$ ]

Add a comment
Know the answer?
Add Answer to:
In C++, Write a program that retrieves a phone number from a database of names and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In C++, Write a program that retrieves a phone number from a txt files of names...

    In C++, Write a program that retrieves a phone number from a txt files of names and phone numbers. The program should ask the user to enter a name and then print the phone number of that person (or vice versa, number into name) The program should allow the user to repeat the operation as often as they would like to. The data is contained in two text files named "phoneNames.txt" and "phoneNums.txt". The files have one name or one...

  • Write a contacts database program that presents the user with a menu that allows the user...

    Write a contacts database program that presents the user with a menu that allows the user to select between the following options: (In Java) Save a contact. Search for a contact. Print all contacts out to the screen. Quit If the user selects the first option, the user is prompted to enter a person's name and phone number which will get saved at the end of a file named contacts.txt. If the user selects the second option, the program prompts...

  • Write a simple telephone directory program in C++ that looks up phone numbers in a file...

    Write a simple telephone directory program in C++ that looks up phone numbers in a file containing a list of names and phone numbers. The user should be prompted to enter a first name and last name, and the program then outputs the corresponding number, or indicates that the name isn't in the directory. After each lookup, the program should ask the user whether they want to look up another number, and then either repeat the process or exit the...

  • Write a python program that prompts the user for the names of two text files and...

    Write a python program that prompts the user for the names of two text files and compare the contents of the two files to see if they are the same. If they are, the scripts should simply output “Yes”. If they are not, the program should output “No”, followed by the first lines of each file that differ from each other. The input loop should read and compare lines from each file. The loop should break as soon as a...

  • Can someone help me with a c++ program Create a program that displays a measurement in...

    Can someone help me with a c++ program Create a program that displays a measurement in either inches or centimeters. If necessary, create a new project named Introductory 18 Project, and save it in the Cpp8\Chap09 folder. The program should allow the user the choice of converting a measurement from inches to centimeters or vice versa. Use two program-defined functions: one for each different conversion type. Enter your C++ instructions into a source file named Introductory 18.cpp Also enter appropriate...

  • Exercise 3) Creating a QT program to search for Customer Phone Number : Create a text...

    Exercise 3) Creating a QT program to search for Customer Phone Number : Create a text file (customer.txt) and put names and phone numbers into it. each name has to have its own phone number right under it. In the QT window, the user has to enter a name and the program prints the phone number related to it. NB: If the user input a name that doesn't exist, the program should print an error message. Text file example: QT...

  • Please write this in C. Write this code in Visual Studio and upload your Source.cpp file for checking (1) Write a program to prompt the user for an output file name and 2 input file names. The progra...

    Please write this in C. Write this code in Visual Studio and upload your Source.cpp file for checking (1) Write a program to prompt the user for an output file name and 2 input file names. The program should check for errors in opening the files, and print the name of any file which has an error, and exit if an error occurs opening any of the 3 For example, (user input shown in caps in first line) Enter first...

  • In Java. Write a GUI contact list application. The program should allow you to input names...

    In Java. Write a GUI contact list application. The program should allow you to input names and phone numbers. You should also be able to input a name and have it display the previously entered phone number. The GUI should look something like the following, although you are welcome to format it in any way that works. This should be a GUI application with a JFrame. The program should contain two arrays of Strings. One array will contain a list...

  • C++ programming Phone Book Create a class that can be used for a Phone Book. The...

    C++ programming Phone Book Create a class that can be used for a Phone Book. The class should have attributes for the name and phone number. The constructor should accept a name and a phone number. You should have methods that allow the name to be retrieved, the phone number to be retrieved, and one to allow the phone number to be changed. Create a toString() method to allow the name and number to be printed. Write a program that...

  • Write a program **(IN C)** that displays all the phone numbers in a file that match the area code...

    Write a program **(IN C)** that displays all the phone numbers in a file that match the area code that the user is searching for. The program prompts the user to enter the phone number and the name of a file. The program writes the matching phone numbers to the output file. For example, Enter the file name: phone_numbers.txt Enter the area code: 813 Output: encoded words are written to file: 813_phone_numbers.txt The program reads the content of the file...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT