GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 28-Jun-2006, 19:06
Cecil Cecil is offline
New Member
 
Join Date: May 2006
Posts: 10
Cecil is on a distinguished road

Pass by references?


Hello, long time no post!

Anyways, I had some questions concerning pass by references. I want to create a list of functions that will be displayed in a menu. For example:

Sample Run
1 Cylinder Menu
2 Sphere Menu
3 Cube Menu
4 quit
1
1 Define a cylinder
2 Surface Area
3 Circumference
4 Volume
5 back to Main Menu
2
You have not defined a cylinder yet. Try again.
1 Define a cylinder
2 Surface Area
3 Circumference
4 Volume
5 back to Main Menu
1
What is the radius and the height: 3 4
Done!
1 Define a cylinder
2 Surface Area
3 Circumference
4 Volume
5 back to Main Menu
4
The cylinder’s volume is 112.




I'll go into detail about the cylander menu:



Define a cylinder: asks the user for the radius and the height of the cylinder. Note that pass by reference must be used to reflect the change the user makes to these variables to be passed to the caller of this function: Cylinder Menu.

Surface Area: if a cylinder is already defined since the last time the user entered into the Cylinder Menu, then this will compute and print the surface area of the defined cylinder. Refer to Sample Run section to see the scope of a defined cylinder. If a cylinder is not defined yet, it will give a proper error message.

Circumference:
if a cylinder is already defined since the last time the user entered into the Cylinder Menu, then this will compute and print the circumference of the defined cylinder. If a cylinder is not defined yet, it will give a proper error message.

Volume: if a cylinder is already defined since the last time the user entered into the Cylinder Menu, then this will compute and print the volume of the defined cylinder. If a cylinder is not defined yet, it will give a proper error message.




I'm having some trouble putting this all together. I'm just learning about pointers, and I know that pointers will be involved in this.

Here is an example of the code I've made so far which includes the main menu, and the the cylander function (which works fine so far as I know) and the two functions to be called within the cylander menu that I am having trouble with.
CPP / C++ / C Code:
int main() 

	{

	    int choice,dummy;
	    menu();
	    scanf("%d", &choice);
	 
	    while (choice != 4)
		  {
	
	         if (choice == 1)
	              cylander();
					  
	         else if (choice == 2)
	              sphere();
					  
	         else if (choice == 3)
	              cube();
					  
	         else if (choice != 4)
	              printf("Sorry, please enter your choice again.\n");
	     
	         menu();
	         scanf("%d", &choice);
   	  }
	}//end main



/*                       --------+++ Functions List +++-------                   */

void menu()
	{
		printf("1  Cylander Menu.\n2  Sphere Menu.\n3  Cube Menu.\n4  Quit.\n");   // Main Menu
	}

void cylander()																						// Cylander Menu****
	{

    int choice=0, radius = -1 , height = -1;
				 
    while (choice != 5)
	  {
 			printf("\n1  Define a Cylander.\n2  Surface Area.\n3  Circumference.\n4  Volume.\n5  Back to Main Menu.\n"); 
    		scanf("%d", &choice);
			
	         if (choice == 1)
	              define_cylander(&radius, &height);
					  
	         else if (choice == 2)
	              surface_area(&radius, &height);
					  
	         else if (choice == 3)
	              circumference();
					  
	         else if (choice == 4)
	              volume();
					  				
	         else if (choice != 5)
	              printf("Sorry, please enter your choice again.\n");
     }
	  
	}
	
void define_cylander(int *x, int *y)																	// Define Cylander
	{
		int radius = 0, height = 0;

			printf("Enter your radius and height:");
			scanf("%d %d",x,y);
		*x = radius;
		*y = height;
			printf("radius = %d  height = %d\n", radius, height);
	}
	
void surface_area(int radius, int height)																		// Surface Area
	{
	//	x = radius;   (might not work, commented out for now)
	//	y = height;
		if (radius != -1 && height != -1)
			printf("\ncalculations here.  %d %d\n", radius, height);
		
		else 
			printf ("\nPlease define your cylander!\n");
	}



