if statement - C++ Logicial Operators ( Could someone Explain and help) -
so have written small program work out perimeter of simple shapes (very new coding keeping simple). stuck though code works triangle , can't life of me work out how last logical operator work!!!? appreciate time , advice best regards jake
#include <iostream> #include <string> using namespace std; int main () { int length; int diameter; float pi = 3.14; string shape; string square = "square"; string triangle = "triangle"; string circle = "circle"; cout <<"=======================" << endl; cout <<"=welcome perimeters=" << endl; cout <<"#######################" << endl; cout <<"###|select shape|####" << endl; cout <<"=======================" << endl; cout <<"= | circle | =" << endl; cout <<"= | triangle | =" << endl; cout <<"= | square | =" << endl; cout <<"=======================" << endl; cout <<"enter shape >; "; cin >> shape; if (shape == "square") { cout << "enter length of side >: "; cin >> length; cout << "perimeter = " ; cout << length * 4 <<endl; } else { (shape == "triangle"){ cout << "enter length of side >: "; cin >> length; cout << "perimeter = " ; cout << length * 3 <<endl; } } else { (shape == "circle") { cout << "enter diameter >: "; cin >> diameter; cout << "perimeter = " ; cout << diameter * pi <<endl; } } return 0; }
you not writing else-if statement correctly. should in form:
if(boolean expression) {} else if (boolean expression) {} // many else ifs need else {} // optional
therefore else if conditionals should be:
if (shape == "square") { cout << "enter length of side >: "; cin >> length; cout << "perimeter = " ; cout << length * 4 <<endl; } else if (shape == "triangle"){ // , on... } else { cout << "invalid shape entered."; }
additionally, pi isn't 3.14. include <math.h>
, use m_pi
.
Comments
Post a Comment