I'm trying to use a do...while within a do...while in my program...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • strangetorpedo
    New Member
    • Oct 2009
    • 2

    I'm trying to use a do...while within a do...while in my program...

    The problem lies under where if (choice == '2') is...I need the program to keep prompting for num and den values as long as the user enters values less than or equal to 0. Once they are values greater than zero, the program is supposed to carry on and calculate the log, etc. HELP?!

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    #include <math.h>
    #define PI 3.14159
    
    
    
    int main(void)
    {
    	
    	/*   Declare required variables.
    		choice will represent the user's menu selection choice
    		angle will represent the angle used to calculate the tangent
    		rad will represent the angle in radians
    		tang will represent the calculated tangent of the angle
    		logarithm will represent the log10 of the ratio
    		num represents the entered value for the numerator of the ratio
    		den represents the entered value for the denominator of the ratio*/
    	
    	char choice;
    	double angle, rad, tang, ratio, logarithm, num, den;
    	
    	
    	/*  Print a description of what the program will do.								*/
    	printf("This program will invite the user to make a selection from a menu\nand then " 
    	"will either find the tangent of an angle, find the logarithm of a \nratio, or quit "
    	"the program.\n\n");
    
    	
    	/* Repeat until user wants to quit.	*/
    	
    	do
    	{
    	
    	/*Display menu and wait for valid choice. */
    	printf("Please Choose an Option. \n");
    	printf("1. Find the Tangent of an Angle. \n");
    	printf("2. Find the Logarithm of a Ratio. \n");
    	printf("3. Quit the Program. \n");
    
    	while ((choice = getch()) > '3' || choice < '1');
    
    	/*Process the choice if it was not a quit. */
    
    	if (choice == '1')
    
    	/*Prompt for an angle in degrees. */
    	
    	{  	printf("\n\nEnter an angle in degrees (0 to 360).\n=> ");
    		scanf("%lf", &angle);
    	
    	/*Convert the angle from degrees to radians, and calculate the tangent of the angle. */
    
    		rad = (PI/180) * angle;
    		tang = tan(rad); 
    
    	/* Display the angle in degrees, radians, and its calculated tangent. */
    		printf("The angle in degrees is %3.2f.\n", angle);
    		printf("The angle in radians is %3.2f.\n", rad);
    		printf("The calculated tangent of the angle is %3.2f.\n", tang);
    		printf("Press Any Key to Clear Screen.");
    	}	while (!kbhit());
    		system("cls");
    
    
    	if (choice == '2')
    		
    		/*Prompt for non-zero and positive numerator and denominator values. */
    		
    	{		do {printf("\n\Please enter non-zero and positive numerator and denominator values (seperated \nby comma).\n =>");
    			scanf("%lf,%lf", &num, &den);
    	}while ((num < '0') && (den < '0'));
    	
    
    		/*Calculate the log10 value of the ratio. */
    
    			ratio = num/den;
    			logarithm = log10(ratio);
    
    	/*Display the numerator, denominator, and the calculated value of the log. */
    
    			printf("The value of the numerator is %5.2f.\n", num);
    			printf("The value of the denominator is %5.2f.\n", den);
    			printf("The calculated value for log10 of the ratio is %5.2f.\n", logarithm);
    			printf("Press any key to clear the screen.\n");
    			}while (!kbhit());
    			system("cls");
    
    	
    	/*If choice is 3, quit program and display credits. */	
    	}	while (choice != '3');
    		printf("\n\nThis output has been generated by the program: tangent.cpp. \n\n"
    		   "This program was written by Shelby Moore.\n\n"); 
    
    	return 0;
    	
    		
    	}
    Last edited by Banfa; Oct 14 '09, 08:28 AM. Reason: Added [code] ... [/code] tags
  • newb16
    Contributor
    • Jul 2008
    • 687

    #2
    You compare double with '0' that is most likely to be ascii value of 48.

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      Also you test for den<0 but later you use den as the denominator in a division. This leaves the option of den == 0 and a divide by zero error.

      Comment

      • strangetorpedo
        New Member
        • Oct 2009
        • 2

        #4
        Thanks, I figured it out.

        Comment

        Working...