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

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...
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.
(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...
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...
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...
how to read, write and append a text file using a switch case in java.I need...
how to read, write and append a text file using a switch case in java.I need help with the code.
C++ Write a program that asks a teacher to input a student’s first name, last name,...
C++ Write a program that asks a teacher to input a student’s first name, last name, and four test scores. The program should find the average of the four test scores and should then write the following information to a file named “students.txt” last_name first_name average A student's first name of “XX” should be used as a sentinel value and no numeric grades less than 0 or greater than 100 should be accepted.  The program should then read the information in...
Please write a java program to write to a text file and to read from a...
Please write a java program to write to a text file and to read from a text file.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT