freelanceprogrammers.org Forum Index » C

Mixing struct, pointer and array.


View user's profile Post To page top
blodstone2v Posted: Fri Dec 31, 2004 11:55 pm


Joined: 31 Dec 2004

Posts: 3
Mixing struct, pointer and array.
Hai,
I`m trying to use pointer, array and struct together. I`m a newbie.
Here is my code. I still don`t know what`s wrong with it. So guys plz help
me out.

#include <iostream>
using namespace std;
#include <conio.h>

const int nameSize = 20;
struct pizza
{
char name[nameSize];
float diameter;
float weight;
};

int main()
{
cout << "Welcome to William Wingate Pizza Analysis Service!
";
cout << "How many pizza do you want to make?
";

/////////////////
int quantity;
(cin >> quantity).get();
pizza * guest=new pizza[quantity];
/////////////////

///////////////////////////////////////////////////////////
for (int i=1; i<=quantity; i++)
{
cout << "Enter number " << i << " pizza statistics!
";
cout << "Enter pizza name : ";
cin.getline(guest[i]->name,nameSize);
cout << "Enter pizza diameter : ";
cin >> guest[i]->diameter;
cout << "Enter pizza weight : ";
(cin >> guest[i]->weight).get();
}
///////////////////////////////////////////////////////////
cout << "
Here is your pizza statistics!
";
///////////////////////////////////////////////////////////
for (int i=1; i<=quantity; i++)
{
cout << "Pizza number " << i << endl;
cout << "Name : " << guest[i]->name << endl;
cout << "Diameter : " << guest[i]->diameter << endl;
cout << "Weight : " << guest[i]->weight << endl;
}
///////////////////////////////////////////////////////////
getch();
return 0;
}

Thank you and Happy New Year
Reply with quote
Send private message
View user's profile Post To page top
maniksinghal Posted: Sun Jan 02, 2005 12:33 am


Joined: 02 Jan 2005

Posts: 9
Mixing struct, pointer and array.
--- Hardy Huang <blodstone@...> wrote:

>
> Hai,
> I`m trying to use pointer, array and struct
> together. I`m a newbie.
> Here is my code. I still don`t know what`s wrong
> with it. So guys plz help
> me out.
>
> #include <iostream>
> using namespace std;
> #include <conio.h>
>
> const int nameSize = 20;
> struct pizza
> {
> char name[nameSize];
> float diameter;
> float weight;
> };
>
> int main()
> {
> cout << "Welcome to William Wingate Pizza
> Analysis Service!
";
> cout << "How many pizza do you want to
> make?
";
>
> /////////////////
> int quantity;
> (cin >> quantity).get();
> pizza * guest=new pizza[quantity];
> /////////////////
>
>
>
///////////////////////////////////////////////////////////
> for (int i=1; i<=quantity; i++)
> {
> cout << "Enter number " << i << " pizza
> statistics!
";
> cout << "Enter pizza name : ";
> cin.getline(guest[i]->name,nameSize);
> cout << "Enter pizza diameter : ";
> cin >> guest[i]->diameter;
> cout << "Enter pizza weight : ";
> (cin >> guest[i]->weight).get();
> }
>
>
///////////////////////////////////////////////////////////
> cout << "
Here is your pizza statistics!
";
>
>
///////////////////////////////////////////////////////////
> for (int i=1; i<=quantity; i++)
> {
> cout << "Pizza number " << i << endl;
> cout << "Name : " << guest[i]->name <<
> endl;
> cout << "Diameter : " << guest[i]->diameter
> << endl;
> cout << "Weight : " << guest[i]->weight
> << endl;
> }
>
>
///////////////////////////////////////////////////////////
> getch();
> return 0;
> }
>
> Thank you and Happy New Year
>
>
>
>
>
>

Hi Binit,

Firstly you didnt mention what problem are you facing
with the code.. (compiler error.. runtime error? )

The loop range you are using is from 1 to <quantity>.
It should be from 0 to <quantity -1>.

Regards
Manik




__________________________________
Do you Yahoo!?
Send holiday email and support a worthy cause. Do good.
http://celebrity.mail.yahoo.com
Reply with quote
Send private message
View user's profile Post To page top
blodstone2v Posted: Sun Jan 02, 2005 3:17 pm


