Question

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 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. If name or number does not exist, display error and let user try again.

Example:

Welcome to the Phone Number Lookup Program!

Enter the phone number you wish to look up: 510-649-0730

That phone number belongs to Pamela Contag

Would you like to find another name? (Y or N): y

Enter the phone number you wish to look up: 614-685-5195

That phone number belongs to Tom Daly

Would you like to find another name? (Y or N): y

Enter the phone number you wish to look up: 256-512-1024

256-512-1024 is not in the data base, sorry.

Would you like to find another name? (Y or N): n

End program.

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

I hope this is what you want. if you still have any doubt please let me know. comment is done where it is necessary. I have implemented both the method based on number as well as name (name is actually).

Transform function convert both string into lower case

----------------------------------------------------------------------------------------------

#include <iostream> // Header file for input and output
#include <fstream> // header file for file operation
#include<string.h> //header file for string function
#include<algorithm>
using namespace std;
int main()
{

    ifstream fin,fin1; // input file stream for reading purpose two object fin, fin1
    string line,line1,line2; //line for phone number, line1 for name, line2 for inputby user

    char ch='y';//for reapeting operation
    int t; //for selecting case
   cout<<"Welcome to the phone number look up program"<<endl;

    while(ch=='y'||ch=='Y'){ //loop untill y is pressed
       cout<<"Enter a number 1. for searching name from number and 2. for searching number for name="<<endl;
       cin>>t; //input from user is read into t which select a case
       cin.ignore(); // ignoring first enter
    
       int x=0; // for compare function
       int flag1=0,flag=0;// for printing validation
       switch(t){
           case 1 :     fin.open("phoneNames.txt", ios::out | ios::in); //opening file phonenames
    fin1.open("phoneNumber.txt", ios::out | ios::in);//openaning file phonenumber
         
                       flag=0;
                       cout<<"Enter the Number you wish to lookup= ";
                       getline(cin,line2); // reading all string in line 2
                
                         while (fin) { // untill file is end
                            getline(fin, line);// reading a line from file phonename
                           getline(fin1,line1); // reading a line from file phone number
                           if((x=line1.compare(line2))==0){ // comparing phone number with input if it is 0 it is match
                               cout<<"That Number belongs to"<<line<<endl;// printing name
                               flag=1;// for printing when mtch is foun we are setting flag to 1
                               break;
                           }
                        }
                   
                        if(flag==0){ // if flag==0 than match not found
                                cout<<line2<<" is not in database sorry!"<<endl;
                       }
                       fin.close(); //file close
                         fin1.close();
                        break;
        
           case 2:
                      fin.open("phoneNames.txt", ios::out | ios::in); //opening file phonenames
    fin1.open("phoneNumber.txt", ios::out | ios::in);//openaning file phonenumber
                   
                         flag1=0;
                       cout<<"Enter the Name you wish to lookup= ";
                        
                       getline(cin,line2); // reading all string in line 2
                         
                        while (fin1) { //untill file is end
                            getline(fin, line);
                           getline(fin1,line1);
                           transform(line2.begin(),line2.end(),line2.begin(),::tolower);
                           transform(line.begin(),line.end(),line.begin(),::tolower);
                         
                           if((x=line.compare(line2))==0){
                               cout<<"That number of "<<line2 <<"is "<<line1<<endl;
                               flag1=1;
                               break;
                           }
                        }
                      
                        if(flag1==0){
                                cout<<line2<<" is not in database sorry!"<<endl;
                                   fin1.seekg(0, ios::beg);
                              
                       }
                        fin.close(); //file close
                         fin1.close();
                       break;
     
        default: cout<<"Enter valid choice"<<endl;
        
       }

       cout<<"Would you like another Name OR Number to continue press y ";
       cin>>ch;
   }

    return 0;
}

---------------------------------------------------------------------

Classes Debug Untitied 60 O C:\Users\Makadia Vyomesh\Documents\Untitled1.exe 61 E if((x=line.compare(line2))==0){ cout<<That

Classes Debug Untitied 60 O C:\Users\Makadia Vyomesh\Documents\Untitled1.exe 61 E if((x=line.compare(line2))==0){ cout<<"That number of "<<line2 Enter a number 1. for searching name from number and 2. for searching number for name= flag1=1; break; Welcome to the phone number look up program 62 63 2 64 Enter the Name you wish to lookup= John Doe 65 That number of John Doeis 440-879-9876 Would you like another Name OR Number to continue press y y Enter a number 1. for searching name from number and 2. for searching number for name= 66 67 68 É if(flag1==0){ Enter the Name you wish to lookup= John John is not in database sorry! fin1.seekg(0, ios::beg); Would you like another Name OR Number to continue press y y cout<<line2<<" is not in datab 69 70 71 Enter a number 1. for searching name from number and 2. for searching number for name= 72 73 fin.close(); //file close fin1.close(); break; Enter the Number you wish to lookup= 440-879-9876 That Number belongs toJohn Doe Would you like another Name OR Number to continue press y n 74 75 76 default: cout<<"Enter valid choice"<<endl; 77 Process exited after 34.72 seconds with return value 0 78 Press any key to continue . 79 80 cout<<"Would you like another Name OR Number to continu cin>>ch; 81 82 83 84 85 86 return 0; 87 88 38 Compiler Resources dlh Compile Log Debug 3 Find Results Close Compilation results... Abort Compilation Errors: 0 Warnings: 0 Output Filename: C:\Users\Makadia Vyomesh\Documents\Untitledl.exe Shorten compiler paths Output Size: 1.83431243896484 MiB Compilation Time: 1.16s Col: ne: 88 Sel: 3906 Lines: 88 Length: 3908 Insert Done parsing in 0 seconds

Add a comment
Know the answer?
Add Answer to:
In C++, Write a program that retrieves a phone number from a txt files of names...
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
  • build a phone number from digits entered by your user. Your program will loop until 10...

    build a phone number from digits entered by your user. Your program will loop until 10 valid digits have been entered. It will then use those digits to display the phone number entered using the format: XXXXXXXXXX (or (XXX) XXX – XXXX for extra credit). The program will ask for a digit, it will check to see if the digit is actually between 0 and 9 inclusively. If so, the digit will be stored as the next number in the...

  • 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 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...

  • Using Java how would I write a program that reads and writes from binary or text...

    Using Java how would I write a program that reads and writes from binary or text files and gives me an output similar to this? Example Output: --------------------Configuration: <Default>-------------------- Enter the file name: kenb Choose binary or text file(b/t): b Choose read or write(r/w): w Enter a line of information to write to the file: lasdklj Would you like to enter another line? Y/N only n Continue? (y/n)y Enter the file name: kenb Choose binary or text file(b/t): b Choose...

  • This is Python The program should accept input from the user as either 7-digit phone number...

    This is Python The program should accept input from the user as either 7-digit phone number or 10-digit. If the user enters 7 characters, the program should automatically add "512" to the beginning of the phone number as the default area code. Dash (hyphen) characters do not count in input character count, but must not be random. If the user enters dashes the total character count must not exceed 12. The program should not crash if the user enters invalid...

  • 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...

  • Write a program that asks the user for the name of an image, the name of an output file. Your pro...

    Write a program that asks the user for the name of an image, the name of an output file. Your program should then save the upper right quarter of the image to the output file specified by the user. A sample run of your program should look like: Enter image file name: hunterLogo.png Enter output file: logOUR.png which would have as input and output: HUNTER ITER The City University of New York Hint: See sample programs from Lectures 3 and...

  • 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...

  • How can I create Loops and Files on Java? • Create a program that reads a...

    How can I create Loops and Files on Java? • Create a program that reads a list of names from a source file and writes those names to a CSV file. The source file name and target CSV file name should be requested from the user • The source file can have a variable number of names so your program should be dynamic enough to read as many names as needed • When writing your CSV file, the first row...

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