Home > Software design >  How to use decimal fractions in c
How to use decimal fractions in c

Time:09-11

How can i make it understand decimal fractions, because most of the time bulviukaina,mesoskaina and miltukaina are gonna be decimal fractions? and when i enter bulviukaina as a decimal fraction it calculates instantly without being able to insert mesoskaina and miltukaina?

i tried writing the numbers as 0,99 0.99 and 99/100

any ideas? im new to c

# include <bits/stdc  .h>
using namespace std;
int main ()
{
int bulviukiekis, mesoskiekis, miltukiekis, bulviukaina, mesoskaina, miltukaina, 
bendrakaina;
cout<<"Bulviu kiekis = ";
cin>>bulviukiekis;
cout<<"Mesos kiekis = ";
cin>>mesoskiekis;
cout<<"Miltu kiekis = ";
cin>>miltukiekis;
cout<<"Bulviu kaina = ";
cin>>bulviukaina;
cout<<"Mesos kaina = ";
cin>>mesoskaina;
cout<<"Miltu kaina = ";
cin>>miltukaina;
bendrakaina=((bulviukiekis*bulviukaina) (mesoskiekis*mesoskaina)  
(miltukiekis*miltukaina))*4;
cout<<"Seima per menesi bulvems, mesai ir miltams isleidzia "<<bendrakaina<<" Eur";

return 0;

CodePudding user response:

Fractional values are typically represented by floating point types (float and double). The standard input streams do not know how to do math, so you must do it for them.

#include <iostream>

double get_fraction_from_input( std::istream & input = std::cin )
//
// Obtain a fractional value from the user.
// It may be formatted as a standard floating point representation: "3.14", "1e-7", etc,
// or as a numerator and denominator separated by a solidus: "1/2", etc.
//
{
  double n, d;
  input >> n;  // get the numerator OR get the fractional value
  if (input.peek() == '/')  // if there is a denominator...
  {
    input.get();  // skip the solidus
    input >> d;   // and get the denominator
    n /= d;       // n/d --> compute the final fractional value
  }
  return n;
}

int main()
{
  double amount_of_meat = get_fraction_from_input();
}

It is also possible to overload the usual stream input operators to understand a “fraction” type, but that would be beyond what you are doing (or need).

CodePudding user response:

Use double instead of int. Double is not exactly storing fractions, but from your simple example it should be good enough for what you need.

CodePudding user response:

Modify int to float or double.

float is a 32-bit floating-point number.
double is a 64-bit floating-point number with double the precision.

CodePudding user response:

bulviukaina is int type variable so bulviukaina can assign integers only

CodePudding user response:

If bulviukaina,mesoskaina and miltukaina are floating-point numbers, you should call bulviukaina mesoskaina, mesoskaina miltukaina, and bulviukaina miltukaina. Only if they're integer, bulviukaina,mesoskaina and miltukaina are appropriate, but you shouldn't divide mesoskaina by bulviukaina and miltukaina. Better use bulviukiekis, mesoskiekis, miltukiekis, bulviukaina, mesoskaina, miltukaina, and bendrakaina in that case, as mesoskiekis, miltukiekis, bulviukaina are integers.

  •  Tags:  
  • c
  • Related