Joined: 31 Dec 2004

Posts: 3
Mixing struct, pointer and array.
It is a compiler error.
It says:
base operand of `->` has non-pointer type `pizza`

At 01:33 AM 1/2/2005, you wrote:

--- Hardy Huang
<blodstone@...> wrote:
>
> Hai,
>       I`m trying to use pointer, array
and struct
> together. I`m a newbie.
> Here is my code. I still don`t know what`s wrong
> with it. So guys plz help
> me out.
>
> #include <iostream>
> using namespace std;
> #include <conio.h>
>
> const int nameSize = 20;
> struct pizza
> {
>      char name[nameSize];
>      float diameter;
>      float weight;
> };
>
> int main()
> {
>      cout << "Welcome to William
Wingate Pizza
> Analysis Service!
";
>      cout << "How many pizza do
you want to
> make?
";
>
>      /////////////////
>      int quantity;
>      (cin >> quantity).get();
>      pizza * guest=new
pizza[quantity];
>      /////////////////
>
>    
>
///////////////////////////////////////////////////////////
>      for (int i=1; i<=quantity;
i++)
>      {
>          cout <<
"Enter number " << i << " pizza
> statistics!
";
>          cout <<
"Enter pizza name     : ";
>         
cin.getline(guest[i]->name,nameSize);
>          cout <<
"Enter pizza diameter : ";
>          cin >>
guest[i]->diameter;
>          cout <<
"Enter pizza weight   : ";
>          (cin >>
guest[i]->weight).get();
>      }
>    
>
///////////////////////////////////////////////////////////
>      cout << "
Here is your
pizza statistics!
";
>    
>
///////////////////////////////////////////////////////////
>      for (int i=1; i<=quantity;
i++)
>      {
>          cout <<
"Pizza number " << i << endl;
>          cout <<
"Name     : " << guest[i]->name
<<
> endl;
>          cout <<
"Diameter : " << guest[i]->diameter
> << endl;
>          cout <<
"Weight   : " << guest[i]->weight
> << endl;
>      }
>    
>
///////////////////////////////////////////////////////////
>      getch();
>      return 0;
> }
>
> Thank you and Happy New Year
>
>
>
>
>
>
Hi Binit,
Firstly you didnt mention what problem are you facing
with the code.. (compiler error.. runtime error? )
The loop range you are using is from 1 to <quantity>.
It should be from 0 to <quantity -1>.
Regards
Manik

           
__________________________________
Do you Yahoo!?
Send holiday email and support a worthy cause. Do good.
http://celebrity.mail.yahoo.com


To unsubscribe :
programmers-town-unsubscribe@yahoogroups.com



Yahoo! Groups Links

To visit your group on the web, go to:
http://groups.yahoo.com/group/Programmers-Town/
  To unsubscribe from this group, send an email to:
Programmers-Town-unsubscribe@yahoogroups.com
  Your use of Yahoo! Groups is subject to the
Yahoo! Terms of Service.



*************************************************************************
Laughing at our mistakes can lengthen our own life.
Laughing at someone else`s can shorten
it.      
---Cullen Hightower---
************************************************************************
Reply with quote
Send private message
View user's profile Post To page top
maniksinghal Posted: Mon Jan 03, 2005 9:18 am


Joined: 02 Jan 2005

Posts: 9
Mixing struct, pointer and array.
Ya, the compiler is right (obviously it had to be
right)

The statement:
pizza * guest = new pizza[quantity];

allocates an array of pizza and makes guest pointer
point to it.
Therefore guest[i] is not a pointer but a pizza
element.
Therefore use the dot (.) operator instead of ->

guest[i].name ....

Regards
Manik


--- Hardy Huang <blodstone@...> wrote:

> It is a compiler error.
> It says:
>
> base operand of `->` has non-pointer type `pizza`
>
>
> At 01:33 AM 1/2/2005, you wrote:
>
>
> >--- Hardy Huang <blodstone@...> wrote:
> >
> > >
> > > Hai,
> > > I`m trying to use pointer, array and
> struct
> > > together. I`m a newbie.
> > > Here is my code. I still don`t know what`s wrong
> > > with it. So guys plz help
> > > me out.
> > >
> > > #include <iostream>
> > > using namespace std;
> > > #include <conio.h>
> > >
> > > const int nameSize = 20;
> > > struct pizza
> > > {
> > > char name[nameSize];
> > > float diameter;
> > > float weight;
> > > };
> > >
> > > int main()
> > > {
> > > cout << "Welcome to William Wingate Pizza
> > > Analysis Service!
";
> > > cout << "How many pizza do you want to
> > > make?
";
> > >
> > > /////////////////
> > > int quantity;
> > > (cin >> quantity).get();
> > > pizza * guest=new pizza[quantity];
> > > /////////////////
> > >
> > >
> > >
>
>///////////////////////////////////////////////////////////
> > > for (int i=1; i<=quantity; i++)
> > > {
> > > cout << "Enter number " << i << " pizza
> > > statistics!
";
> > > cout << "Enter pizza name : ";
> > > cin.getline(guest[i]->name,nameSize);
> > > cout << "Enter pizza diameter : ";
> > > cin >> guest[i]->diameter;
> > > cout << "Enter pizza weight : ";
> > > (cin >> guest[i]->weight).get();
> > > }
> > >
> > >
>
>///////////////////////////////////////////////////////////
> > > cout << "
Here is your pizza
> statistics!
";
> > >
> > >
>
>///////////////////////////////////////////////////////////
> > > for (int i=1; i<=quantity; i++)
> > > {
> > > cout << "Pizza number " << i << endl;
> > > cout << "Name : " << guest[i]->name
> <<
> > > endl;
> > > cout << "Diameter : " <<
> guest[i]->diameter
> > > << endl;
> > > cout << "Weight : " <<
> guest[i]->weight
> > > << endl;
> > > }
> > >
> > >
>
>///////////////////////////////////////////////////////////
> > > getch();
> > > return 0;
> > > }
> > >
> > > Thank you and Happy New Year
> > >
> > >
> > >
> > >
> > >
> > >
> >
> >Hi Binit,
> >
> >Firstly you didnt mention what problem are you
> facing
> >with the code.. (compiler error.. runtime error? )
> >
> >The loop range you are using is from 1 to
> <quantity>.
> >It should be from 0 to <quantity -1>.
> >
> >Regards
> >Manik
> >
> >
> >
> >
> >__________________________________
> >Do you Yahoo!?
> >Send holiday email and support a worthy cause. Do
> good.
>
><http://celebrity.mail.yahoo.com>http://celebrity.mail.yahoo.com
> >
> >
> >
> >
> >
> >To unsubscribe :
> programmers-town-unsubscribe@yahoogroups.com
> >
> >
> >
> >
> >
> >----------
> >Yahoo! Groups Links
> > * To visit your group on the web, go to:
> > *
> >
>
<http://groups.yahoo.com/group/Programmers-Town/>http://groups.yahoo.com/group/P
rogrammers-Town/
>
> >
> > *
> > * To unsubscribe from this group, send an email
> to:
> > *
> >
>
<mailto:Programmers-Town-unsubscribe@yahoogroups.com?subject=Unsubscribe>Program
mers-Town-unsubscribe@yahoogroups.com
>
> >
> > *
> > * Your use of Yahoo! Groups is subject to the
> > <http://docs.yahoo.com/info/terms/>Yahoo! Terms of
> Service.
>
>
>
*************************************************************************
> Laughing at our mistakes can lengthen our own life.
> Laughing at someone else`s can shorten it.
>
> ---Cullen Hightower---
>
************************************************************************
>




__________________________________
Do you Yahoo!?
Send holiday email and support a worthy cause. Do good.
http://celebrity.mail.yahoo.com
Reply with quote
Send private message
View user's profile Post To page top
hassancyrk99 Posted: Mon Jan 03, 2005 12:59 pm


Joined: 03 Jan 2005

