Simple c++ question, so as you can see it creates a table and inputs variable a and also variable t answer, the problem is I don't know how to fix the if ()
.
As you can see it has an error(typo). I don't know how to identify if variable t has example: 1
or 1.5
, if number have 1.(something here and it's bigger than number.1) then call one condition else call other.
int a,b = 18;
double t;
for (a = 0; a <= b; a++)
{
t = 8 + (double)(18 - a) / 2;
if (t >= *.1)
cout << setw(9) << a << setw(20) << fixed << setprecision(1) << t << endl;
else
cout << setw(9) << a << setw(20) << t << endl;
}
Tried :
#include <iostream>
#include <iomanip>
#include <cmath>
#include <math.h>
using namespace std;
int main ()
{
int a,b = 18;
double t;
for (a = 0; a <= b; a++)
{
t = 8 + (double)(18 - a) / 2;
if (modf(t, NULL) >= 0.1)
cout << setw(9) << a << setw(20) << fixed << setprecision(1) << t << endl;
else
cout << setw(9) << a << setw(20) << t << endl;
}
}
Fixed in my own way, still thanks 'Angew' he was the first to post modf():
#include <iostream>
#include <iomanip>
#include <cmath>
#include <math.h>
using namespace std;
int main ()
{
int a,b = 18;
double t,z;
int k;
for (a = 1; a <= b; a++)
{
t = 8 + (double)(18 - a) / 2;
if (modf(t, &z) >= 0.5)
cout << setw(9) << a << setw(20) << fixed << setprecision(1) << t << endl;
else
k = t;
cout << setw(9) << a << setw(20) << k << endl;
}
}
Are you perhaps looking for std::modf
?
double wholePart;
if (std::modf(t, &wholePart) >= 0.1)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With