I took this C++ class online because there was nothing else available and I needed to keep a full load. It is flat out kicking my tale, I just don't get some of it. Could someone please help me with this problem?
I am requesting from the user the number of rectangles to calculate.
Alowing the user to enter decimals for the lenght and width
and the output should be formatted with precision of 2.
example i came up with....
Enter number of rectangles: 2
Enter lenth of rectangle: 12.345
Enter width of rectangle: 10.1112
are of rectangle: 124.82
perimeter of rectangle is 44.91
lenght of rectangle is 2.111
width of rectangle is 3.4321
area of rectagnle is 7.25
perimeter is 11.09
would it be kinda like?
//Mike
//CSCI 2010
/* (comments)
*/
#include<iostream>
using namespace std;
int main()
{
int lenght;
int width;
int area, perimeter
cout<<"Please enter the length:"'
cin>>length;
cout<<"Please enter the width:";
cin>>widht;
return 0;
perimeter=2*lenth+2*width;
area=lenght*width;
cout<<The area is"<<area<<endl;
cout<<"The perimeter is"<<perimeter<<endl;
return 0;
This works for using one rectangle...how could I set it up for two inputs and using decimals? Totally lost, please help
CPlusPlus.com's tutorial has a section on Dynamic Memory, which seems to be exactly what you're looking for. You input how many rectangles you want, and then you can allocate the right amount of space for that many rectangles. http://cplusplus.com/doc/tutorial/dynamic.html
Also, to simplify things, you'd probably want to make a struct to represent a rectangle, and then you don't have to have a dynamic memory array for each length, each width, etc. http://cplusplus.com/doc/tutorial/structures.html
Hotaru and Azrael thanks for the help. Still having some questions though.
So would it be like?
# include<iostream>
using namespace std;
int main()
{
float length, width, area, parameter;
int numrectangles;
cout<<"Please enter the rectangle"
cin>>rectangle;
cout<<"Please enter length"
cin>>length;
cout<<"Please enter width"
cin>>width;
return 0;
parameter=2*length+2*width;
area=length*width;
cout<<"The parameter is"<<parameter<<endl;
cout<<"The area is"<<area<<endl;
return 0;
}
Sorry for bothering you guys with all these questions. And once again, thanks for the help.
P.S. Could you give me an example with the loop, and the set precision of 2? Also, is there a certain header code that needs to be entered to allow the user to input decimals...Like a <iomanip>?