Posts: 1
Mixing struct, pointer and array.
Hi, did u get it to work , if so please let me know with running code.
thanks, arashmanik singhal <maniksinghal@...> wrote:
Ya, the compiler is right (obviously it had to beright)The statement: pizza * guest = new pizza[quantity];allocates an array of pizza and makes guest pointerpoint to it.Therefore guest[i] is not a pointer but a pizzaelement.Therefore use the dot (.) operator instead of ->guest[i].name ....RegardsManik--- Hardy Huang <blodstone@...> wrote:> It is a compiler error.> It says:> > base operand of `->` has non-pointer type `pizza`> > > At 01:33 AM 1/2/2005, you wrote:> > > >--- Hardy Huang <blodstone@...> wrote:> >> > >> > > Hai,> > >       I`m trying to use pointer, array and> struct> > > together. I`m a
newbie.> > > Here is my code. I still don`t know what`s wrong> > > with it. So guys plz help> > > me out.> > >> > > #include <iostream>> > > using namespace std;> > > #include <conio.h>> > >> > > const int nameSize = 20;> > > struct pizza> > > {> > >      char name[nameSize];> > >      float diameter;> > >      float weight;> > > };> > >> > > int main()> > > {> > >      cout << "Welcome to William Wingate Pizza> > > Analysis Service!
";> > >      cout << "How many pizza do you want to> > > make?
";> > >> >
>      /////////////////> > >      int quantity;> > >      (cin >> quantity).get();> > >      pizza * guest=new pizza[quantity];> > >      /////////////////> > >> > >> > >>>///////////////////////////////////////////////////////////> > >      for (int i=1; i<=quantity; i++)> > >      {> > >          cout << "Enter number " << i << " pizza> > > statistics!
";> > >          cout << "Enter pizza name     : ";> > >         
cin.getline(guest[i]->name,nameSize);> > >          cout << "Enter pizza diameter : ";> > >          cin >> guest[i]->diameter;> > >          cout << "Enter pizza weight   : ";> > >          (cin >> guest[i]->weight).get();> > >      }> > >> > >>>///////////////////////////////////////////////////////////> > >      cout << "
Here is your pizza> statistics!
";> > >> > >>>///////////////////////////////////////////////////////////> > >      for (int i=1; i<=quantity; i++)> >
>      {> > >          cout << "Pizza number " << i << endl;> > >          cout << "Name     : " << guest[i]->name> <<> > > endl;> > >          cout << "Diameter : " <<> guest[i]->diameter> > > << endl;> > >          cout << "Weight   : " <<> guest[i]->weight> > > << endl;> > >      }> > >> > >>>///////////////////////////////////////////////////////////> > >      getch();> > >      return 0;> >
> }> > >> > > Thank you and Happy New Year> > >> > >> > >> > >> > >> > >> >> >Hi Binit,> >> >Firstly you didnt mention what problem are you> facing> >with the code.. (compiler error.. runtime error? )> >> >The loop range you are using is from 1 to> <quantity>.> >It should be from 0 to <quantity -1>.> >> >Regards> >Manik> >> >> >> >> >__________________________________> >Do you Yahoo!?> >Send holiday email and support a worthy cause. Do> good.>><http://celebrity.mail.yahoo.com>http://celebrity.mail.yahoo.com> >> >> >>
>> >> >To unsubscribe :> programmers-town-unsubscribe@yahoogroups.com> >> >> >> >> >> >----------> >Yahoo! Groups Links> >    * To visit your group on the web, go to:> >    * > >><http://groups.yahoo.com/group/Programmers-Town/>http://groups.yahoo.com/group/Programmers-Town/> > >> >    *> >    * To unsubscribe from this group, send an email> to:> >    * > >><mailto:Programmers-Town-unsubscribe@yahoogroups.com?subject=Unsubscribe>Programmers-Town-unsubscribe@yahoogroups.com> > >> >    *> >    * Your
use of Yahoo! Groups is subject to the > > <http://docs.yahoo.com/info/terms/>Yahoo! Terms of> Service.> > >*************************************************************************> Laughing at our mistakes can lengthen our own life.> Laughing at someone else`s can shorten it.> > ---Cullen Hightower--->************************************************************************>             __________________________________ Do you Yahoo!? Send holiday email and support a worthy cause. Do good. http://celebrity.mail.yahoo.comTo unsubscribe : programmers-town-unsubscribe@yahoogroups.com__________________________________________________Do You Yahoo!?Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com
Reply with quote
Send private message
View user's profile Post To page top
blodstone2v Posted: Mon Jan 03, 2005 6:26 pm


