0

Not Yet Answered # cstdio input?

Salem 5,138 Discussion Starter wellibedamned Radical Edward 301 Discussion Starter wellibedamned Discussion Starter wellibedamned Discussion Starter wellibedamned Radical Edward 301 Discussion Starter wellibedamned Hey, so I wanna ask how I need to create a method who will remove word if in that word is 2 same chars. Example: "Potato" in this word there is a 2 "o" chars so this word will need to be removed. "Forum" in this word there is no ...

Hi I'm having a problem implementing a mini shopping cart drop down in the header to show the user all the products they have in their shopping cart. It seems the only solution for this is Ajax, and I've looked all over and can't find anything that I could possibly ...

0

0

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.

0

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

0

> 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.

0

```
#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

0

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??

0

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

0

> 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 );`

0

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 article has been dead for over six months. Start a new discussion instead.

Recommended Articles

Hi. Im using vb 2010. I want to dynamically add textboxes to my form by clicking on a button. I've google searched and so far this code worked:

```
Private Sub btn_addline_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_addline.Click
Dim txtB1 As New TextBox
Dim i
For i = ...
```