We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,577 Members — Technology Publication meets Social Media

# structure called toy with members

hi! I'm new in c++, can anyone give some instruction how I'll start this structure solution? if so, please give me some instruction

Problem given:

Define a structure called Toy with the following members:

. Name of Toy
. Price of Toy
. Manufactured date (define as a structure with date, month, year)

Using the structure defined above, write a program that declares a variable of structure
Toy. Ask the user to enter the information of a toy. Then pass the variable to a function
to do the following:

. Check the manufactured date of the toy. If the date is before January 2012,
decrease 10% from the current price.
RM10.00 to the price.

Display the toy’s information in main(). Repeat for a new toy. Let the user chooses to
stop.

4
Contributors
8
Replies
1 Day
Discussion Span
6 Months Ago
Last Updated
9
Views
akashi
Newbie Poster
14 posts since Sep 2012
Reputation Points: 0
Skill Endorsements: 0

What part are you getting hung up on? Do you know how to create a struct?

NathanOliver
Posting Virtuoso
1,513 posts since Apr 2009
Reputation Points: 269
Skill Endorsements: 3
``````#include<iostream>
#include<string>
using namespace std;

struct Toy
{
string toyName;
double price;
int d,m,y;
};

void input (Toy & to)
{
cout<<"Enter The Toy Information:\n"<<endl;
cout<<"Enter Name of Toy: "; cin>>to.toyname;
cout<<" Enter Price of Toy: "; cin>>to.price;
cout<<"\n"<<endl;
cout<<"Manufactured Date Info"<<endl;
cout<<"Enter Day: "; cin>>to.d;
cout<<"Enter Month: "; cin>>to.m;
cout<<"Enter Year: "; cin>>to.y;

}
void Display (Toy to)
{
cout<<"Name of Toy:"<<to.toyName<<endl;
cout<<"Price of Toy: "<<to.price<<endl;
cout<<"Manufactured Date: "<<to.d <<"/"<<to.m<<"/"<<to.y<<"\n"<<endl;
}

int main ()
{
Toy to;
input (to);
display (to);

system ("pause");
return 0;
}
``````

`Inline Code Example Here`

akashi
Newbie Poster
14 posts since Sep 2012
Reputation Points: 0
Skill Endorsements: 0

I can do only up to this. what chang do I need sir?

akashi
Newbie Poster
14 posts since Sep 2012
Reputation Points: 0
Skill Endorsements: 0

I hate to reiterate what Nathan already said but....what exactly are you stuck on?

Having said that I should point out that the above code already has mistakes -- did you know this?
e.g. line 16 should actually be:
`cout<<"Name of Toy:"<<to.toyName<<endl;` -- toyname is not a member of your struct but toyName is

In main you also call `display()` when in fact you should be calling `Display()` (unless you rename your function).

Also, if you read the requirements that you posted you will notice that the manufacture date should be a struct, hence your toy struct should contain another struct instead of the three ints that you are currently using.

For example:

``````struct Toy
{
string toyName;
double price;
Date date;
};
``````

Where Date is defined as:

``````struct Date
{
int day;
int month;
int year;
};
``````

Finally, you don't appear to have any validation at present is this what you need help with?

spuriousgeek
Newbie Poster
13 posts since Nov 2012
Reputation Points: 14
Skill Endorsements: 0

I've updated a bit in the previous coding for date that you mentioned. Now I need to do the validation for( date and madein). (1) to decrease 10% of price if date before January 2012 (2) to add 10 Rm if the madein country is Malaysia. stuck again!

akashi
Newbie Poster
14 posts since Sep 2012
Reputation Points: 0
Skill Endorsements: 0
``````#include<iostream>
#include<string>
using namespace std;

struct Toy
{
string toyName;
double price;

};

struct Date
{
int day;
int month;
int year;
};

void input (Toy & to)
{
cout<<"Enter The Toy Information:"<<endl;
cout<<"***************************"<<endl;
cout<<"\n Enter Name of Toy: "; cin>>to.toyName;
cout<<" Enter Price of Toy: "; cin>>to.price;
cout<<"\n"<<endl;
}
void input (Date & da)
{
cout<<"\nManufactured Date Info"<<endl;
cout<<"***********************"<<endl;
cout<<" Enter Day: "; cin>>da.day;
cout<<" Enter Month: "; cin>>da.month;
cout<<" Enter Year: "; cin>>da.year;

}
void display (Toy to)
{
cout<<"\n      Name of Toy: "<<to.toyName<<endl;
cout<<"     Price of Toy: "<<"RM "<<to.price<<endl;

}

void Display (Date da)
{
cout<<"\nManufactured Date: "<<da.day <<"/"<<da.month<<"/"<<da.year<<"\n"<<endl;
}
int main ()
{
Toy to;
input (to);
display (to);
Date da;
input (da);
Display (da);

system ("pause");
return 0;
}
``````

`Inline Code Example Here`

akashi
Newbie Poster
14 posts since Sep 2012
Reputation Points: 0
Skill Endorsements: 0

Now I need to do the validation for( date and madein). (1) to decrease 10% of price if date before January 2012 (2) to add 10 Rm if the madein country is Malaysia. stuck again!

Why?

If you have a day, month and year what is stopping you from determining if it's before january 2012?..

You also have a "madeIn" member. Why can't you compare it's value with "malaysia" and act based on that?

I'm not sure if you are even trying..

Gonbe
Posting Whiz in Training
231 posts since Jan 2010
Reputation Points: 33
Skill Endorsements: 9

I would like to mention a couple of things akashi:
1) When programming you really need to pay close attention to code that you are reading or writing -- The updated code that you have provided shows a new struct that can be used to represent a date, however, your Toy struct still doesn't contain a data member.

It should look more like:

``````struct Toy
{
string toyName;
double price;
Date date;
};
``````

2) If you are struggling to figure out how to write something in code it can very often be helpful to write down a solution in pseudo-code or plain English first and then convert to code after.

Finally, just in case you weren't already aware, DaniWeb isn't a place you should come to get someone else to do your work for you - You will never learn this way!
Instead, the forumns provide a way for you to get assistance when you are having problems or to get new ideas that may further your development skills.

Edit:
Once you have made an attempt at the validation, please post the code that you have produced yourself so that we can help you to resolve any issues.

spuriousgeek
Newbie Poster
13 posts since Nov 2012
Reputation Points: 14