i'm having problems reading with scanf...
i'm trying to input some coordinates enclosed in brackets but it kinda skips the input that way...
is it forbidden to say scanf( "(%d,%d) (%d,%d) (%d,%d) (%d,%d)", x1, yo, x2, y2, x3, y3, x4, y4 );?

i'm using yo instead of y1 cause cmath doesn't allow it...

8 Years
Discussion Span
Last Post by wellibedamned

Well the first thing I would suggest is (if you're going to use the C API in a C++ program) is to read the line into a buffer, then use sscanf()

Next, I would suggest you treat each (%d,%d) as a single input routine, and call that 4 times in a loop.

Oh, and you forgot a whole load of & in front of all those variables.


hm yeah...the &s, thanks
i have another question, i just don't want to post a new thread...
i've already posted this question but now i'm asking...
"%.5g" prints an integer if there are no digits beyond the decimal point, but it rounds all to one digit like if i had a float f = 1.3333, output would be 1.3
why? and how do i get normal output


> why?
I don't understand your question because it doesn't match what Edward knows about printf. The precision field of the %g format modifier uses significant digits because it supports both %f and %e formatting. When the %f formatting is used, as in this case, "significant digits" includes digits on either side of the radix. Printing 1.3333 with %.5g should give you 1.3333. With %.3g you should get 1.33 and with %.7g the result should be 1.3333. There's not really a case where the output would be 1.3 unless you use 2 as the precision.

Can you post a short program that shows the error so that Ed can run it? Also mention what compiler you're using.

#include <cstdio>
int main( void )
    int h, m, s; double kh = 0, km = 0;
    scanf( "%d%d%d", &h, &m, &s );
    for( int i = 0; i < h; i++ )
         kh += 30;
    for( int i = 0; i < m; i++ )
             kh += 0.5;
             km += 6;
    for( int i = 0; i < s; i++ )
             kh += 0.00833333333333333;
             km += 0.1;
    while( kh >= 360 )kh -= 360;
    while( km >= 360 )km -= 360;
    printf( "%.12f\n%.12f", kh, km );
    return 0;

okay, so the problem is measuring the angle between the clock pointers and number 12 on the clock, it gives good results but i don't like the formating of the doubles


umm, yea, another question came up... and again, i don't want to post a new thread.
i'm trying to do a float integer check, and it simply doesn't work.
i made a function like this:

bool isint( float x )
       int n = x;
       return (x == (float)x);

i tried with static_cast too, but i guess it doesn't matter. the input is for example 11/6, it gives true??


i want them printed as integers if they are, i want the printed with non zero significant digits...for example: input: 1.3 output: 1.3


> i'm trying to do a float integer check, and it simply doesn't work.
Checking float values for equality is tricky. A simple method to see if the value has a non-zero precision up to a certain number of digits is to subtract the whole number away from the float value, then multiply by 10 for the number of precision digits you want to check. If the whole number is 0, there wasn't any precision.

Say you have 123.00256. To check if it's an integer to 2 digits of precision, lop off the 123 to get 0.00256, then multiply by 10 twice to get 0.256. The whole number is 0, so it's an integer. For 3 digits of precision, multiply by 10 three times to get 2.56. The whole number isn't 0, so it's not an integer:

bool isint(float value, int precision)
  // Remove the whole number
  value -= (int)value;

  // Shift the precision into a whole number
  while (--precision >= 0)
    value *= 10;

  return (int)value == 0;

It's not perfect, but it might work for what you want.

> i want them printed as integers if they are, i want the printed with non zero significant digits
Does just %g not work? If the number doesn't have any precision, it prints just the integer. If the number has a precision, it prints the precision too, but without trailing zeros:

printf( "%g\n%g", kh, km );

hmm, i think i get the %g format. and i found out where my error was while doing the integer check. i was doing division on an integer and forgot that in c++ '/' gives integer results. i did a cast to float on it, now it works perfectly! Thanks Ed!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.