C++ help for a beginner

I am VERY new to C++ and was wondering if someone could help me write this program. I am trying to find the area and perimeter of a rectangle. The length is 10 and the width is 12. Area = length * width (i am presuming) and Permiter would = 2 * length + 2 * width.

would it be like?

length = 10;
width = 12
area = length * width;
perimeter = 2 * lenght + 2 * width;
cout<<"length * width"<<endl;
cout<<"2 * length + 2 * width<<endl

Please help!! Thanks, Mike
try using this code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <conio.h>
using namespace std;                    //declare this so that you should be able to use "cout" and "cin"

int main()                              //this an important fucntion
{
    int length = 10;
    int width = 12;
    int Area = length * width;
    int Perimeter = 2*length + 2*width;
    
    cout << "Area = " << Area <<endl;
    cout << "Perimeter = " << Perimeter;
    
    getch();
    return 0;
}
    
    



can anyone else please try to explain to mike?????

Last edited on
Firstly you would need to include the "<iostream>" header, for the output functions, & "conio.h" for the getch function
Next "using namespace std;" for using functions within the namespace "std" without having to write std::func_name.
Declare the "main" function of the program, which returns an integer, so is done using the integer keyword. "int main() { ... }"
Then we need to declare the length, width, area & perimeter of the rectangle, in this example they are all whole numbers (integers), so we use the "int" keyword, then use "=" to set a value to them.
The cout function then prints text within "quotation marks" or variables without quotation marks.
The variable endl is for a newline.
Use the getch function to wait for user to press enter.
Return 0, as the main function returns an integer.
hazda and chris thanks for all the help and explanations.

I came up with this, and it worked...Thanks again!


#include<iostream>
using namespace std;


int main()
{

int length;
int width;
int area, perimeter;

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

perimeter=2*length+2*width;
area=length*width;
cout<<"The area is "<<area<<endl;
cout<<"The perimeter is "<<perimeter<<endl;
return 0;
}

Then after i compiled it and executed it, i got a prompt to enter the length and width....Worked like gold! Thanks again!


Last edited on
Topic archived. No new replies allowed.