Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'m having a problem with my if/else statement... for whatever reason, only the

ID: 3566104 • Letter: I

Question

I'm having a problem with my if/else statement... for whatever reason, only the "if" portion of the condition is executing. Actually, whichever one is first is the one that will execute (I've swapped them just to see). I can't imagine what the problem is because the condition is pretty straight forward. Please be detailed when answering. Thanks a bunch.

#include <iostream>
using namespace std;

int main()
{

int choice;
float radius, area, perimeter;
  

cout<<" 1. Area of circle :";
cout<<" 2. Perimeter of circle :";
cout<<" Enter choice :";
cin>>choice;
cout<<" Enter radius of circle :" << endl;
cin>>radius;

if(choice = 1){
  
area = 3.14*radius*radius;
cout << "Area = " << area << endl;
  
}

else{
  
perimeter = 2*3.14*radius;
cout << "Perimeter = " << perimeter << endl;
  
}
  


return 0;
}

Explanation / Answer

#include <iostream>
using namespace std;

int main()
{

int choice;
float radius, area, perimeter;


cout<<" 1. Area of circle :";
cout<<" 2. Perimeter of circle :";
cout<<" Enter choice :";
cin>>choice;
cout<<" Enter radius of circle :" << endl;
cin>>radius;

//if(choice =1){ //You are using = that is a assignment operator so it is always evaluaated to true

if(choice == 1){ // use == that is comparison opeartor so it will do the comparison and give
// true false accordinly

area = 3.14*radius*radius;
cout << "Area = " << area << endl;

}

else{

perimeter = 2*3.14*radius;
cout << "Perimeter = " << perimeter << endl;

}

return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote