Question

C++: Array of contact info Design a contact struct, that takes your phone struct and address...

C++: Array of contact info

Design a contact struct, that takes your phone struct and address struct along with a c string for a name to create a record of contact information.

(

C++: Design a Struct

Design two structs address and phone

Address has the following attributes: street address, city name, state code, zip code.

phone has 3 numbers: area code, prefix and suffix

test these by writing a program that creates one of each and fills them with the user given info and prints them out.

must have at least two structs in code and two variables of each struct type that are used.

Example run:

Enter address: 4323 SW 6th Ave.

City: Los Angeles

State Code: CA

Zip Code: 90001

Enter Phone number:

Area Code: 907

Prefix: 342

Suffix: 2731

You Entered:

Address: 4323 SW 6th Ave.

Los Angeles, CA 90001

Phone: (907) - 342 - 2731

) /// this is what it means by your address and phone struct

Test the contact struct by writing a program that creates an array of 50 contact structs.

Allow the user to enter however many contacts and use a for loop to allow the user to enter data for each contact

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

C++ Code :

#include <iostream>
#include <stdio.h>            // for using scanf() and printf()
#include <string.h>           // for using c string
using namespace std;

#define MAX_LIMIT 100           // assuming maximum limit for each string value

// creating a structure for phone
struct phone      
{
   int area_code;      
   int prefix;
   int suffix;
};

// creating a structure for address
struct address
{
   char address[100];
   char city_name[100];
   char state_code[10];
   int zip_code;
};

// creating a structure for contact
struct contact
{
   char name[100];
   int num;
   phone p[1000];       // assuming maximum number of phone numbers entered by a particular contact is 1000
   address add;
};

int main()
{

   // creating an array of contact type of size 50 ( as given in the requirement )
   contact info[50];
  
   // iterating 50 times for taking input in the contact array
   for(int i=0;i<50;i++)
   {      
       // taking input of name of the contact
       cout<<"\nEnter your name: ";
       fgets(info[i].name, MAX_LIMIT, stdin);
      
       // taking input of address of the contact
       cout<<"Enter address: ";
       fgets(info[i].add.address, MAX_LIMIT, stdin);
      
       // taking input of city of the contact
       cout<<"City: ";
       fgets(info[i].add.city_name, MAX_LIMIT, stdin);
      
       // taking input of state code of the contact
       cout<<"State Code: ";
       scanf("%s",info[i].add.state_code);
      
       // taking input of zip code of the contact
       cout<<"Zip Code: ";
       cin>>info[i].add.zip_code;
      
       // Asking how many contacts a particular contact want to enter
       cout<<"How many contacts do you want to enter: ";
       cin>>info[i].num;           // taking input of number of contacts
      
       // iterating info[i].num times for taking input of number of contacts
       for(int j=0;j<info[i].num;j++)
       {
           cout<<"Enter Phone number: ";
          
           // taking input of area code
           cout<<"\nArea Code: ";
           cin>>info[i].p[j].area_code;
          
           // taking input of prefix
           cout<<"Prefix: ";
           cin>>info[i].p[j].prefix;
          
           // taking input of suffix
           cout<<"Suffix: ";
           cin>>info[i].p[j].suffix;
       }
       cin.ignore();           // for clearing one or more characters from the input buffer
   }

   // printing the all entered 50 contact details
   cout<<"\nYou Entered:\n\n";
   for(int i=0;i<50;i++)
   {
       // printing name
       cout<<"Name: "<<info[i].name;
      
       // printing address
       cout<<"Address: "<<info[i].add.address;
      
       // printing city name without new line
       int n = strlen(info[i].add.city_name);
       for(int j=0;j<n-1;j++)
           printf("%c",info[i].add.city_name[j]);
          
       // printing state code
       printf(", %s ",info[i].add.state_code);
      
       // printing zip code
       cout<<info[i].add.zip_code<<"\n";
      
       // printing all the numbers enetered by a particular contact
       for(int j=0;j<info[i].num;j++)
           cout<<"Phone: ("<<info[i].p[j].area_code<<") - "<<info[i].p[j].prefix<<" - "<<info[i].p[j].suffix<<"\n";
          
       cout<<"\n";
   }
}


Output : Note that the Below output is only for 3 contacts due to screenshot size. You can test the above code by entering upto 50 contacts as per the given requirements.

Enter your name: John Woo Enter address: 4323 SW 6th Ave. City: Los Angeles State Code: CA Zip Code: 90001 How many contacts

Add a comment
Know the answer?
Add Answer to:
C++: Array of contact info Design a contact struct, that takes your phone struct and address...
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
  • Please help!! (C++ PROGRAM) You will design an online contact list to keep track of names...

    Please help!! (C++ PROGRAM) You will design an online contact list to keep track of names and phone numbers. ·         a. Define a class contactList that can store a name and up to 3 phone numbers (use an array for the phone numbers). Use constructors to automatically initialize the member variables. b.Add the following operations to your program: i. Add a new contact. Ask the user to enter the name and up to 3 phone numbers. ii. Delete a contact...

  • Programing In C Add Command Line Arguments (Argc and Argv) and use files to the following...

    Programing In C Add Command Line Arguments (Argc and Argv) and use files to the following program. My program is called Ultimo.exe and it is in (C:\Users\Dell\source\repos\Ultimo\Debug). The file used is a .txt file in the directory of the .exe program. The text file which is address.txt is at the same location as Ultimo.exe (C:\Users\Dell\source\repos\Ultimo\Debug). The program takes information in the address.txt file and sorts it by zip code, from smallest to largest. The program works by using input/output redirection...

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