9 Replies - 488 Views - Last Post: 09 July 2012 - 02:10 PM Rate Topic: -----

#1 osu1  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 84
  • Joined: 17-June 12

Receiving and Printing Input from An Array of Structs

Posted 09 July 2012 - 11:17 AM

Hello , I have to write a program that for a family has 4 members. The program has to store the information of the family members and then to display it. The information to be stored for each member are:
First Name
Last Name
Date of Birth
Telephone number
Age
The age needs to be calculated based on current date and the Date of Birth. All these information except the age should be obtained from the user and should be stored in an array.

My problem is that I do not know how to receive and name the variables for input for each structure. My book wont tell me how. This is all I have so far. I am stuck. For example , will the same variables be named for each member?



#include <iostream>
struct inflatable
{
	char firstName[10];
	char lastName[10];
	int dateOfBirth[10];
	int telephone[10];
	int Age[3]; 

};
int main()
{
	using namespace std;
	inflatable members[4] = // initializing array of structs 
	{ firstName, lastname, dateOfBirth, telephone, Age}



Is This A Good Question/Topic? 0
  • +

Replies To: Receiving and Printing Input from An Array of Structs

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3468
  • View blog
  • Posts: 10,688
  • Joined: 05-May 12

Re: Receiving and Printing Input from An Array of Structs

Posted 09 July 2012 - 11:41 AM

What book are you using that it doesn't even show basic I/O in C or C++?
Was This Post Helpful? 0
  • +
  • -

#3 osu1  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 84
  • Joined: 17-June 12

Re: Receiving and Printing Input from An Array of Structs

Posted 09 July 2012 - 12:16 PM

View PostSkydiver, on 09 July 2012 - 11:41 AM, said:

What book are you using that it doesn't even show basic I/O in C or C++?


Primer Plus, it didnt show me any examples where there was actual input in a array of structs.
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3468
  • View blog
  • Posts: 10,688
  • Joined: 05-May 12

Re: Receiving and Printing Input from An Array of Structs

Posted 09 July 2012 - 12:24 PM

Did the book show you how to input into a variable? Did the book show you how to access member variables in a struct? Did the book show you how to access structs in an array? If so, you've got all the information you need. Structs and arrays are just ways of grouping and organizing variables.
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3468
  • View blog
  • Posts: 10,688
  • Joined: 05-May 12

Re: Receiving and Printing Input from An Array of Structs

Posted 09 July 2012 - 12:38 PM

I don't own a copy of the book, but doing a quick scan of the freely available pages of that book on Amazon.com, I saw code that accessed member variables of a struct on p. 142, and some array access in page 118. The array access was for an array of simple types, rather than an array of structs, though.

Perhaps, you are correct that there isn't an exact example of doing I/O with an array of structs.

It shouldn't matter, though, since once you've got access to an struct in array, then you are playing with a struct, and you would use it the same way you would use any other struct.
Was This Post Helpful? 0
  • +
  • -

#6 osu1  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 84
  • Joined: 17-June 12

Re: Receiving and Printing Input from An Array of Structs

Posted 09 July 2012 - 12:41 PM

View PostSkydiver, on 09 July 2012 - 12:24 PM, said:

Did the book show you how to input into a variable? Did the book show you how to access member variables in a struct? Did the book show you how to access structs in an array? If so, you've got all the information you need. Structs and arrays are just ways of grouping and organizing variables.


I know but my problem is that the book doesnt show me how to name the members with input. Since I will be inputing data of 4 different members do I name the members the same or not for input? I tried naming the different members in the array but the program is telling me is wrong.

View Postosu1, on 09 July 2012 - 12:38 PM, said:

View PostSkydiver, on 09 July 2012 - 12:24 PM, said:

Did the book show you how to input into a variable? Did the book show you how to access member variables in a struct? Did the book show you how to access structs in an array? If so, you've got all the information you need. Structs and arrays are just ways of grouping and organizing variables.


I know but my problem is that the book doesnt show me how to name the members with input. Since I will be inputing data of 4 different members do I name the members the same or not for input? I tried naming the different members in the array but the program is telling me is wrong.


#include <iostream>
struct inflatable
{
	char firstName[10];
	char lastName[10];
	int dateOfBirth[10];
	int telephone[10];
	int Age[3]; 

};
int main()
{
	using namespace std;
	inflatable members[1] = // initializing array of structs 
	{ firstName, lastname, dateOfBirth, telephone, Age}// naming the members for input
};



I did this for one member. I have not added cin nor cout yet just trying to set up the array of structs. Thank you for your help but this program has been frustrating me for hours.
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3468
  • View blog
  • Posts: 10,688
  • Joined: 05-May 12

Re: Receiving and Printing Input from An Array of Structs

Posted 09 July 2012 - 01:02 PM

You don't have to initialize to specific values. You can simply have:
inflatable members[4] = { 0 };


This will create an array of 4 inflatables. Each of the inflatables will be zero filled.

Now you can proceed to writing your code that loops and gets input from the user and fill in each struct.
Was This Post Helpful? 0
  • +
  • -

#8 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3468
  • View blog
  • Posts: 10,688
  • Joined: 05-May 12

Re: Receiving and Printing Input from An Array of Structs

Posted 09 July 2012 - 01:09 PM

Out of curiosity, inside inflatable, why do you have these members:
	int dateOfBirth[10];
	int telephone[10];
	int Age[3]; 



Do you plan on inputing the date of birth and telephone number one integer at a time and storing them separately? Wouldn't it make more sense to have the date of birth as 3 separate integers: day, month, and year? As for the phone number, wouldn't just a character string like for the names suffice?

You are suppose to compute the age, but it looks like you plan on storing 3 different ages. Or are you planning on storing the age one digit at a time?
Was This Post Helpful? 0
  • +
  • -

#9 osu1  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 84
  • Joined: 17-June 12

Re: Receiving and Printing Input from An Array of Structs

Posted 09 July 2012 - 01:22 PM

View PostSkydiver, on 09 July 2012 - 01:02 PM, said:

You don't have to initialize to specific values. You can simply have:
inflatable members[4] = { 0 };


This will create an array of 4 inflatables. Each of the inflatables will be zero filled.

Now you can proceed to writing your code that loops and gets input from the user and fill in each struct.


Do i have to assign a certain input to each struct? Or does is not matter, for example, would it be the same as for member 1 as member 2 even though its different info? I really do not get how to do this program but thank you for giving me tips.

View PostSkydiver, on 09 July 2012 - 01:09 PM, said:

Out of curiosity, inside inflatable, why do you have these members:
	int dateOfBirth[10];
	int telephone[10];
	int Age[3]; 



Do you plan on inputing the date of birth and telephone number one integer at a time and storing them separately? Wouldn't it make more sense to have the date of birth as 3 separate integers: day, month, and year? As for the phone number, wouldn't just a character string like for the names suffice?

You are suppose to compute the age, but it looks like you plan on storing 3 different ages. Or are you planning on storing the age one digit at a time?


I thought you put the members there like that. Are you suppose to store them differently with int? How would I put the birth differentely like that? Thank you.
Was This Post Helpful? 0
  • +
  • -

#10 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3468
  • View blog
  • Posts: 10,688
  • Joined: 05-May 12

Re: Receiving and Printing Input from An Array of Structs

Posted 09 July 2012 - 02:10 PM

Consider this code that inputs the coordinates of triangle:
struct Coord
{
    int x;
    int y;
};

struct Triangle
{
    char name[32];
    Coord points[3];
};

int main()
{
    Triangle triangle;
    cout << "Enter a name for the triangle: ";
    cin >> triangle.name;
    for(int i = 0; i < 3; ++i)
    {
        cout << "Triangle corner #" << i + 1 << ":" << endl;
        cout << "Enter x coordinate: ";
        cin >> triangle.points[i].x;
        cout << "Enter y coordinate: ";
        cin >> triangle.points[i].y;
    }
    return 0;
}



View Postosu1, on 09 July 2012 - 01:22 PM, said:

View PostSkydiver, on 09 July 2012 - 01:09 PM, said:

Out of curiosity, inside inflatable, why do you have these members:
	int dateOfBirth[10];
	int telephone[10];
	int Age[3]; 



Do you plan on inputing the date of birth and telephone number one integer at a time and storing them separately? Wouldn't it make more sense to have the date of birth as 3 separate integers: day, month, and year? As for the phone number, wouldn't just a character string like for the names suffice?

You are suppose to compute the age, but it looks like you plan on storing 3 different ages. Or are you planning on storing the age one digit at a time?


I thought you put the members there like that. Are you suppose to store them differently with int? How would I put the birth differentely like that? Thank you.


As written, you are saying that you will have 10 integers for date of birth, 10 integers for the the telephone number and 3 integers for the age.

As I stated before you probably want just 3 integers for the date of birth: 1 to store the date (1-31), 1 for the month (1-12), and 1 for the year. The phone number is probably better stored as a string just like you are storing your names right now. As for the age, a single integer should be sufficient, don't you think?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1