954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

how to use pointers in struct

[TEX]can someone teach me how to use pointer in this program??
i dont know if what i did in this program is correct. can you tell me how to use pointers here.thanks!
[/TEX]

#include<stdio.h>
#include<iostream.h>


struct fruits
{
	int apple;
	int banana;
	int orange;
	int jackfruit;

} fruit;

	int shape;
	int color;
	int texture;

void main()
{
	struct fruits *pfruit;
	pfruit=&fruit;

	cout<<"Enter the number of your choice for shape: [0] round/ [1] oblong"<<endl;
	cin>>inputFruits();
	cout<<"Enter the number of your choice for color: [0] green/ [1] yellow/ [2] red/ [3] orange"<<endl;
	cin>>inputFruits();
	cout<<"Enter the number of your choice for texture: [0] smooth/ [1] rough"<<endl;
	cin>>inputFruits();

}

void inputFruits(struct fruits *pfruit)
{
	if(shape==0)
	{
		cout<<"round"<<endl;
		cin>>shape;
	}
	else
	{
		cout<<"oblong"<<endl;
		cin>>shape;
	}
	if(color==0)
	{
		cout<<"green"<<endl;
		cin>>color;
	}
	if(color==1)
	{
		cout<<"yellow"<<endl;
		cin>>color;
	}
	if(color==2)
	{
		cout<<"red"<<endl;
		cin>>color;
	}
	if(color==3)
	{
		cout<<"orange"<<endl;
		cin>>color;
	}
	if(texture==0)
	{
		cout<<"smooth"<<endl;
		cin>>texture;
	}
	else
	{
		cout<<"rough"<<endl;
		cin>>texture;
	}
	
}

void identifyFruits(struct fruits *pfruit)
{
	if(shape==0)
	{
		if(color==2)
		{
			if(texture==0)
			{
				cout<<"Apple"<<endl;
				cin>>pfruit->apple;
	}}}

	if(shape==1)
		{
			if(color==1)
			{	
				if(texture==0)
				{
					cout<<"Banana"<<endl;
					cin>>pfruit->banana;
	}}}

	if(shape==0)
		{
			if(color==3)
			{
				if(texture==0)
				{
					cout<<"Orange"<<endl;
					cin>>pfruit->orange;
	}}}
	if(shape==1)
	{
		if(color==0)
		{
			if(texture==1)
			{
				cout<<"Jackfruit"<<endl;
				cin>>pfruit->jackfruit;
	}}}


}
abby2589
Light Poster
33 posts since Jan 2009
Reputation Points: 8
Solved Threads: 0
 

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

Freaky_Chris
Master Poster
702 posts since Apr 2008
Reputation Points: 325
Solved Threads: 118
 

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

abby2589
Light Poster
33 posts since Jan 2009
Reputation Points: 8
Solved Threads: 0
 

>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/should-i-use-void-main-or-int-main-or.html

use #include<iostream> and #include<cstdio>
Use a new compiler not a old one( http://cppdb.blogspot.com/2008/10/why-you-shouldnt-use-you-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.

siddhant3s
Practically a Posting Shark
816 posts since Oct 2007
Reputation Points: 1,486
Solved Threads: 140
 

...

Please Be specific.

...

Secondly, DO NOT USE VOID MAIN() http://cppdb.blogspot.com/2009/02/should-i-use-void-main-or-int-main-or.html

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

...

Totally agreed with that !!

tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 

Just, declare a pointer as you always do ...
* ;
e.g.: int * ptr;

tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You