Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
#include <stdio.h>
#include <math.h>
#include <conio.h>

int main()
{
    double x;
    printf ("Enter degrees:__.");
    scanf ("%lf", &x);
    if ("%lf", &x, 0<=x<=90){
        printf("Everything is + ");
    }
     if ("%lf", &x, 90<=x<=180){
        printf("sin is + but cos,tg,ctg is - ");
    }
     if ("%lf", &x,180<=x<=270){
        printf("sin,cos is - but tg,ctg is + ");
    }
     if ("%lf", &x,270<=x<=360){
        printf("sin,tg,ctg is - but cos is + ");
    }
	getch();
	return 0;
}


What I have tried:

Why this show me evething from "printf"?? What i should do??
Posted
Updated 28-Sep-19 22:40pm
v2
Comments
k5054 28-Sep-19 13:33pm    
if ("%lf", &x, 0<=x<=90)

That's not how you write an if statement. Go back and look at sample if statements from your study material. You should be able to work it out.
Edit: In particular what does 0 <= x <= 90 evaluate to. Its not what you think.
Unnamed :D 28-Sep-19 13:46pm    
@k5054 i read my article and i really don't know what to do((
i'm tring to find the mistake for 2 hours and can't..
(Enter degrees:__.76
Everything is + sin is + but cos,tg,ctg is - sin,cos is - but tg,ctg is + sin,tg,ctg is - but cos is + )
this is my problem
Unnamed :D 28-Sep-19 13:51pm    
how i shoud to do corectly?
Unnamed :D 28-Sep-19 14:10pm    
if (x>=271 && x<=360){
printf("sin,tg,ctg is - but cos is + ");
}
corect?
k5054 28-Sep-19 14:12pm    
Right!

Your code has several problems:
Major flaws
  1. it features invalid syntax: the
    Quote:
    if ("%lf", &x, 0<=x<=90)
    line is illegal code in C, as k5054 pointed out, this is (though not extremely useful) legal C code.
  2. It lacks input validation.

Minor problems
  1. Type mismatches: 0, 90, .. are integer constant literals, your code compares them with a double variable (while this will work due to C type promotion rules, it is incorrect and misleading).
  2. Incorrect handling of ranges (corner cases), for instance
    Quote:
    if ("%lf", &x, 0<=x<=90){
    and
    Quote:
    if ("%lf", &x, 90<=x<=180){
    would both handle x==90.
  3. Your code uses an old, non standard header (conio.h).


Try
C
#include <stdio.h>

int main()
{
  double x;

  printf ("Please enter the angle value in degrees:\n");

  // input validation 1:  always check the return value of the scanf function 
  if ( scanf("%lf", &x) != 1  )
  {
    printf("invalid entry, please enter the angle degrees\n");
    return -1;
  }

  // input validation 2: check that number is in the allowed range
  if ( x < 0.0 || x >= 360.0)
  {
    printf("out of range, please enter a number in [0,360) range\n");
    return -1;
  }

  // input interpretation 
  if ( x < 90.0 )
    printf("everything is positive\n");
  else if ( x <180 )
    printf("sin is positive while cos, tg, ctg are negative\n");
  else if ( x < 270 )
    printf("sin and cos are both negative while tg, ctg are both positive\n");
  else
    printf("sin, tg, ctg are negative while cos is positive\n");

  printf("please enter a character to exit\n");

  getchar();

  return 0;
}
 
Share this answer
 
v2
Comments
k5054 29-Sep-19 10:50am    
Quote:it features invalid syntax: the
Quote:
if ("%lf", &x, 0<=x<=90)
line is illegal code in C.
That line compiles with both gcc and clang, so its not illegal code. What is going on here is the Comma Operator[^] is being used to evaluate the expression. The comma operator takes an expression like (a, b, c, ...), evaluates a, b, c, ... left to right (including any side effects) and returns the value of the last term. In this case "%lf" is just a pointer to a const char *, which gets thrown away, then we have &x, which is a double *, which again gets thrown away. Then we have 0<=x<=90. While that looks like its checking to see if x is between 0 and 90 what its actually doing is evaluating as (0 <= x) <= 90. The expression (0 <x) will always evaluate to either false (0) or true (1), so the expression expression (0 <= x <= 90) will always evaluate to true, regardless of the value of x;

All the pedantic stuff out of the way, though, I'd have to agree that the code is, conceptually at least, wrong. I'm not sure what the OP was thinking when he wrote the if statement, but clearly there was some misunderstanding of how an if statement should be written. I must confess, though, it took me a little while to figure out why 0 <= x <= 90 was always true. Its a subtle mistake, and I'm sure I've made it myself in the past, and may make it again in the future. And I'm willing to bet I'm not the only one.
CPallini 29-Sep-19 11:14am    
My bad, thank you for pointing it out.
Quote:
The sign of a trigonometric function is dependent on the signs of the coordinates of the points on the terminal side of the angle
SparkNotes: Trigonometry: Trigonometric Functions: Functions in Quadrants[^]

Here is a free course to learn C++ https://www.sololearn.com/[^]
 
Share this answer
 
v2
Comments
Unnamed :D 28-Sep-19 13:58pm    
i can't use this link((
RickZeeland 28-Sep-19 14:23pm    
Well it's just some mathematic mumbo jumbo about functions giving a positive or negative result :)
Unnamed :D 28-Sep-19 14:00pm    
and in my hometask wrote that i can't use standart mathematical functions and do not use standart libraries..
RickZeeland 28-Sep-19 14:28pm    
I think k5054 pointed you in the right direction, the math functions are not needed, this is just a simple quiz with text input and output.
Unnamed :D 28-Sep-19 14:31pm    
Thanks)!
You must learn the language. In if statement are only boolean expressions and some boolean operators (like,<,>, && and ||) best. The compiler is (mis) interpreting your strings as boolean expression. It is a bug.

And the syntax of printf you can read in that printf reference. Take also a look at the provided sample code.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900