I'm trying to find the average and it says that p must not be g>=5 or g<=5
and i get the error ...... else without a previous if...please help
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
float p,g,m;
cin>>p>>g;
if
((p==g+3)||(p==g-3))
m=(((p)+g)/2);
cout<<fixed<<setprecision(2)<<m<<endl;
else {
if
((p>=g+5)||(p<=g+5))
p=(p==g+3)||(p==g-3);
m=(((p)+g)/2);
cout<<fixed<<setprecision(2)<<m<<endl;
Use a bracket after your condition to execute some statement inside if
1 2 3 4 5
if(condition){
//block of statement
}else{
//block of statement
}
What were you doing here is
1 2 3 4 5 6
if(condition)
cout<<"test";//if will end here
cout<<"test2";//its not a part of if
else{ //so else here is declare after if is ended
//block of statement
}
#include<iostream>
#include<iomanip>
usingnamespace std;
int main()
{
float p,g,m;
cin>>p>>g;
if ((p==g+3)||(p==g-3))
m=(((p)+g)/2); //This statement belongs to if block
cout<<fixed<<setprecision(2)<<m<<endl; //This is not
//If block is already ended and next exle does not have a corresponding if
else {
if ((p>=g+5)||(p<=g+5))
p=(p==g+3)||(p==g-3);
m=(((p)+g)/2);
cout<<fixed<<setprecision(2)<<m<<endl;
}
Exactly as @lendraDwi said and also p=(p==g+3)||(p==g-3)
Doesn't do what you might be intending because it assigns p a bool value 0 if both statements
Are false or 1 if either or both statements are true.