Hi. I am doing this assignment for my computer science class and I can't figure out what the problem is. It is giving me "error C2181: illegal else without matching if" when trying to execute it in visual studio. I can't figure out why, because as far as I know there is not even any 'if' statement in there.
#include <iostream>
usingnamespace std;
int main()
{
//Declare variables
int length = 0; //length of a baking pan in inches
int width = 0; //width of a baking pan in inches
int area;
int smallBrownies;
int bigBrownies;
int panModW;
int panModL;
int pandimW;
int pandimL;
//Get the pan dimensions
cout << "Enter the baking pan length (in inches): " << endl;
cin >> length;
cout << "Enter the baking pan width (in inches): " << endl;
cin >> width;
//Compute and assign
panModW=width % 2; // lazy conditional (will always be 1)
panModL=length % 2; // lazy conditional (will always be 1)
pandimW=width-panModW; //calculate "correct" width
pandimL=length-panModL; //calculate "correct" length
area = length * width;
smallBrownies = area/1; //determine how many small brownies pan can hold
bigBrownies =((pandimW*pandimL)/4); //determine how many big brownies pan can hold
//Display
cout << "A " << length << " by " << width << " pan can hold " << smallBrownies << " small brownies or " << bigBrownies << " large brownies " << endl;
return 0;
}
Actually rewrote and simplified it to this and it runs fine now. However I'm wondering if there's an easier way to do the calculations for bigBrownies? I'm using modulus to turn the potential odd numbers in width/length into even numbers, since the odd numbers are excess space. (bigBrownies are 2x2 in size, and can only go into the pan as wholes.)
Sorry for the short explanation and if it's too vague.
#include <iostream>
usingnamespace std;
int main()
{
//Declare variables
int length = 9; //length of a baking pan in inches
int width = 9; //width of a baking pan in inches
int smallBrownies;
int bigBrownies;
//Get the pan dimensions
cout << "Enter the baking pan length (in inches): " << endl;
cin >> length;
cout << "Enter the baking pan width (in inches): " << endl;
cin >> width;
//Compute and assign
smallBrownies = (length * width)/1; //determine how many small brownies pan can hold
bigBrownies = (((width-(width % 2))*(length-(length % 2)))/4);
//Display
cout << "A " << length << " by " << width << " pan can hold " << smallBrownies << " small brownies or " << bigBrownies << " large brownies " << endl;
return 0;
}