Question

So I'm not sure what I'm trying to do here. I don't understand how *adr works,...

So I'm not sure what I'm trying to do here. I don't understand how *adr works, I guess it's a

pointer to the struct Address. How do I complete this function? And how would I have it

just return the zip code or one element if I wanted to?

I have two structs:

struct Address
{
   int number; // street number
   string street; // street Name
   string city; // city
   short zip; // zip code
   string state; // state
};


struct Client
{
   int id; // ID
   string name; // Name
   Address *adr; // ZIP code
   double balance; // Account balance
   string lastTrans; // Date of last transaction
};

And two functions:

Address * newAdr()
{
    Address * na = nullptr;

   na = new Address;

   cout << "\n Enter new address: " << endl;

   cout << " Street Number: ";
   cin >> na->number;

   cout << "\n Street Name: ";
   cin >> na->street;

   cout << "\n City: ";
   cin >> na->city;

   cout << "\n Zip: ";
   cin >> na->zip;

   cout << "\n State: ";
   cin >> na->state;

cout << endl;

   return na;
}

The newAdr() function is fine. But now I call it within this next function:

Client * newClnt()
{
   Client * nc = nullptr;

   nc = new Client;

   // call first newAdr, then complete the rest

   nc->adr = newAdr();

   cout << "\n Please enter other client info: " << endl;

   cout << " Client ID: ";
   cin >> nc->id;

   cout << " Client Name: ";
   cin >> nc->name;

   cout << " Client Balance: ";
   cin >> nc->balance;

   cout << " Client Last Transaction: ";
   cin >> nc->lastTrans;

   // complete the rest

   cout << endl;

   return nc;
}

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

Hi,

Please find the answer below::

Call the function newClnt and save it's result to pointer of client. Now this pointer will contains all the information about the client personal details and address as well.

Please find the code below where print function is defined and using pointer to client as parameter and printing all the details.

If wants to add a function to return particular info that can use -> to get the value.

main.cpp

#include <iostream>
using namespace std;

struct Address
{
   int number; // street number
   string street; // street Name
   string city; // city
   short zip; // zip code
   string state; // state
};


struct Client
{
   int id; // ID
   string name; // Name
   Address *adr; // ZIP code
   double balance; // Account balance
   string lastTrans; // Date of last transaction
};

Address * newAdr()
{
   Address * na = nullptr;

   na = new Address;

   cout << "\n Enter new address: " << endl;

   cout << " Street Number: ";
   cin >> na->number;

   cout << "\n Street Name: ";
   cin >> na->street;

   cout << "\n City: ";
   cin >> na->city;

   cout << "\n Zip: ";
   cin >> na->zip;

   cout << "\n State: ";
   cin >> na->state;

   cout << endl;

   return na;
}


Client * newClnt()
{
   Client * nc = nullptr;

   nc = new Client;

   // call first newAdr, then complete the rest

   nc->adr = newAdr();

   cout << "\n Please enter other client info: " << endl;

   cout << " Client ID: ";
   cin >> nc->id;

   cout << " Client Name: ";
   cin >> nc->name;

   cout << " Client Balance: ";
   cin >> nc->balance;

   cout << " Client Last Transaction: ";
   cin >> nc->lastTrans;

   // complete the rest

   cout << endl;

   return nc;
}


void printDetails(Client * nc){
   cout << "\nClient ID: ";
   cout << nc->id;

   cout << "\nClient Name: ";
   cout << nc->name;

   cout << "\nClient Balance: ";
   cout << nc->balance;

   cout << "\nClient Last Transaction: ";
   cout << nc->lastTrans;


   Address * na;
   cout<<"\nAddress info"<<endl;
   na = nc->adr;

   cout << "\nStreet Number: ";
   cout << na->number;

   cout << "\n Street Name: ";
   cout << na->street;

   cout << "\n City: ";
   cout << na->city;

   cout << "\n Zip: ";
   cout << na->zip;

   cout << "\n State: ";
   cout << na->state;


}

int main() {

   Client *myClient;
   myClient = newClnt();

   printDetails(myClient);

   return 0;
}

output:

Add a comment
Know the answer?
Add Answer to:
So I'm not sure what I'm trying to do here. I don't understand how *adr works,...
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
  • I need help with this code. I'm using C++ and Myprogramming lab to execute it. 11.7:...

    I need help with this code. I'm using C++ and Myprogramming lab to execute it. 11.7: Customer Accounts Write a program that uses a structure to store the following data about a customer account:      Customer name      Customer address      City      State      ZIP code      Telephone      Account balance      Date of last payment The program should use an array of at least 20 structures. It should let the user enter data into the array, change the contents of any element, and display all the...

  • C++ getline errors I am getting getline is undefined error messages. I would like the variables...

    C++ getline errors I am getting getline is undefined error messages. I would like the variables to remain as strings. Below is my code. #include <iostream> #include<string.h> using namespace std; int index = 0; // variable to hold how many customers are entered struct Address //Structure for the address. { int street; int city; int state; int zipcode; }; // Customer structure struct Customer { string firstNm, lastNm; Address busAddr, homeAddr; }; // Functions int displayMenu(); Customer getCustomer(); void showCustomer(Customer);...

  • I'm just a beginner in programming,how to make this program more simple without using #include<iostream> and #include<redex> here is the question Terms to know: If-else statement,for.....

    I'm just a beginner in programming,how to make this program more simple without using #include<iostream> and #include<redex> here is the question Terms to know: If-else statement,for..while..do while,loops,pointer,address,continue,return,break. Create a C++ program which contain a function to ask user to enter user ID and password. The passwordmust contain at least 6 alphanumeric characters, 1 of the symbols !.@,#,$,%,^,&,* and 1 capital letter.The maximum allowable password is 20. Save the information. Test the program by entering the User ID and password. The...

  • write a parent class that describes a parcel (like a package, as shown below) and a...

    write a parent class that describes a parcel (like a package, as shown below) and a child class that describes an overnight parcel. Use the provided Address class. A parcel is described by: id (which might contain numbers and letters) weight (described as the number of pounds; a parcel could be less than 1 pound) destination address (uses the provided class) An overnight parcel is described by id, weight, destination address and also: whether or not a signature is required...

  • I need to get this two last parts of my project done by tonight. If you...

    I need to get this two last parts of my project done by tonight. If you see something wrong with the current code feel free to fix it. Thank you! Project 3 Description In this project, use project 1 and 2 as a starting point and complete the following tasks. Create a C++ project for a daycare. In this project, create a class called child which is defined as follows: private members: string First name string Last name integer Child...

  • I need help with understanding dummy nodes in doubly-linked lists. Here is the code that I...

    I need help with understanding dummy nodes in doubly-linked lists. Here is the code that I have right now. ************city.h**************** #ifndef city_h #define city_h #include <string> using namespace std; class City{ public: City () { name = "N/A"; population = 0; } City (string nm, unsigned int pop){ name = nm; population = pop; } void setName (string name) { this -> name = name; } void setPopulation (unsigned int population){ this -> population = population; } string getName() const...

  • C++ language I need to update this code with the following: ask the user for how...

    C++ language I need to update this code with the following: ask the user for how many structures they would like. Once you get the number, allocate an array of pointers. Once they have been created, proceed to loop through and get the data as usual, and display it back to the screen. struct record {    int age;    string name; }; int check(record r[], int n, string nm) {    for (int i = 0; i < n;...

  • Hey, so i am trying to have my program read a text file using a structure...

    Hey, so i am trying to have my program read a text file using a structure but i also want to be able to modify the results(I kinda have this part but it could be better). I cant seem to get it to read the file(all the values come up as 0 and i'm not sure why because in my other program where it wrote to the txt file the values are on the txt file) i copied and pasted...

  • Declare and define TtuStudentNode in TtuStudentNode.h and TtuStudentNode.cpp file. TtuStudentNode has its unique string variable "studentEmail" which contains email address. TtuStudentNode ha...

    Declare and define TtuStudentNode in TtuStudentNode.h and TtuStudentNode.cpp file. TtuStudentNode has its unique string variable "studentEmail" which contains email address. TtuStudentNode has its unique function member GetEmail() which returns the string variable "studentEmail". TtuStudentNode has its overriding "PrintContactNode()" which has the following definition. void TtuStudentNode::PrintContactNode() { cout << "Name: " << this->contactName << endl; cout << "Phone number: " << this->contactPhoneNum << endl; cout << "Email : " << this->studentEmail << endl; return; } ContactNode.h needs to have the function...

  • This is for a C++ program: I'm almost done with this program, I just need to...

    This is for a C++ program: I'm almost done with this program, I just need to implement a bool function that will ask the user if they want to repeat the read_course function. I can't seem to get it right, the instructions suggest a do.. while loop in the main function. here is my code #include <iostream> #include <cstring> #include <cctype> using namespace std; const int SIZE = 100; void read_name(char first[], char last[]); void read_course(int & crn, char des[],...

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