how to use pointers in struct

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2009
Posts: 29
Reputation: abby2589 is an unknown quantity at this point 
Solved Threads: 0
abby2589's Avatar
abby2589 abby2589 is offline Offline
Light Poster

how to use pointers in struct

 
-2
  #1
Mar 12th, 2009
can someone teach me how to use pointer in this program??<br />
i dont know if what i did in this program is correct. can you tell me how to use pointers here.thanks!<br />

  1. #include<stdio.h>
  2. #include<iostream.h>
  3.  
  4.  
  5. struct fruits
  6. {
  7. int apple;
  8. int banana;
  9. int orange;
  10. int jackfruit;
  11.  
  12. } fruit;
  13.  
  14. int shape;
  15. int color;
  16. int texture;
  17.  
  18. void main()
  19. {
  20. struct fruits *pfruit;
  21. pfruit=&fruit;
  22.  
  23. cout<<"Enter the number of your choice for shape: [0] round/ [1] oblong"<<endl;
  24. cin>>inputFruits();
  25. cout<<"Enter the number of your choice for color: [0] green/ [1] yellow/ [2] red/ [3] orange"<<endl;
  26. cin>>inputFruits();
  27. cout<<"Enter the number of your choice for texture: [0] smooth/ [1] rough"<<endl;
  28. cin>>inputFruits();
  29.  
  30. }
  31.  
  32. void inputFruits(struct fruits *pfruit)
  33. {
  34. if(shape==0)
  35. {
  36. cout<<"round"<<endl;
  37. cin>>shape;
  38. }
  39. else
  40. {
  41. cout<<"oblong"<<endl;
  42. cin>>shape;
  43. }
  44. if(color==0)
  45. {
  46. cout<<"green"<<endl;
  47. cin>>color;
  48. }
  49. if(color==1)
  50. {
  51. cout<<"yellow"<<endl;
  52. cin>>color;
  53. }
  54. if(color==2)
  55. {
  56. cout<<"red"<<endl;
  57. cin>>color;
  58. }
  59. if(color==3)
  60. {
  61. cout<<"orange"<<endl;
  62. cin>>color;
  63. }
  64. if(texture==0)
  65. {
  66. cout<<"smooth"<<endl;
  67. cin>>texture;
  68. }
  69. else
  70. {
  71. cout<<"rough"<<endl;
  72. cin>>texture;
  73. }
  74.  
  75. }
  76.  
  77. void identifyFruits(struct fruits *pfruit)
  78. {
  79. if(shape==0)
  80. {
  81. if(color==2)
  82. {
  83. if(texture==0)
  84. {
  85. cout<<"Apple"<<endl;
  86. cin>>pfruit->apple;
  87. }}}
  88.  
  89. if(shape==1)
  90. {
  91. if(color==1)
  92. {
  93. if(texture==0)
  94. {
  95. cout<<"Banana"<<endl;
  96. cin>>pfruit->banana;
  97. }}}
  98.  
  99. if(shape==0)
  100. {
  101. if(color==3)
  102. {
  103. if(texture==0)
  104. {
  105. cout<<"Orange"<<endl;
  106. cin>>pfruit->orange;
  107. }}}
  108. if(shape==1)
  109. {
  110. if(color==0)
  111. {
  112. if(texture==1)
  113. {
  114. cout<<"Jackfruit"<<endl;
  115. cin>>pfruit->jackfruit;
  116. }}}
  117.  
  118.  
  119. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 670
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: how to use pointers in struct

 
0
  #2
Mar 12th, 2009
Don't bother using text tags for yout text, thats not what its for. it stands for Latex, a Mathematical Notation Syntax.

Secondly, we don't know what your program is supposed to do and what it is or is not doing, so we can't help you.


Chris
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 29
Reputation: abby2589 is an unknown quantity at this point 
Solved Threads: 0
abby2589's Avatar
abby2589 abby2589 is offline Offline
Light Poster

Re: how to use pointers in struct

 
0
  #3
Mar 12th, 2009
Sorry..
In this program you will enter the number of your choice if 0 (round) or 1 (oblong) for the shape.
For the color 0(green), 1(yellow), 2(red), 3(orange)
For the texture 0(smooth) or 1(rough).
It will identify the type of fruit..
example if you input 0 for shape, 2 for color, and 0 for texture it will tell you that fruit is an apple
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 792
Reputation: siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of 
Solved Threads: 135
siddhant3s's Avatar
siddhant3s siddhant3s is offline Offline
Master Poster

Re: how to use pointers in struct

 
1
  #4
Mar 12th, 2009
>can someone teach me how to use pointer in this program??i don't know if what i did in this program is correct. can you tell me how to use pointers here.thanks!

Please Be specific.
This is not a teaching classroom dear. You must read your text book and come to us with very specific questions.
We, can tell you how to use pointers but how the hell are we suppose to explain you the whole program. Do you think it is feasible?

Secondly, DO NOT USE VOID MAIN() http://cppdb.blogspot.com/2009/02/sh...t-main-or.html

use #include<iostream> and #include<cstdio> Use a new compiler not a old one(http://cppdb.blogspot.com/2008/10/wh...use-old-c.html)

And thirdly, The program contains lot of (trivial) errors. We are not going to debug them for you. For your curiosity, there's a error in the cin>>inputfruits() You cannot cin to function.

So, please correct the errors and read the book. Then asks what is troubling you.
Last edited by siddhant3s; Mar 12th, 2009 at 9:03 pm.
Siddhant Sanyam
(Not posting much)
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: how to use pointers in struct

 
0
  #5
Mar 13th, 2009
Originally Posted by siddhant3s View Post
...

Please Be specific.

...

Secondly, DO NOT USE VOID MAIN() http://cppdb.blogspot.com/2009/02/sh...t-main-or.html

use #include<iostream> and #include<cstdio> Use a new compiler not a old one
(http://cppdb.blogspot.com/2008/10/wh...use-old-c.html)

...
Totally agreed with that !!
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: how to use pointers in struct

 
0
  #6
Mar 15th, 2009
Just, declare a pointer as you always do ...
<type> * <name>;
e.g.: int * ptr;
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC