 HI! I am new to programming (*although i know some html*) and I was wondering if someone could tell me what is wrong with my C++ area calculator? Thanks!

``````#include <iostream>
using namespace std;
int main (int argc, char **argv)
{
int height;
int width;
int area = height*width;

cout << "This program was written by Michael Keefe\n\n---------------------------------------------------------------- \n\nWhat is the height of the quadrilateral? \n" << endl;
cin >> height;

cout << "\nThankyou.\n\nThe height of the quadrilateral is: " << height << "\n\nPlease enter the width of the quadrilateral.\n" << endl;
cin >> width;

cout << "\nThankyou.\n\nThe height of the quadrilateral is: " << height << "\nThe width of the quadrilateral is: " << width << "\n\n-------------------------------------------------------\n\nThe area of the quadrilateral you specified is: " << area << endl;
cout << "\n\nThis program was developed by Michael Keefe" << endl;

system("pause");
return (0);
}``````

Every time I input something, it gives me the area as 152! Even 3x3!

Can someone help? P.S. This isn't homework! :cheesy: :cheesy:

I am simply learning how to program!

It because you are setting the area at the begging to a garbage value if you want to update the area set it to height * width after the values are inputted

I am allso new to this. But here is the code I made for the same type of thing.
I am sure the pros will be able to stream line it.

``````#include<iostream>
using namespace std;

int main()
{cout<<"Welcome to the Handy-Dandy Area Calc.\n"<<"\n";
cout<<"\n";

int input;
while ( input != 5 ){
float x;
float y;
float z;
float r;

cout<<"1...Square\n";
cout<<"2...Rectangle\n";
cout<<"3...Circle\n";
cout<<"4...Triangle\n";
cout<<"5...Exit\n";
cin>> input;
cin.ignore();

if ( input == 1 ) {
while ( input == 1 ){
cout<<"what is the length of one side?  ";
cin>> x;
cin.ignore();
cout<<"The area of your square is "<< x*x;
x = 0;
cin.get();
input = 0;
}
}
else if ( input == 2 ) {
while ( input == 2 ){
cout<<"What is the length of the rectangle?  ";
cin>> x;
cin.ignore();
cout<<"How wide is the rectangle?  ";
cin>> y;
cin.ignore();
cout<<"The area of your rectangle is "<< x*y;
x = 0;
y = 0;
cin.get();
input = 0;
}
}
else if ( input == 3 ) {
while ( input == 3 ){
cin>> r;
cin.ignore();
cout<<"The area of your circle is "<< 3.14159265*(r*r);
r = 0;
cin.get();
input = 0;
}
}
else if ( input == 4 ) {
while ( input == 4 ){
cout<<"what is the base of the triangle?  ";
cin>> x;
cin.ignore();
cout<<"How tall is the triangle? ";
cin>> y;
cin.ignore();
cout<<"The area of your triangle is "<< (x*y)/2;
x = 0;
y = 0;
cin.get();
input = 0;
}
}
}

}``````

I know I should //Comment.
/* I just think with that code it is easy to follow */

Later Thanks guys, I shall update my code now! You will have to forgive me if i come back even more confused! I am only 13! If u want u can check out my website at www.keefe-interactive.com!

Thanks! Here is my updated code, but the window appears and then dissapears instantly! any thoughts on what i can do? I am using Dev-C++ and when i do "COMPILE AND RUN" it dissapears immediately.

``````#include <iostream>
using namespace std;
int main ()
{
int height;
int width;
cout << "This program was written by Michael Keefe\n\n---------------------------------------------------------------- \n\nWhat is the height of the quadrilateral? \n" << endl;
cin >> height;
cout << "\nThankyou.\n\nThe height of the quadrilateral is: " << height << "\n\nPlease enter the width of the quadrilateral.\n" << endl;
cin >> width;
int area = height*width;
cout << "\nThankyou.\n\nThe height of the quadrilateral is: " << height << "\nThe width of the quadrilateral is: " << width << endl;
cout << "\n\n-------------------------------------------------------\n\nThe area of the quadrilateral you specified is: " << area << endl;
cout << "\n\nThis program was developed by Michael Keefe" << endl;
return (0);
}``````

I removed the PAUSE bit because it was making my program say:

PRESS ANY KEY TO CONTINUE

as soon as i ran it, and when i pressed a key it closed. Thanks dave, my program doesn't close immediately now, but I still get incorrect calculations! :sad:

Here is copied text from a console window when i tried to find the area of a 5 by 5 square:

``````This program was written by Michael Keefe

----------------------------------------------------------------

What is the height of the quadrilateral?

5

Thankyou.

The height of the quadrilateral is: 5

5

Thankyou.

The height of the quadrilateral is: 5
The width of the quadrilateral is: 5

-------------------------------------------------------

The area of the quadrilateral you specified is: 152

This program was developed by Michael Keefe
Press any key to continue . . .``````

Any ideas as to why this happens??

Dunno. It looks like you didn't recompile. Maybe trim the clutter 'til later.

``````#include <iostream>
using namespace std;

int main()
{
int height;
int width;
cout << "height? ";
cin  >>  height;
cout << "width? ";
cin  >>  width;
int area = height * width;
cout << "height = " << height << endl;
cout << "width  = " << width  << endl;
cout << "area   = " << area   << endl;
return 0;
}

/* my output
height? 4
width? 5
height = 4
width  = 5
area   = 20
*/`````` That may well be it, thanks!

It would seem that my Dev-C++ is playing up... i will reboot and reinstall it to see what happens. **but i will back up my programs first!** lol :mrgreen:

thx alot Ok, sorted! Got the program re-compiled. it was a matter of using the "rebuild all" function.

However, i now cannot get my program to stop to allow me to see the output! very annoying, any ideas? I entered this but it still closes immediately:

``````#include <iostream>
using namespace std;

int main(void)
{
int height;
int width;
cin  >>  height;
cin  >>  width;
int area = height * width;
cout << "\nHeight = " << height << "CM" << endl;
cout << "Width  = " << width  << "CM" << endl;
cout << "\n\nThe area of the quadrilateral is " << area << "CM" << endl;
std::cout <<"Press [Enter] to continue" <<std::endl;
std::cin.get();
return(0);

}``````

Ah. It grabs the newline left after you entered the width. Add a second one. THANKYOU!

My 2nd program is finished! Thanks a load! 