i just finished this problem but i feel the code could be better improved and simplified. can anybody help me out? this program finds which of the two rectangles has a bigger area
int length, width,area;
int length2, width2,area2;
cout << "what is the length of the rectangle"<<endl;
cin >> length;
cout << "what is the width of the rectangle"<<endl;
cin >> width;
area = length * width;
cout << "what is the length of the second rectangle?"<< endl;
cin >> length2;
cout << "what is the width of the second rectangle? "<<endl;
cin >> width2;
area2 = length2 * width2;
if ( area > area2)
cout << "the first rectangle has a bigger area"<<endl;
else
cout << "the second rectangle has a biggger area"<<endl;
int length, width,area;
int length2, width2,area2;
//They need to put a space in between the two numbers
cout << "what is the length and width of the rectangle"<<endl;
cin >> length;
cin >> width;
area = length * width;
cout << "what is the length and width of the second rectangle?"<< endl;
cin >> length2;
cin >> width2;
area2 = length2 * width2;
if ( area > area2)
cout << "the first rectangle has a bigger area"<<endl;
else
cout << "the second rectangle has a biggger area"<<endl;
If just trying to make code more compact get rid of area and area2. Then change last part to,
cout << "The " << ((length * width) > (length2 * width2) ? "first" : "second ") << "rectangle has a bigger area\n";
Vidus, great suggestions that work well! But then I accidentally entered two rectangles of the same area and realized that there was a third possibility, so had to resort to longer code! Donnie
int length,length2,width,width2,area,area2;cout<<"what is the length and width of the rectangle"<<endl;cin>>length;cin>>width;area=length*width;cout<<"what is the length and width of the second rectangle?"<<endl;cin>>length2;cin>>width2;area2=length2*width2;if(area>area2){cout<<"the first rectangle has a bigger area"<<endl;}else{cout<<"the second rectangle has a biggger area"<<endl;}