if statement didnt work correctly

Hello, i have a program where inputed numbers must be in interval from 2.0 to 5.0. if it's true, second if block must run forward, if not true, further blocks must be ignored, and program show else. Now when program run, i'm always get else.

p.s Sorry for bad english :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
 if ((pirmas || antras || trecias) >= 2.0 && (pirmas || antras || trecias <= 5.0)) // else carried always, nested if did'nt work.
    {

        if (pirmas > antras && pirmas > trecias)
        {
        vidurkis = (pirmas + antras + trecias)/3;
        cout << "Atleto vardas yra: " << vardas << endl;
        cout << "Geriausio suvio metu kulka nuskriejo: " << pirmas << " metra.\n";
        cout << "Suvis buvo atliktas: " << data << endl;
        cout << "Visu suviu vidurkis yra: " << vidurkis << endl;
        }
        else if (antras > pirmas && antras > trecias)
        {
        vidurkis = (pirmas + antras + trecias)/3;
        cout << "Atleto vardas yra: " << vardas << endl;
        cout << "Geriausio suvio metu kulka nuskriejo: " << antras << " metra.\n";
        cout << "Suvis buvo atliktas: " << data << endl;
        cout << "Visu suviu vidurkis yra: " << vidurkis << endl;
        }
        else if (trecias > pirmas && trecias > antras)
        {
        vidurkis = (pirmas + antras + trecias)/3;
        cout << "Atleto vardas yra: " << vardas << endl;
        cout << "Geriausio suvio metu kulka nuskriejo: " << trecias << " metra.\n";
        cout << "Suvis buvo atliktas: " << data << endl;
        cout << "Visu suviu vidurkis yra: " << vidurkis << endl;
        }
        }
    else
    {
        cout << "Ivesti skaiciai yra klaidingi, intervalas yra nuo 2.0 iki 5.0\n";
        cout << "Perkraukite programa ir bandykite is naujo.\n";
    }
(pirmas || antras || trecias) >= 2.0This is not doing what you think.
first (pirmas || antras || trecias) would be calculated with result bool which will be converted in either 0 or 1 which is less than 2, so your condition is always false.
Correct way: ((primas >= 2.0) || (antras >= 2.0) || (treacias >=2.0)) && //...
Also you can run into floating point comparsion issues here.
@Floating point comparison issues:
Probably not, the most issues are with equal values, but not with < or >.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <iomanip>

int main()
{
    double d(0);
    for(int i = 0; i < 10; ++i)
        d += 0.1;
    if (d >= 1.0)
        std::cout << "OK";
    else
        std::cout << "Fail";
    std::cout << std::endl << d;
    std::cout << std::setprecision(20) << d;
}
Fail
1
0.99999999999999988898
Comparsion issues can arise everywhere where '=' symbol is present (==, !=. >=, <=)
Last edited on
i'm change program like you say: <code>if ((pirmas >= 2.0) || (antras >= 2.0) || (trecias >= 2.0) && (pirmas <= 5.0) || (antras <= 5.0) || (trecias <= 5.0))</code> but now if i write incorect number like 8.2 or something else, program still carried forward, and not show else statement
You have problem with logic here. Describe what do you want to achieve.
MiiNiPaa wrote:
You have problem with logic here. Describe what do you want to achieve.

Show us your current code
here's my task:
Write a program that asks for the name of a shooter and the date and vault
heights (in meters) of the athlete s three best vaults. It should then report, in order of height (best rst), the date on which vault was made and it height.
Input Validation: Only accept values between 2.0 and 5.0 for the heights.
Your problem that now your if statement requires at least one of your variables to be equal or larger that 2.0 and at least one to be equal or lesser that 8.0.
You want all of them to be >=2 and <=8. So change all logical or (||) to and (&&)
Thank you alot, problem solved :)
Topic archived. No new replies allowed.