Else without a previous if

Oct 31, 2019 at 7:15pm
closed account (jhXS8vqX)
Hello. I meet a problem in my code(fragment of a code):
1
2
3
4
5
6
7
8
9
10
  if(calitate == 0.065 &&  esantion_dublu < 500)
    A1 = 0;
    R1 = 1;
    A2 = 0;
    R2 = 1;
else if(calitate == 0.065 &&  esantion_dublu == 500)
    A1 = 0;
    R1 = 2;
    A2 = 1;
    R2 = 2;

Error: else without a previous if.. how can I do? Thank you.
Last edited on Oct 31, 2019 at 7:19pm
Oct 31, 2019 at 7:31pm
Use brackets { } for multi-statement bodies of if statements.

1
2
3
4
5
6
7
8
9
10
11
12
if (a)
{
    b;
    c;
    d;
}
else if (e)
{
    f;
    g;
    h;
}


http://www.cplusplus.com/doc/tutorial/control/
Last edited on Oct 31, 2019 at 7:31pm
Oct 31, 2019 at 7:32pm
closed account (jhXS8vqX)
In final of code i want to see:

cout<<"Numarul de componente pentru care accept lotul [A1]: "<<A1<<endl;
cout<<"Numarul de componente pentru care resping lotul [R1]: "<<R1<<endl;
cout<<"Numarul de componente pentru care accept lotul [A2]: "<<A2<<endl;
cout<<"Numarul de componente pentru care resping lotul [R2]: "<<R2<<endl;

I make what did you said, but terminal print A1=0, R1=0, A2=93743638, R2=0..
Oct 31, 2019 at 7:38pm
Please post the full program.
Oct 31, 2019 at 7:39pm
Hey,
you appear to be comparing a double using equality. this often fails. It may not be entering either statement because calitate == 0.065 may never be true.
consider
if( abs(calitate - 0.065) > 0.001 && … //0.001 is an example it can be smaller

instead.
Last edited on Oct 31, 2019 at 7:41pm
Oct 31, 2019 at 7:39pm
Directly comparing floating-point values for equality is very error prone if you don't know what you're doing.
Stick to integers for computation if possible.
Last edited on Oct 31, 2019 at 7:40pm
Topic archived. No new replies allowed.