Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ extracting decimal solution error

Tags:

c++

codeblocks

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;
    }
}
like image 423
karolis Avatar asked Mar 25 '23 01:03

karolis


1 Answers

Are you perhaps looking for std::modf?

double wholePart;
if (std::modf(t, &wholePart) >= 0.1)
like image 120
Angew is no longer proud of SO Avatar answered Apr 06 '23 06:04

Angew is no longer proud of SO