Joined: 31 Dec 2004

Posts: 3
Mixing struct, pointer and array.
Thank You Manik. Now I can compile and run the code without any
problem.
Regards
Hardy
At 10:18 AM 1/3/2005, you wrote:
Ya, the compiler is right
(obviously it had to be
right)
The statement:
pizza * guest = new pizza[quantity];
allocates an array of pizza and makes guest pointer
point to it.
Therefore guest[i] is not a pointer but a pizza
element.
Therefore use the dot (.) operator instead of ->
guest[i].name ....
Regards
Manik

--- Hardy Huang <blodstone@...> wrote:
> It is a compiler error.
> It says:
>
> base operand of `->` has non-pointer type `pizza`
>
>
> At 01:33 AM 1/2/2005, you wrote:
>
>
> >--- Hardy Huang <blodstone@...> wrote:
> >
> > >
> > > Hai,
> > >       I`m trying to use
pointer, array and
> struct
> > > together. I`m a newbie.
> > > Here is my code. I still don`t know what`s wrong
> > > with it. So guys plz help
> > > me out.
> > >
> > > #include <iostream>
> > > using namespace std;
> > > #include <conio.h>
> > >
> > > const int nameSize = 20;
> > > struct pizza
> > > {
> > >      char name[nameSize];
> > >      float diameter;
> > >      float weight;
> > > };
> > >
> > > int main()
> > > {
> > >      cout << "Welcome
to William Wingate Pizza
> > > Analysis Service!
";
> > >      cout << "How many
pizza do you want to
> > > make?
";
> > >
> > >      /////////////////
> > >      int quantity;
> > >      (cin >>
quantity).get();
> > >      pizza * guest=new
pizza[quantity];
> > >      /////////////////
> > >
> > >
> > >
>
>///////////////////////////////////////////////////////////
> > >      for (int i=1;
i<=quantity; i++)
> > >      {
> > >          cout
<< "Enter number " << i << " pizza
> > > statistics!
";
> > >          cout
<< "Enter pizza name     : ";
> > >         
cin.getline(guest[i]->name,nameSize);
> > >          cout
<< "Enter pizza diameter : ";
> > >          cin
>> guest[i]->diameter;
> > >          cout
<< "Enter pizza weight   : ";
> > >          (cin
>> guest[i]->weight).get();
> > >      }
> > >
> > >
>
>///////////////////////////////////////////////////////////
> > >      cout << "
Here
is your pizza
> statistics!
";
> > >
> > >
>
>///////////////////////////////////////////////////////////
> > >      for (int i=1;
i<=quantity; i++)
> > >      {
> > >          cout
<< "Pizza number " << i << endl;
> > >          cout
<< "Name     : " <<
guest[i]->name
> <<
> > > endl;
> > >          cout
<< "Diameter : " <<
> guest[i]->diameter
> > > << endl;
> > >          cout
<< "Weight   : " <<
> guest[i]->weight
> > > << endl;
> > >      }
> > >
> > >
>
>///////////////////////////////////////////////////////////
> > >      getch();
> > >      return 0;
> > > }
> > >
> > > Thank you and Happy New Year
> > >
> > >
> > >
> > >
> > >
> > >
> >
> >Hi Binit,
> >
> >Firstly you didnt mention what problem are you
> facing
> >with the code.. (compiler error.. runtime error? )
> >
> >The loop range you are using is from 1 to
> <quantity>.
> >It should be from 0 to <quantity -1>.
> >
> >Regards
> >Manik
Reply with quote
Send private message
Post new topic Reply to topic
Display posts from previous:   
 

All times are GMT
Page 1 of 1
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Freelace Website Designer - Customer web design and software building.
Booking Calendar - reservation calendar script
Land Surveying - total station instruments and equipments
China Wholesale - Electronics Products
Character Studio - Tutorials and Help