943,735 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 608
  • C++ RSS
Mar 12th, 2009
-2

how to use pointers in struct

Expand Post »
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 />

C++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 8
Solved Threads: 0
Light Poster
abby2589 is offline Offline
33 posts
since Jan 2009
Mar 12th, 2009
0

Re: how to use pointers in struct

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
Reputation Points: 325
Solved Threads: 118
Master Poster
Freaky_Chris is offline Offline
702 posts
since Apr 2008
Mar 12th, 2009
0

Re: how to use pointers in struct

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
Reputation Points: 8
Solved Threads: 0
Light Poster
abby2589 is offline Offline
33 posts
since Jan 2009
Mar 12th, 2009
1

Re: how to use pointers in struct

>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.
Reputation Points: 1486
Solved Threads: 140
Practically a Posting Shark
siddhant3s is offline Offline
816 posts
since Oct 2007
Mar 13th, 2009
0

Re: how to use pointers in struct

Click to Expand / Collapse  Quote originally posted by siddhant3s ...
...

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 !!
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Mar 15th, 2009
0

Re: how to use pointers in struct

Just, declare a pointer as you always do ...
<type> * <name>;
e.g.: int * ptr;
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Need help with visual C++
Next Thread in C++ Forum Timeline: how to do string input?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC