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?! -
#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;
-
-
-
}
-
3 1768
You compare double with '0' that is most likely to be ascii value of 48.
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.
Thanks, I figured it out.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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);
};
|
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...
|
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
|
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:
|
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
| |
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.
|
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...
|
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...
|
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 ==...
|
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,...
|
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...
| |
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,...
|
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...
|
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();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |