Question

In: Computer Science

Write a simple phonebook using c++ that read text file name list.txt and has:  first name, last...

Write a simple phonebook using c++ that read text file name list.txt and has:  first name, last name, and phone number as the example:

MIKEL BUETTNER 3545044
ENOCH BUGG 2856220
BRENDON LAVALLEY 433312
QUINTIN CREEK 5200413
JAMISON MILLETT 6243458
FLORENCIO PUMPHREY 3296862
DARRICK FREGOSO 6868442
TOBIAS GLASSMAN 6040564

and then ask the user to add a new contact first name, last name, and phone number and same the information into a file.

Use array and pointer

Solutions

Expert Solution

#include <cmath>

#include <cstdio>

#include <vector>

#include <iostream>

#include <algorithm>

using namespace std;


//creating the struct for the local storage of the phonebook for firstname , lastname and phonenumber as array of char

typedef struct phonebook_detail

   {

      char FirstName[20];

      char LastName[20];

      char PhoneNumber[20];

   } phone;

//Declaring the FILE pointer *pRead to read and *pWrite pointer to write the file

FILE *pRead;

FILE *pWrite;

//Function to Add_new_Entry

void Add_new_Entry (phone * phonebook)

{

        //use the pWrite pointer to open the list.txt file using fopen

         pWrite = fopen("list.txt", "a");


        //Take the use input as First name, Last name and phone number

         printf("\nFirst Name: ");

         scanf("%s", phonebook[counter-1].FirstName);

         printf("Last Name: ");

         scanf("%s", phonebook[counter-1].LastName);

        printf("Phone Number: ");

         scanf("%s", phonebook[counter-1].PhoneNumber);

        // Adding the details took by user as input to the file using fprint and pWrite

         fprintf(pWrite, "%s\t%s\t%s\n", phonebook[counter-1].FirstName, phonebook[counter-1].LastName, phonebook[counter-1].PhoneNumber);

        // close the file after saving the detail's in the file

         fclose(pWrite);

      }

int main(){

   //Declaring the phonebook pointer of typedef phone which used to store the user input

    phone *phonebook;

   // Allocate the memory using malloc function of typedef phone

    phonebook = (phone*) malloc(sizeof(phone)*100);

   // call the function adding new entry

    Add_new_Entry (phonebook);

}


Related Solutions

In linux , Using a simple text editor, create a text file with the following name...
In linux , Using a simple text editor, create a text file with the following name &quot;Test&quot; and content: GNU GENERAL PUBLIC LICENSE The GNU General Public License is a free, copy left license for the GNU General Public License is intended to guarantee your freedom to GNU General Public License for most of our software; it applies … 2-Write the command to create the text file. 3-Print the file permissions. 4-Create a directory named &quot;361&quot; 5-Copy file &quot;Test&quot; to...
The program reads a text file with student records (first name, last name and grade on...
The program reads a text file with student records (first name, last name and grade on each line) and determines their type (excellent or ok). <--- Completed Need help on this Then it prompts the user to enter a command, executes the command and loops. The commands are the following: "all" - prints all student records (first name, last name, grade, type). "excellent" - prints students with grade > 89. "ok" - prints students with grade <= 89. "end" -...
Write a C++ Program to print the first letter of your first and last name using...
Write a C++ Program to print the first letter of your first and last name using stars. Note: 1) Using nested For Loop 2) The number of lines is given by user. 3) Using one Outer loop to print your letters. 4) Print the letters beside each other.
Write a C++ Program to print the first letter of your first and last name using...
Write a C++ Program to print the first letter of your first and last name using stars. Note: 1) Using nested For Loop 2) The number of lines is given by user. 3) Using one Outer loop to print your letters. 4) Print the letters beside each other
HW_6a - Read a text file Create a new C++ project and name it as:   Numbers...
HW_6a - Read a text file Create a new C++ project and name it as:   Numbers Create a text file and     save it as:   data.txt Create and save the file      in a C++ project      in the Resource folder. Enter the following numbers:        3                                                              4                                                              5       Note:   After you enter the 5, don’t press the enter key. Save and close the file. Add another file and name it:   Source.cpp Write one statement that declares a file...
C++ Parse text (with delimiter) read from file. If a Text file has input formatted such...
C++ Parse text (with delimiter) read from file. If a Text file has input formatted such as: "name,23" on every line where a comma is the delimiter, how would I separate the string "name" and integer "23" and set them to separate variables for every input of the text file? I am trying to set a name and age to an object and insert it into a vector. #include <iostream> #include <vector> #include <fstream> using namespace std; class Student{    ...
Write a C program to run on unix to read a text file and print it...
Write a C program to run on unix to read a text file and print it to the display. It should count of the number of words in the file, find the number of occurrences of a substring, and take all the words in the string and sort them (ASCII order). You must use getopt to parse the command line. There is no user input while this program is running. Usage: mywords [-cs] [-f substring] filename • The -c flag...
(C++) Write a program to read from a grade database (data.txt). The database (text file) has...
(C++) Write a program to read from a grade database (data.txt). The database (text file) has students names, and grades for 10 quizzes.Use the given function prototypes to write the functions. Have main call your functions. The arrays should be declared in main() and passed to the functions as parameters. This is an exercise in parallel arrays, int and char 2 dim arrays. Function prototypes: int readData(ifstream &iFile, int scores[][10], char names[][30]); This functions takes the file stream parameter inFile...
The objective is to read the last five lines of a file (text file) in Perl....
The objective is to read the last five lines of a file (text file) in Perl. I wrote down what I have written so far in Perl but I get an error message saying, "Can't open sample.txt: No such file or directory." The sample.txt file is located in the same directory that I am executing the perl scripts on the command line. sample.txt ============================================================= My name is Jennifer and I like to eat tacos de birria . =========================================================== #!/usr/bin/perl $filename=...
Python program: Write a program that reads a text file named test_scores.txt to read the name...
Python program: Write a program that reads a text file named test_scores.txt to read the name of the student and his/her scores for 3 tests. The program should display class average for first test (average of scores of test 1) and average (average of 3 tests) for each student. Expected Output: ['John', '25', '26', '27'] ['Michael', '24', '28', '29'] ['Adelle', '23', '24', '20'] [['John', '25', '26', '27'], ['Michael', '24', '28', '29'], ['Adelle', '23', '24', '20']] Class average for test 1...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT