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...
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++) 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...
Using OOP, write a C++ program that will read in a file of names. The file...
Using OOP, write a C++ program that will read in a file of names. The file is called Names.txt and should be located in the current directory of your program. Read in and store the names into an array of 30 names. Sort the array using the selection sort or the bubblesort code found in your textbook. List the roster of students in ascending alphabetical order. Projects using global variables or not using a class and object will result in...
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...
How would I read only the first line of text file into C++ For instance, the...
How would I read only the first line of text file into C++ For instance, the first line of a text file is: 5 (space) 5. I need to read these numbers into a row variable and a column variable. I am just not sure how to do it. I can only use the #include header. I can't use any other header. The project mentions using redirected input in Visual Studio for our text file.
Write a simple text-formating.cpp file that reads (asks for then reads) a text file and produces...
Write a simple text-formating.cpp file that reads (asks for then reads) a text file and produces another text file in Which blank lines are removed, multiple blanks are replaced with a single blank, and no lines are longer than some given length (let say 80). Put as many words as possible on the same line (as close as possible to 80 characters). You will have to break some lines of the given file, but do not break any words or...
Using C++, write a code that this program always stores text file output into a text...
Using C++, write a code that this program always stores text file output into a text file named "clean.txt". -The program should read one character at a time from "someNumbers.txt", and do the following. -If it is a letter, print that letter to the screen, AND also store it in the text file. All letters should be converted to lowercase beforehand. -If it is a number, print that number to screen, but do NOT store it in the text file....
Write a C++ program to open and read a text file and count each unique token...
Write a C++ program to open and read a text file and count each unique token (word) by creating a new data type, struct, and by managing a vector of struct objects, passing the vector into and out of a function. Declare a struct TokenFreq that consists of two data members: (1) string value; and (2) int freq; Obviously, an object of this struct will be used to store a specific token and its frequency. For example, the following object...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT