-1

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;
  • 2
    An integer divided by an integer is an integer. 1/2 is 0. Not sure if this is your problem I don't understand your variable names. Also if the numbers have a decimal point maybe you want to use double instead of int. – drescherjm Sep 10 '22 at 14:28
  • 1
    [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) – Jesper Juhl Sep 10 '22 at 16:35

5 Answers5

1

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

Dúthomhas
  • 8,200
  • 2
  • 17
  • 39
0

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.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
0

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.

Beyondo
  • 2,952
  • 1
  • 17
  • 42
0

bulviukaina is int type variable so bulviukaina can assign integers only

0

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.

davidhigh
  • 14,652
  • 2
  • 44
  • 75