problem with a program

closed account (2wv7M4Gy)
I made my self this program , but it doesn't work correctly , can anyone help me ?

#include <iostream>
using namespace std;
int main()
{
float a,b,c,l,s,p;

cout<<"a="; cin>>a; if (a<=0) cout<<"Can't accept negative numbers or 0"<<endl<<cout<<"a="; else cin>>a;
cout<<"b="; cin>>b; if (b<=0) cout<<"Can't accept negative numbers or 0"<<endl<<cout<<"b="; else cin>>b;
cout<<"c="; cin>>c; if (c<=0) cout<<"Can't accept negative numbers or 0"<<endl<<cout<<"c="; else cin>>c;
cout<<"l="; cin>>l; if (l<=0) cout<<"Can't accept negative numbers or 0"<<endl<<cout<<"l="; else cin>>l;

s=(a*l)/2;
p=a+b+c;

cout<<"To embadon tou trigonu einai :" <<s<<endl;
cout<<"I perimetros tou trigonou einai :" <<p<<endl;

system ("pause");
return 0;
}

How it works if and else ? i don't know if it is correct .

If anyone could help me i would appreciate it !
lines containing something like this
/* ... */<<endl<<cout<</* ... */
are wrong, modify them as this:
cout<<"Can't accept negative numbers or 0"<<endl<<"a=";
(so remove that cout<<)

The way you are using the if/else statement is correct but I think in this case a loop would be better

http://www.cplusplus.com/doc/tutorial/control.html
Last edited on
I guess you want to promt the user for a value, and if that value is outside a certain range (below or equal to zero) it should aks the user again. Right?
In that case, you should use a (do-)while-loop:
1
2
3
4
5
6
7
8
9
float a;
do
{
    cout<<"Enter a number:"<<endl;
    cin>>a;
    if (a<=0)
        cout<<"Can't accept negative numbers or 0"<<endl;
}
while (a<=0)


Since you would have to do this four times, you may want to create a function.
Topic archived. No new replies allowed.