help please

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

Thanks..
Last edited on
you need
1
2
float length,width,area,parameter;
int numrectangles; // could also use short 


Use a cin to get the number of rectangles and then a for loop to repeat the length and width questions and calculations.

Bonus: Add an if ( length == 0 ) return 0 so the user can quit early.
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>?


Last edited on
Hotaru.....I got it to work, thanks for the help...

you were right i had to do a for loop..

thanks.
OK, thanks.
I have no idea how my comment got posted here. Sorry.
hi im ismael ... from phillipines
i think try this new code...
i think it can helps you
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include<iostream.h>
#include <math.h>

int main()
{
    double length;
    double width;
    double area, perimeter;

	


    cout <<"Please enter the length:";
    cin >> length;
    cout <<"Please enter the width:";
    cin >> width;

    perimeter=2*length+2*width;
    cout <<"\nThe perimeter is:"<<perimeter<<endl;

    area=length*width;
    cout <<"The area is:"<<area<<endl;	

return 0;
}





Last edited on
Topic archived. No new replies allowed.