473,748 Members | 8,530 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

2 New Member
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?!

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <stdlib.h>
  4. #include <math.h>
  5. #define PI 3.14159
  6.  
  7.  
  8.  
  9. int main(void)
  10. {
  11.  
  12.     /*   Declare required variables.
  13.         choice will represent the user's menu selection choice
  14.         angle will represent the angle used to calculate the tangent
  15.         rad will represent the angle in radians
  16.         tang will represent the calculated tangent of the angle
  17.         logarithm will represent the log10 of the ratio
  18.         num represents the entered value for the numerator of the ratio
  19.         den represents the entered value for the denominator of the ratio*/
  20.  
  21.     char choice;
  22.     double angle, rad, tang, ratio, logarithm, num, den;
  23.  
  24.  
  25.     /*  Print a description of what the program will do.                                */
  26.     printf("This program will invite the user to make a selection from a menu\nand then " 
  27.     "will either find the tangent of an angle, find the logarithm of a \nratio, or quit "
  28.     "the program.\n\n");
  29.  
  30.  
  31.     /* Repeat until user wants to quit.    */
  32.  
  33.     do
  34.     {
  35.  
  36.     /*Display menu and wait for valid choice. */
  37.     printf("Please Choose an Option. \n");
  38.     printf("1. Find the Tangent of an Angle. \n");
  39.     printf("2. Find the Logarithm of a Ratio. \n");
  40.     printf("3. Quit the Program. \n");
  41.  
  42.     while ((choice = getch()) > '3' || choice < '1');
  43.  
  44.     /*Process the choice if it was not a quit. */
  45.  
  46.     if (choice == '1')
  47.  
  48.     /*Prompt for an angle in degrees. */
  49.  
  50.     {      printf("\n\nEnter an angle in degrees (0 to 360).\n=> ");
  51.         scanf("%lf", &angle);
  52.  
  53.     /*Convert the angle from degrees to radians, and calculate the tangent of the angle. */
  54.  
  55.         rad = (PI/180) * angle;
  56.         tang = tan(rad); 
  57.  
  58.     /* Display the angle in degrees, radians, and its calculated tangent. */
  59.         printf("The angle in degrees is %3.2f.\n", angle);
  60.         printf("The angle in radians is %3.2f.\n", rad);
  61.         printf("The calculated tangent of the angle is %3.2f.\n", tang);
  62.         printf("Press Any Key to Clear Screen.");
  63.     }    while (!kbhit());
  64.         system("cls");
  65.  
  66.  
  67.     if (choice == '2')
  68.  
  69.         /*Prompt for non-zero and positive numerator and denominator values. */
  70.  
  71.     {        do {printf("\n\Please enter non-zero and positive numerator and denominator values (seperated \nby comma).\n =>");
  72.             scanf("%lf,%lf", &num, &den);
  73.     }while ((num < '0') && (den < '0'));
  74.  
  75.  
  76.         /*Calculate the log10 value of the ratio. */
  77.  
  78.             ratio = num/den;
  79.             logarithm = log10(ratio);
  80.  
  81.     /*Display the numerator, denominator, and the calculated value of the log. */
  82.  
  83.             printf("The value of the numerator is %5.2f.\n", num);
  84.             printf("The value of the denominator is %5.2f.\n", den);
  85.             printf("The calculated value for log10 of the ratio is %5.2f.\n", logarithm);
  86.             printf("Press any key to clear the screen.\n");
  87.             }while (!kbhit());
  88.             system("cls");
  89.  
  90.  
  91.     /*If choice is 3, quit program and display credits. */    
  92.     }    while (choice != '3');
  93.         printf("\n\nThis output has been generated by the program: tangent.cpp. \n\n"
  94.            "This program was written by Shelby Moore.\n\n"); 
  95.  
  96.     return 0;
  97.  
  98.  
  99.     }
  100.  
Oct 14 '09 #1
3 1768
newb16
687 Contributor
You compare double with '0' that is most likely to be ascii value of 48.
Oct 14 '09 #2
Banfa
9,065 Recognized Expert Moderator Expert
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.
Oct 14 '09 #3
strangetorpedo
2 New Member
Thanks, I figured it out.
Oct 15 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
15956
by: muser | last post by:
I have a logical error in my program, I have submitted the program and my tutor hasn't listed the other problems with the code, but said that the program won't run because of a while statement. The while statement is as follows. while(infile.peek() != EOF) { infile.getline(temp1, max); };
8
2091
by: Rich Grise | last post by:
I think I've finally found a tutorial that can get me started: http://www.zib.de/Visual/people/mueller/Course/Tutorial/tutorial.html and I've been lurking for awhile as well. What happened is, I've been playing with Qt in KDE on a Slackware 10.0 system, and following their cookbook exercises, and everything's working, and I have no clue what the code is doing, i.e.. how do I connect my brain to the ascii in the files to the stuff on...
5
1435
by: Simon Dean | last post by:
Hello, Im hoping you can help me. Im, probably not that advanced with PHP, well, I probably am, but with a memory like a sieve, I rarely take in information... But, Im just considering some OOP using PHP's Classes. Seems quite handy so far, already generated a Login class and it does
42
3154
by: Martin Jørgensen | last post by:
Hi, I'm trying to move a matlab program into c language. For those who knows matlab, this is the line I want to program in c: hx(1:nx,1:ny) = 0; % nx=10, ny=10 It works on a 2-dimensional array (size is 10*10), setting all the values inside the 10*10 matrix to zero. My C-function looks like this:
14
5904
by: dba_222 | last post by:
Dear experts, Again, sorry to bother you again with such a seemingly dumb question, but I'm having some really mysterious results here. ie. Create procedure the_test As
16
3532
by: Claudio Grondi | last post by:
Sometimes it is known in advance, that the time spent in a loop will be in order of minutes or even hours, so it makes sense to optimize each element in the loop to make it run faster. One of instructions which can sure be optimized away is the check for the break condition, at least within the time where it is known that the loop will not reach it. Any idea how to write such a loop? e.g.
3
2293
by: techgirl | last post by:
Hi All- I was wondering if anyone could help me with this issue. I am trying to run this 3rd party application called "CoreFTP.exe" from within C#. I have tested my code with "Notepad.exe" and that works fine. I've tried it with another program executable and it works. I am receiving this error from within .NET when I try to run "CoreFTP": Run-time Error! C:\Program Files\CoreFTP\coreftp.exe This application has requested the...
2
3022
by: techgirl | last post by:
Hi All- I was wondering if anyone could help me with this issue. I am trying to run this 3rd party application called "CoreFTP.exe" from within C#. I have tested my code with "Notepad.exe" and that opens up fine. I've tried it with another program executable and it works as well. When I go to run coreftp.exe, it will not work. I have even tried putting code into a .bat file and trying to execute that and it executes, but I get the same...
6
2416
by: Shawn Minisall | last post by:
I've been having some problems with using a while statement for one menu within another while statement for the main menu, first time I've done it. It's with choice number two from the menu. When I run the program, I get a UnboundLocalError: local variable 'ai' referenced before assignment. I initialize ai as "", but then it just skips to the you choose scissors I choose and nothing shows up. As soon as I take away the while again ==...
0
8828
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9537
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9319
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8241
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4599
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4869
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2213
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.