Thread: loop problem

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    12

    loop problem

    I've got this program that seems to be working properly, but it keeps looping after it's done calculating the areas. Can anyone help me out with what's wrong with the loops?

    Code:
    /*Filename: Richacm2.c
    Program: Calculate Area
    */
    
    /*
    ---------------------------------------------------------
    Calculate the area for a rectangle, triangle, circle,
    	ellipse, and sphere.
    Use scanf() to retrieve values.
    Calculate the area.
    Use printf() to display.
    --------------------------------------------------------
    */
    
    
    
    #include <stdio.h>
    #include <math.h>
    #define PI 3.141593
    
    int main()
    {
    
    
    
    /* Declare and Initialize All Variables*/
    	float area_rectangle, base_rect, height_rect;
    	float	area_triangle, height_tri, base_tri; 
    	float	area_circle, radius_circ;
    	float	area_ellipse, length_a_ellipse, length_b_ellipse; 
    	float area_sphere, radius_sphere;
    	int option;
    
    do
    	{	
    	
    	printf("Please type 1, 2, 3, 4, 5, or 0\n"
    			"1. Rectangle\n"
    			"2. Triangle\n"
    			"3. Circle\n"
    			"4. Ellipse\n"
    			"5. Sphere\n"
    			"0. Quit\n");
    	scanf("%d", &option);
    	
    	
    	
    	
    	if(option==1)
    	{
    		/*
    		---------------------------------------------------------
    		Area of Rectangle
    		-------------------------------------------------------
    		*/	
    	
    		
    		/* Obtain Dimensions*/
    				printf("Enter base of rectangle: ");
    				scanf("%f",& base_rect);
    		
    				printf("Enter height of rectangle: ");
    				scanf("%f",& height_rect);
    		
    		/*Compute the Area*/
    				area_rectangle = base_rect * height_rect;
    				
    		/*Output the results*/
    				printf("Base =%5.2f\nHeight =%5.2f\nArea of the Rectangle = %5.2f\n",
    					base_rect, height_rect, area_rectangle);			
    		
    	}
    	else if (option==2)
    	{
    		/*
    		---------------------------------------------------------
    		Area of Triangle
    		-------------------------------------------------------
    		*/	
    		
    		/* Obtain Dimensions*/	
    				printf("Enter base of the triangle: ");
    				scanf("%f",& base_tri);
    				
    				printf("Enter height of the triangle: ");
    				scanf("%f",& height_tri);
    		
    		/*Compute the Area*/		
    				area_triangle = (base_tri * height_tri)/2;
    		
    		/*Output the results*/		
    				printf("Base =%5.2f\nHeight =%5.2f\nArea of the Triangle = %5.2f\n", 
    					base_tri, height_tri, area_triangle);
    	}
    	else if (option==3)
    	{		
    		/*
    		---------------------------------------------------------
    		Area of Circle
    		-------------------------------------------------------
    		*/	
    		
    		/* Obtain Dimensions*/
    				printf("Enter radius of the circle: ");
    				scanf("%f",& radius_circ);
    				
    		/*Compute the Area*/		
    				area_circle = pow(radius_circ,2) * PI;
    				
    		/*Output the results*/		
    				printf("Radius =%5.2f\nArea of the Circle =%5.2f\n",
    						radius_circ, area_circle);
    	}
    	else if (option==4)
    	{					
    		/*
    		---------------------------------------------------------
    		Area of Ellipse
    		-------------------------------------------------------
    		*/	
    		
    		
    		/* Obtain Dimensions*/
    				printf("Enter length A of the ellipse: ");
    				scanf("%f",& length_a_ellipse);
    				
    				printf("Enter length B of the ellipse: ");
    				scanf("%f",& length_b_ellipse);
    				
    		/*Compute the Area*/		
    				area_ellipse = PI * length_a_ellipse * length_b_ellipse;
    				
    		/*Output the results*/		
    				printf("Length A =%5.2f\nLength B =%5.2f\nArea of the Ellipse =%5.2f\n",
    						length_a_ellipse, length_b_ellipse, area_ellipse);	
    	}					
    	else if (option==5)
    	{
    		/*
    		---------------------------------------------------------
    		Surface Area of a Sphere
    		-------------------------------------------------------
    		*/	
    		
    		/* Obtain Dimensions*/
    				printf("Enter radius of the sphere: ");
    				scanf("%f",& radius_sphere);
    				
    		/* Compute the Area */
    				area_sphere = pow(radius_sphere,2) * 4 * PI;
    				
    		/* Output the Results */
    				printf("Radius =%5.2f\nSurface Area of the Sphere =%5.2f\n",
    						radius_sphere, area_sphere);
    	}					
    	else 
    	{
    		printf("Loop complete\n");
    	}
    	
    	printf("Please type 1, 2, 3, 4, 5, or 0\n"
    			"1. Rectangle\n"
    			"2. Triangle\n"
    			"3. Circle\n"
    			"4. Ellipse\n"
    			"5. Sphere\n"
    			"0. Quit\n");
    	scanf("%d", &option);
    	
    }while(option);
    
    do
    
    	scanf("%d", &option);
    
    
    
    while(option>0);
    				
    return 0;
    
    }
    Thanks.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Code:
    do
    
    	scanf("%d", &option);
    
    
    
    while(option>0);
    Well, this is completely useless...is that where it's getting stuck in the loop?
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    12
    What it's doing is... the menu comes up, I enter an option, it asks for dimensions, and then I give them to it. Then it calculates the area, and the menu pops back up. I want it to be done after it caclulates, not bring the menu back up.

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Follow the logic of the code. The loop logic says to keep looping as long as option is true (not 0). This means that a) you need to change the value of option somewhere inside the loop or b) you need to choose option 0 from the menu.

    You're also printing the menu twice and asking for input twice per loop which is pretty crazy. It's time to step back and rethink the overall structure of the program rather than just trying to hack away at it in different areas. Right now it's pretty messed up.
    Last edited by itsme86; 09-20-2005 at 10:21 AM.
    If you understand what you're doing, you're not learning anything.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    #define PI 3.141593
    You know that PI is 3.141592653.

    Try re-writing the loop. And put better comments in:
    Code:
    /* Declare and Initialize All Variables*/
    	float area_rectangle, base_rect, height_rect;
    	float	area_triangle, height_tri, base_tri; 
    	float	area_circle, radius_circ;
    	float	area_ellipse, length_a_ellipse, length_b_ellipse; 
    	float area_sphere, radius_sphere;
    	int option;
    I don't see any variable initialized.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Quote Originally Posted by dwks
    Code:
    #define PI 3.141593
    You know that PI is 3.141592653.
    Yes, but his is correctly rounded, yours isn't

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    15
    Quote Originally Posted by rhymewithnothin
    I want it to be done after it caclulates, not bring the menu back up.
    But since you are using a do-while loop, it is suppose to loop back to the menu.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Ugh, you're right: 3.141592654.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    ~Team work is the best!~ wakish's Avatar
    Join Date
    Sep 2005
    Posts
    85
    uuumm....one more thing; in cases where u need to do a menu driven program like urs, try to use "switch-cases"...it gives a better visual look and it's indented more properly..


    well. wait i will try to write and compile this work for u...i'll post the codes soon...

  10. #10
    ~Team work is the best!~ wakish's Avatar
    Join Date
    Sep 2005
    Posts
    85
    Here it is at last:
    (plz let me know any suggestions for any kind of improvements-tnx!)

    Code:
    
    //compiler :  Borland Turbo C++
    //programmer: wakish
    
    
    
    //libraries
    # include <stdio.h>
    # include <stdlib.h>	//for exit() function
    # include <conio.h>		//for clrscr() and getch() functions
    # include <dos.h>		//for delay() function
    
    
    //definition of constants used
    #define PI 3.141592654
    
    
    //function prototypes
    void goodbye();
    float areaRectangle(float,float);
    float areaTriangle(float,float);
    float areaCircle(float);
    float areaEllipse(float,float);
    float areaSphere(float);
    
    
    /***************MAIN***************/
    void main()
    {
    	//fields
    	float lengthRectangle, widthRectangle;
    	float baseTriangle, heightTriangle;
    	float radiusCircle, radiusSphere;
    	float lengthA, lengthB;
    	float area;
    	char option;
    
    	//to clearscreen
    	clrscr();
    
    	//this loop is to run infinitly for the main menu
    	do
    	{
    		printf("\n\n\t   Enter your option:   ");
    		printf("\n\n\tEXIT\t : Press 0");
    		printf("\n\n\tRectangle: Press 1");
    		printf("\n\tTriangle : Press 2");
    		printf("\n\tCircle\t : Press 3");
    		printf("\n\tEllipse\t : Press 4");
    		printf("\n\tSphere\t : Press 5");
    
    		//let user enter the option he wants
    		printf("\n\n\n\tYour option : ");
    
    		//read input directly without waiting for carriage return to be pressed
    		option = getche();
    		clrscr();
    
    		//to validate option
    		switch(option)
    		{
    			case '0':
    
    				//ask user for a confirmation for his previous option
    
    				printf("\n\n\n\n\n");	//just to leave a certain no. of lines
    
    				printf("\n\n\n      MAIN MENU\t  : Press 1");
    				printf("\n      EXIT\t  : Press Any Key");
    
    				printf("\n\n\n\tYour option:  ");
    				option = getche();
    
    				if(option != '1')
    				{
    					clrscr();
    					goodbye();
    				}
    
    				//to clearscreen
    				clrscr();
    
    				break;
    
    			case '1':
    
    					printf("\nEnter the length of rectangle: ");
    					scanf("%f",&lengthRectangle);
    
    					printf("\nEnter the width of rectangle: ");
    					scanf("%f",&widthRectangle);
    
    					area = areaRectangle(lengthRectangle,widthRectangle);
    
    					printf("\n\nSo, the area of Rectangle is: %f",area);
    
    				break;
    
    			case '2':
    
    					printf("\nEnter the base of triangle: ");
    					scanf("%f",&baseTriangle);
    
    					printf("\nEnter the height of triangle: ");
    					scanf("%f",&heightTriangle);
    
    					area = areaTriangle(baseTriangle,heightTriangle);
    
    					printf("\n\nSo, the area of Triangle is: %f",area);
    
    				break;
    
    			case '3':
    
    					printf("\nEnter the radius of Circle: ");
    					scanf("%f",&radiusCircle);
    
    					area = areaCircle(radiusCircle);
    
    					printf("\n\nSo, the area of Circle is: %f",area);
    
    				break;
    
    			case '4':
    
    					printf("\nEnter the lengthA of Ellipse: ");
    					scanf("%f",&lengthA);
    
    					printf("\nEnter the lengthA of Ellipse: ");
    					scanf("%f",&lengthB);
    
    					area = areaEllipse(lengthA,lengthB);
    
    					printf("\n\nSo, the area of Ellipse is: %f",area);
    
    				break;
    
    			case '5':
    
    					printf("\nEnter the radius of Sphere: ");
    					scanf("%f",&radiusSphere);
    
    					area = areaSphere(radiusSphere);
    
    					printf("\n\nSo, the area of Sphere is: %f",area);
    
    				break;
    
    			default:
    
    				printf("\n\nSorry! Invalid Request! try again! ");
    
    				delay(2500);
    				clrscr();
    
    				break;
    		}//end switch
    
    		printf("\n\n\n\n\n");	//just to leave a certain no. of lines
    
    
    	}while(1); //end while loop
    
    }//end main
    
    
    
    /***************FUNCTIONS***************/
    
    //to display goodbye
    void goodbye()
    {
    	printf("\n\nGOODBYE!");
    	delay(2500);
    	exit(0);
    }
    
    
    
    //to calculate area of Rectangle
    float areaRectangle(float l,float w)
    {
    	float areaR;
    
    	areaR = l * w;
    
    	return areaR;
    }
    
    
    //to calculate area of Triangle
    float areaTriangle(float b,float h)
    {
    	float areaT;
    
    	areaT = (b * h) / 2;
    
    	return areaT;
    }
    
    
    //to calculate area of Circle
    float areaCircle(float r)
    {
    	float areaC;
    
    	areaC = 2 * PI * r * r;
    
    	return areaC;
    }
    
    
    //to calculate area of Ellipse
    float areaEllipse(float a,float b)
    {
    	float areaE;
    
    	areaE = a * b * PI;
    
    	return areaE;
    }
    
    
    //to calculate of Sphere
    float areaSphere(float r)
    {
    	float areaS;
    
    	areaS = 4 * PI * r * r;
    
    	return areaS;
    }

  11. #11
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    void main()

    Well your going to get flammed because of this by a lot of people.

    getche();
    Why are you using this for reading a character. There are better choices...
    Mr. C: Author and Instructor

  12. #12
    ~Team work is the best!~ wakish's Avatar
    Join Date
    Sep 2005
    Posts
    85
    can u name those better choices???
    tnx!

    y not void? i dnt want main to return anything, that's all!

  13. #13
    kiddo
    Join Date
    Sep 2005
    Posts
    8
    the break in default is not required and everything else is pretty fine. try to use getch() instead of delay() as it irritates if thr is lot of things to b calculated.

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Always a good idea to say break, unless you specifically want fall-through behaviour
    Code:
    #include <stdio.h>
    
    int main ( int argc, char *argv[] ) {
      switch ( argc ) {
        default:
          printf( "This is default\n" );
        case 1:
          printf( "This is case 1\n" );
          break;
      }
      return 0;
    }
    It's not compulsory to have default at the end.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  15. #15
    ~Team work is the best!~ wakish's Avatar
    Join Date
    Sep 2005
    Posts
    85
    ok..but what's the purpose of the: int argc, char *argv[] inside the () of main???
    tnx!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Addition problem in loop
    By murjax in forum C Programming
    Replies: 3
    Last Post: 07-01-2009, 06:29 PM
  2. validation problem in a loop (newbie question)
    By Aisthesis in forum C++ Programming
    Replies: 11
    Last Post: 05-10-2009, 10:47 PM
  3. For Loop Problem
    By xp5 in forum C Programming
    Replies: 10
    Last Post: 09-05-2007, 04:37 PM
  4. Loop problem
    By Tesnik in forum C++ Programming
    Replies: 29
    Last Post: 08-23-2007, 10:24 AM
  5. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM