please help me with this error

#include <iostream>

using namespace std;

int main()
{float x,y,z;
cout<<"care este valoarea laturii x a triunghiului?";
cin>>x;
cout<<"care este valoarea laturii y a triunghiului?";
cin>>y;
cout<<"care este valoarea laturii z a triunghiului?";
cin>>z;
if(x=y=z) cout<<"triunghiul este echilateral";
else if(x=y)||(x=z)||(y=z) cout<<"triunghiul este isoscel";
else if(x^2=y^2+z^2)||(y^2=x^2+z^2)||(z^2=x^2+y^2) cout<<"triunghiul este dreptunghic";



return 0;
}
Last edited on
if(x=y=z)

Very very wrong.

Try this:

1
2
if ( x == y &&
     y == z      )


Similarly in this if(x=y)||(x=z)||(y=z) and this if(x=y)||(x=z)||(y=z)

x = y means "set the value of x to the same as y"

x == y means "are x and y the same?"
#include <iostream>

using namespace std;

int main()
{int x,y,z;
cout<<"care este valoarea laturii x a triunghiului?";
cin>>x;
cout<<"care este valoarea laturii y a triunghiului?";
cin>>y;
cout<<"care este valoarea laturii z a triunghiului?";
cin>>z;
if(x==y&&y==z);
cout<<"triunghiul este echilateral";
if(x==y||x==z||y==z);
cout<<"triunghiul este isoscel";
if(x*x==y*y+z*z||y^2==x^2+z^2||z^2==x^2+y^2);
cout<<"triunghiul este dreptunghic";


return 0;
}


now it works,but it works wrong. I mean, it tells me "triunghiul este echilateral,triunghiul este isoscel,triunghiul este dreptunghic", even if I set x=6,y=6,z=6
pls tell me what is wrong
More than a couple of parenthesis are missing in your if statements.
I suggest you should:
- avoid declaring variables until you haven’t a value to store inside them;
- don't let any variable uninitialised;
- prefer double to float;
And, if possible:
- read compiler errors / warnings (they describe problems quite well);
- keep in mind operator precedence (precedence of ^ is lower the one of than ==, so perhaps you want to add some parenthesis, don’t you?)
http://en.cppreference.com/w/cpp/language/operator_precedence
- keep your code a bit tidier (you would have noticed the missing parenthesis by yourself).

Let’s look a code like the following:
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
#include <iostream>

int main()
{
    std::cout << "care este valoarea laturii x a triunghiului? ";
    double x = 0.0;
    std::cin >> x;

    std::cout << "care este valoarea laturii y a triunghiului? ";
    double y = 0.0;
    std::cin >> y;

    std::cout << "care este valoarea laturii z a triunghiului? ";
    double z = 0.0;
    std::cin >> z;

    if(x == y && y == z) {
        std::cout << "triunghiul este echilateral.\n";
    }
    if((x == y) || (x == z) || (y == z)) {
        std::cout << "triunghiul este isoscel.\n";
    }
    if(    ((x^2) == (y^2) + (z^2)) 
        || ((y^2) == (x^2) + (z^2)) 
        || ((z^2) == (x^2) + (y^2)) ) {
        std::cout << "triunghiul este dreptunghic.\n";
    }
    std::cin.ignore();
    std::cin.get();

    return 0;
}


In the above code are at least these errors:
a) you are comparing floating point numbers. It’s not a problem for the compiler, but in general you’d better avoid it. One of the many pages on this topic is:
http://floating-point-gui.de/errors/comparison/
In this case, however, since your are storing the numbers directly from the user and they don’t come from calculation, the comparison could be plausible (but I’d avoid it anyway).

b) You’re using the symbol ^ in a wrong context and, I suspect, without knowing its meaning in the C++ language.
Please, have a look here:
http://en.cppreference.com/w/cpp/language/operator_alternative
I think you were searching for the pow() function:
http://en.cppreference.com/w/cpp/numeric/math/pow
now it works :)))
thank you so much for helping me
You're doing if blocks wrongly.

With an if statement, there is NOT to be a semi-colon on the end.

This is bad:
1
2
3
4
if(x==y&&y==z);
{
  // code here
}



This is good:
1
2
3
4
if(x==y&&y==z)
{
  // code here
}



It looks like you simply put a semi-colon on the end of every line, without understanding why. That's wrong. You should only put them where they need to go. If you're going to write more C++, you should definitely go back and re-learn about if statements and semi-colons.
please explain me why I need to use x1 here:
#include <iostream>

using namespace std;

int main()
{int x,x1;
int s,p,nr;
cout<<" x= ";cin>>x;
x1=x;
s=0;
p=1;
nr=0;
if(x==0)nr=1;
while(x1!=0)
{
s=s+x1%10;
p=p*(x1%10);
nr++;
x1=x1/10;
}
cout<<"Suma cifrelor lui "<<x<<" este "<<s<<endl;
cout<<"Produsul cifrelor lui "<<x<<" este "<<p<<endl;
cout<<"Numarul cifrelor lui "<<x<<" este "<<nr;
return 0;
}
please explain me why I need to use x1 here:

You modify “x1” in the course of the calculation, so, if you didn’t use a copy, you wouldn’t any more have the original value for the final display.

Try giving more significant names to your variables:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

int main()
{
    int original = 0;
    std::cout << "x = "; std::cin >> original;
    int copy = original;
    int nr = 0;
    if(original == 0) { nr = 1; }
    int s = 0, p = 1;
    while(copy != 0)
    {
        s = s + copy % 10;
        p = p * (copy % 10);
        nr++;
        copy = copy / 10; // you modify x1 here
    }
    std::cout << "Suma cifrelor lui "     << original << " este " << s  << '\n';
    std::cout << "Produsul cifrelor lui " << original << " este " << p  << '\n';
    std::cout << "Numarul cifrelor lui "  << original << " este " << nr << '\n';
    return 0;
}


And, sorry, my friend, but... is there a tax on spaces in your country? :-)
(just kidding)
Bye.
Topic archived. No new replies allowed.