### having trouble with program and setw()

UPDATE on program calculating area of rectangle, and converting feet to inches.

Result of program should be:
Enter the length in feet and inches: 3 0 <Enter>
Enter the width in feet and inches: 1 11 <Enter>

Dimension Feet Inches Tot.Inches
--------------------------------------
Length 3 0 36
Width 1 11 23
--------------------------------------

Area = 828 square inches.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950`` `````` Put the code you need help with here. // Program finds area of Rectangle // Program converts length and width in feet to inches #include #include // setw(11); using namespace std; int main() { int width_Inches = 11, // Iniialize to 11 int length_Inches = 0, // Intialize to 0 int width_Feet = 1, // Intialize to 1 int length_Feet = 3; // Intialize to 3 int width; int length; int feet; int inches; int area; // Intialize area by using the Formula // Get input of dimenisions of width and length in feet and inches cout << "Enter the length in feet and inches: "; cin >> width >> length; cout << " Enter the width in feet and inches: "; cin >> width >> length; // Converting feet to Inches inches = feet * 12; // Formula for Area of Rectangle. area = length * width; // Display the dimensions, feet, inches, and total inches of the Rectangle cout << "\n Dimension" << " Feet " << " Inches " << " Tot.Inches\n "; cout << "-------------------------------------------\n"; cout << setprecision(2) << fixed; cout << left << setw(11) << "Length " << length_Feet << length_Inches << right << setw(11) << endl; cout << left << setw(11) << "Width " << width_Feet << width_Inches << right << setw(11) << endl; cout << "--------------------------------------------\n\n"; // Calculate the Area. cout << " Area = " << area << " square inches. \n"; return 0; } /``````
Last edited on
You never modify your 'feet' variable.
I would suggest having less ambiguous variable names:
 ``1234`` ``````int length_feet; int length_inches; int width_feet; int width_inches;``````
Last edited on
I talked to my professor, and she said variables are alright (could be better) but its the whole program that needs fixing. She gave me hints setw(11), right and left manipulators. It compiled before but now it won't. I appreciate your help, I'll proabaly still change the variables
If you know about functions then you can simplify your program even further. However in the abscence of functions this is a way to get the answers. There is nothing special about the size of the setw's. All you have to do is choose vale=ues that give you what you want. Also because you are using integers there is no need for the precision or fixed functions from iomanip.

The variable area isn't needed in this case but if you must use it then
`area = (length_Feet*12 + length_Inches)* (width_Feet*12 + width_Inches)`

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152`` ``````#include #include // setw(11); using namespace std; int main() { int width_Inches = 11; // Iniialize to 11 int length_Inches = 0; // Intialize to 0 int width_Feet = 1; // Intialize to 1 int length_Feet = 3; // Intialize to 3 // int width; // int length; // int feet; // int inches; // int area; // Intialize area by using the Formula // Get input of dimensions of width and length in feet and inches cout << "Enter the length in feet and inches: "; cin >> length_Feet >> length_Inches; cout << " Enter the width in feet and inches: "; cin >> width_Feet >> width_Inches; // Display the dimensions, feet, inches, and total inches of the Rectangle cout << '\n' << setw(11) << left << "Dimension" << setw(8) << right << "Feet" << setw(8) << right << "Inches" << setw(12) << right << "Tot.Inches\n" << "-------------------------------------------\n" << setw(11) << left << "Length" << setw(8) << right << length_Feet << setw(8) << right << length_Inches << setw(11) << right << length_Feet*12 + length_Inches << '\n' << setw(11) << left << "Width" << setw(8) << right << width_Feet << setw(8) << right << width_Inches << setw(11) << right << width_Feet*12 + width_Inches << '\n' << "-------------------------------------------\n\n" << " Area = " << (length_Feet*12 + length_Inches)* (width_Feet*12 + width_Inches) << " square inches. \n"; return 0; }``````

 ```Enter the length in feet and inches: 3 0 Enter the width in feet and inches: 1 11 Dimension Feet Inches Tot.Inches ------------------------------------------- Length 3 0 36 Width 1 11 23 ------------------------------------------- Area = 828 square inches. Program ended with exit code: 0```
Hello BlueCOCO1,