Here is a copy/paste of this code in action:

----Hit any key to start.
1 Cylander Menu.
2 Sphere Menu.
3 Cube Menu.
4 Quit.
1

1 Define a Cylander.
2 Surface Area.
3 Circumference.
4 Volume.
5 Back to Main Menu.
1
Enter your radius and height:2 3
radius = 0 height = 0

1 Define a Cylander.
2 Surface Area.
3 Circumference.
4 Volume.
5 Back to Main Menu.



I'm probally thinking the wrong direction for the pointers. Before I dig myself into a deeper hole, I figured I'd ask you guys for any insight on this problem. Thanks in advance, you all have helped me a lot in the past and I really appreciate it. It has made learning C a lot easier *so far*.

I'd hate to push you guys, but please respond asap though, I'm in a time crunch at the moment

Thank you very much once again
  #2  
Old 28-Jun-2006, 19:28
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 927
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: Pass by references?


Hi Cecil,

The error is in here:
CPP / C++ / C Code:
void define_cylander(int *x, int *y)																	// Define Cylander
	{
		int radius = 0, height = 0;

			printf("Enter your radius and height:");
			scanf("%d %d",x,y);

// Here:
		*x = radius;
		*y = height;
		

You are getting the input from the user as x, and y. Look at the scanf again.

Now, you are again overwriting it by using radius and height!


Regards,
Paramesh.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
  #3  
Old 28-Jun-2006, 20:42
Cecil Cecil is offline
New Member
 
Join Date: May 2006
Posts: 10
Cecil is on a distinguished road

Re: Pass by references?


Alright, I changed that function to get rid of those statements, it now looks like this:

CPP / C++ / C Code:
void define_cylander(int *x, int *y)
	{
		int radius = 0, height = 0;

			printf("Enter your radius and height:");
			scanf("%d %d",x,y);
			printf("radius = %d  height = %d\n", x, y);
	}

Here is what now happens:


----Hit any key to start.
1 Cylander Menu.
2 Sphere Menu.
3 Cube Menu.
4 Quit.
1

1 Define a Cylander.
2 Surface Area.
3 Circumference.
4 Volume.
5 Back to Main Menu.
1
Enter your radius and height:2 3
radius = 37813564 height = 37813560

1 Define a Cylander.
2 Surface Area.
3 Circumference.
4 Volume.
5 Back to Main Menu.



Yikes.

Now I know that I'm far from discovering the solution to this. Once I fix this one function, I'll then need to find out how to:

retreive this function's values and make the second function (cylander menu option 2) read the first function's output.

Hmmm...
  #4  
Old 29-Jun-2006, 01:01
Cecil Cecil is offline
New Member
 
Join Date: May 2006
Posts: 10
Cecil is on a distinguished road

Re: Pass by references?


I figured it out! If anybody is interested in knowing, here is the code. Once again I apologize for the horizontal heaviness!

CPP / C++ / C Code:
// Neil M
// COP3223
// Assignment 3
// 6/26/06
// Shapes Menu

#define PI 3.1
#include <stdio.h>
#include <math.h>

void menu();

void cylinder();
void sphere();
void cube();

int define_cylinder(int *x, int *y);
int cylinder_surface_area();
int cylinder_circumference();
int cylinder_volume();

int define_sphere_radius();
int define_sphere_volume();
int sphere_volume();

int define_cube();
int cube_surface_area();
int cube_volume();


int main() 

	{

	    int choice;
	    menu();
	    scanf("%d", &choice);
	 
	    while (choice != 4)
		  {
	
	         if (choice == 1)
	              cylinder();
					  
	         else if (choice == 2)
	              sphere();
					  
	         else if (choice == 3)
	              cube();
					  
	         else if (choice != 4)
	              printf("\nSorry, please enter your choice again.\n");
	     
	         menu();
	         scanf("%d", &choice);
   	  }
		  printf("\nThank you!");
	}//end main





								/*                       --------+++ Functions List +++-------                   */

void menu()
	{
		printf("1  Cylander Menu.\n2  Sphere Menu.\n3  Cube Menu.\n4  Quit.\n");   // Main Menu
	}
	
	
	
	
	

void cylinder()																						// Cylinder Menu****
	{

    int choice=0, r = -1 , h = -1;
				 
    while (choice != 5)
		  {
	 			printf("\n\n1  Define a Cylinder.\n2  Surface Area.\n3  Circumference.\n4  Volume.\n5  Back to Main Menu.\n"); 
	    		scanf("%d", &choice);
				
		         if (choice == 1)
		              define_cylinder(&r, &h);
						  
		         else if (choice == 2)
						{
							if (cylinder_surface_area(r, h) != 0)
						 		printf("\nThe surface area of the cylinder is %d.\n",cylinder_surface_area(r, h));
							else
								printf("\n");
						}
						  
		         else if (choice == 3)
						{
							if (cylinder_circumference(r, h) != 0)
					    	   printf("\nThe circumference of the cylinder is %d.\n",cylinder_circumference(r, h));
							else
								printf("\n");
						}
						  
		         else if (choice == 4)
						{
							if (cylinder_volume(r, h) != 0)
						  		printf("\nThe volume of the cylinder is %d.\n",cylinder_volume(r, h));
							else
								printf("\n");
						}
						  				
		         else if (choice != 5)
		              printf("\nSorry, please enter your choice again.\n");
	     }
		r = -1;
		h = -1; 
	}
	
	
	
int define_cylinder(int *x, int *y)																	// Define Cylinder
	{

			printf("Enter your radius and height:");
			scanf("%d %d",x,y);
			printf("Done!\n");
	}
	
	
	
int cylinder_surface_area(int r, int h)																									// Surface Area
	{
		int cylinder_sa;
		if (r == -1 && h == -1)
			{
				printf("\nEnter your radius and height first!\n");
				return 0;
			}
		else if (r != -1 && h != -1)
			{
				cylinder_sa = ((2*PI*r*r)+(2*PI*r*h));
				return cylinder_sa;
			}
	}
	
	
int cylinder_circumference(int r, int h)																									// Circumference
	{
		int cylinder_circ;
		if (r == -1)
			{
				printf("\nEnter your radius and height first!\n");
				return 0;
			}
		else if (r != -1)
			{
				cylinder_circ = (2*PI*r);
				return cylinder_circ;
			}
	}
		
		
int cylinder_volume(int r, int h)																											// Volume
	{
		int cylinder_vol;
		if (r == -1 && h == -1)
			{
				printf("\nEnter your radius and height first!\n");
				return 0;
			}
		else if (r != -1 && h != -1)
			{
				cylinder_vol = (PI*r*r*h);
				return cylinder_vol;
			}
	}
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
void sphere()																							// Sphere Menu
	{

    int choice = 0, sr = -1, lastvolume, x;
	 int svolume = 1, sradius = 1;
	 
	 
	 
	    while (choice != 4)
		  {
		  
			 printf("\n\n1  Define a Sphere with radius.\n2  Define a Sphere with volume.\n3  Volume.\n4  Back to Main Menu.\n"); 
		    scanf("%d", &choice);
				 
		         if (choice == 1)
						{
						  sr = define_sphere_radius(sradius);
						}
		         else if (choice == 2)
						{
						  sr = define_sphere_volume(svolume);
						}
		         else if (choice == 3)
						{
							if (sphere_volume(sr) != 0)
								printf("\nThe volume of the sphere is %d.\n", sphere_volume(sr));
							else
								printf("\n");

						}
						  
		         else if (choice != 4)
		              printf("\nSorry, please enter your choice again.\n");
						  
					  
	     }
		sr = -1;
	}
	
	
int define_sphere_radius(int sradius)																									// Sphere Radius define
	{

	
		printf("\nWhat is the desired radius?\n");
		scanf("%d",&sradius);
			
			return sradius;
	}
	
	
int define_sphere_volume(int svolume)																									// Sphere Volume to radius define
	{
 		int sradius;
	
		printf("\nWhat is the desired volume?\n");
		scanf("%d",&svolume);
		
			sradius = pow((3*svolume)/(4*PI),0.33);
			printf("Created a sphere with radius %d as the best match.\n\n", sradius);
			
			return sradius;
	}
	
	
int sphere_volume(int sr)																								 					// Sphere Volume calculation
	{
		int svolume;
			if (sr == -1)
				{
					printf("\nDefine your sphere first!\n");
					return 0;
				}
			else if (sr != -1)
				{
					svolume = (1.33)*(PI)*(sr*sr*sr);
					return svolume;
				}
	}

	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
void cube()																													// Cube Menu
	{

    int choice = 0, cs = -1;
	 int cside, csa = -1, cvol = -1;
    while (choice != 4)
	 
	  {
			 printf("\n\n1  Define a Cube.\n2  Surface Area.\n3  Volume.\n4  Back to Main Menu.\n"); 
		    scanf("%d", &choice);
			 
	         if (choice == 1)
					  cs = define_cube(cside);
					  
	         else if (choice == 2)
						{	
							if (cube_surface_area(cs) != 0)						
							printf("\nThe surface area of the cube is %d.\n", cube_surface_area(cs));
							else
							printf("\n");
						}	        
				 else if (choice == 3)
				 		{
							if (cube_volume(cs) != 0)
							printf("\nThe volume of the cube is %d.\n", cube_volume(cs));
							else
							printf("\n");
						}
					  
	         else if (choice != 4)
	              printf("Sorry, please enter your choice again.\n");

     }
	  cs = -1;
	}
	
int define_cube(int cside)																			// Define the Cube
	{

		printf("\nWhat is the legnth of a side of the cube?\n");
		scanf("%d",&cside);
	
			return cside;
	}
	
	
int cube_surface_area(cs)																			// Cube Surface Area
	{
		int cube_sa;
		if (cs == -1)
			{
				printf("\nEnter your cube side first!\n");
				return 0;
			}
		else if (cs != -1)
			{
				cube_sa = (6*cs*cs);
				return cube_sa;
			}
	}
	
	
int cube_volume(cs)																					// Cube Volume
	{
	int cube_vol;
	if (cs == -1)
		{
			printf("\nEnter your cube side first!\n");
			return 0;
		}
	else if (cs != -1)
		{
			cube_vol = (cs*cs*cs);
			return cube_vol;
		}
	}



If you want the .c file, attached is the .zip file containing it.

Turns out it was as simple as I thought. Functions are fun!
Attached Files
File Type: zip Menu Functions.zip (1.7 KB, 5 views)
  #5  
Old 29-Jun-2006, 02:30
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,244
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Pass by references?


Quote:
Originally Posted by Cecil
I figured it out! If anybody is interested in knowing, here is the code. Once again I apologize for the horizontal heaviness!
Never apologize for something easily fixed -- especially apologizing more than once...

Simply stop using multiple tabs when a couple spaces would suffice. It will make your code easier to read. Reading this should help.
__________________

Age is unimportant -- except in cheese
 
 

Recent GIDBlogBelkin Laptop Cooler by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How do you SAFELY pass hidden variables through merchant account payment screens? mrsurrey eCommerce / Merchant Account Forum 4 03-Jul-2006 15:28
References to base class to access derived class only members mirizar C++ Forum 3 03-Dec-2005 20:40
Cannot pass data between forms with enctype="multipart/form-data" attribute MisterJingo MySQL / PHP Forum 0 05-Aug-2005 04:40
Help wit my source code compiler errors Krandygrl00 C++ Forum 1 06-Jun-2005 08:14
vectors of references mirizar C++ Forum 1 12-Apr-2005 02:02

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 12:38.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.