Have a look at this and see what you think:
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748`` ``````#include #include // setw(11); using namespace std; int main() { constexpr int FOOT{ 12 }; //int width_Inches{ 11 }, // Iniialize to 11 // length_Inches{}, // Intialize to 0 // width_Feet{ 1 }, // Intialize to 1 // length_Feet{ 3 }; // Intialize to 3 int width{}, length{}; int lengthFeet{}, lengthInches{}; // <--- ALWAYS initialize all your variables. int widthFeet{}, widthInches{}; int area{}; // Intialize area by using the Formula. No this was uninitialized. cout << setprecision(2) << fixed; // <--- Only needs done once. // Get input of dimenisions of width and length in feet and inches cout << "Enter the length in feet and inches: "; cin >> lengthFeet >> lengthInches; cout << " Enter the width in feet and inches: "; cin >> widthFeet >> widthInches; // <--- Using the same variables for 2 different uses. // Converting feet to Inches length = (lengthFeet * FOOT) + lengthInches; width = (widthFeet * FOOT) + widthInches; // Formula for Area of Rectangle. area = length * width; // <--- Calculate in total inches. // Display the dimensions, feet, inches, and total inches of the Rectangle cout << "\n Dimension" << " Feet " << " Inches " << " Tot.Inches\n" // <--- Do not need the space after the "\n". "-------------------------------------------\n" << left << setw(11) << "Length " << lengthFeet << "\' " << lengthInches << '\"' << right << setw(14) << length<<'\n' << left << setw(11) << "Width " << widthFeet << "\' " << widthInches << '\'' << right << setw(13) << width<<'\n' << "--------------------------------------------\n\n"; // Calculate the Area. cout << " Area = " << area << " square inches. \n"; return 0; } ``````

It produces this output:
 ``` Enter the length in feet and inches: 3 0 Enter the width in feet and inches: 1 11 Dimension Feet Inches Tot.Inches ------------------------------------------- Length 3' 0" 36 Width 1' 11' 23 -------------------------------------------- Area = 828 square inches. ```

For lines 10 - 13 if you are going to use a comma leave the "int" of of lines 11 - 13. Otherwise change the comma to a (;). Somewhat irrelevant as those variables are not used and I have no idea why they are initialized to those values.

I usually put line 20 near the beginning of the program.

Line 8 avoids using a magic number as you had it in your line 31, which is not right to start with.

Andy
Last edited on
If we're getting into the minutia here, 'length', 'width', and 'area' shouldn't even be declared in those places. The best habit is to initialize with the declaration when possible, so you could declare and initialize length, width, and area on lines 30, 31, and 34, respectively. Ironically, before OP edited his post, feet was being initialized to 0, masking what would have been a warning when attempting to use feet in calculations. Guess there's trade-offs with every decision.

And personally, I think "INCHES_IN_FOOT" would be better than "FOOT" as a named constant.

Last edited on
Hello BlueCOCO1,

Now that the program is working better what part of "setw()" do you not understand?

Andy
@Ganado

 And personally, I think "INCHES_IN_FOOT" would be better than "FOOT" as a named constant.
That is the nice thing about variable names you can always change them when you think of something better.

https://www.youtube.com/watch?v=MBRoCdtZOYg Worth watching.

Andy
Thanks but I've watched that, too :)
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647`` ``````#include #include // setw(11); using namespace std; int main() { int width_Inches = 11; // Iniialize to 11 int length_Inches = 0; // Intialize to 0 int width_Feet = 1; // Intialize to 1 int length_Feet = 3; // Intialize to 3 // Get input of dimensions of width and length in feet and inches cout << "Enter the length in feet and inches: "; cin >> length_Feet >> length_Inches; int length = length_Feet * 12 + length_Inches; cout << " Enter the width in feet and inches: "; cin >> width_Feet >> width_Inches; int width = width_Feet * 12 + width_Inches; int area = length * width; // Display the dimensions, feet, inches, and total inches of the Rectangle cout << '\n' << setw(11) << left << "Dimension" << setw(8) << right << "Feet" << setw(8) << right << "Inches" << setw(12) << right << "Tot.Inches\n" << "-------------------------------------------\n" << setw(11) << left << "Length" << setw(8) << right << length_Feet << setw(8) << right << length_Inches << setw(11) << right << length << '\n' << setw(11) << left << "Width" << setw(8) << right << width_Feet << setw(8) << right << width_Inches << setw(11) << right << width << '\n' << "-------------------------------------------\n\n" << " Area = " << area << " square inches. \n"; return 0; }``````

 ```Enter the length in feet and inches: 3 0 Enter the width in feet and inches: 1 11 Dimension Feet Inches Tot.Inches ------------------------------------------- Length 3 0 36 Width 1 11 23 ------------------------------------------- Area = 828 square inches. Program ended with exit code: 0```
Topic archived. No new replies allowed.