954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Urgent: Need Help Writing Simple Program for length and width

I have written the program but I do not know if the results are correct. This is my Declaration: length width area and perimeter. My Input is length and width. The calculations are p=2*(l+w) and a=l*w. My Output is P and A. So what i want to know is, are these the correct results when the program is ran because i do not think so. here is the program:

#include <iostream>
using namespace std;

int main()
{

  int length, width;
  int perimeter, area;              // declarations
  cout <<  "Length = ";             // prompt user
  cin >> length;                    // enter length
  cout << "Width = ";               // prompt user
  cin >> width;                     // input width
  perimeter = 2*(length+width);     // compute perimeter
  area = length*width;              // compute area
  cout << endl
       << "Perimeter is  " << perimeter;
  cout << endl
       << "Area is " << area
       << endl;                    // output results
} // end of main program
southernd0529
Newbie Poster
13 posts since Feb 2009
Reputation Points: 6
Solved Threads: 0
 

It looks correct.

GDICommander
Posting Whiz in Training
211 posts since Jun 2008
Reputation Points: 72
Solved Threads: 26
 

but when i run it, it asks for length (then i type a random number and hit enter) then it asks for width (and i type a random number and hit enter) but then it just closes and thats it. If its possible can you please copy paste and run it so u can see what im talking about. I have this as my first assignment in my class due today at 12 30 Thanks

southernd0529
Newbie Poster
13 posts since Feb 2009
Reputation Points: 6
Solved Threads: 0
 

First, you don't want your instructor to know that you simply copied the code from this web page . Getting assistance by using the page is one thing.... basically plagiarizing the code is another altogether. However, the output is correct, and the program does work.

The reason it is likely "just closing" is because you are running this through a GUI of some kind, that launches the code, and exits (hence, closing the otuput window). You can run this through a command line, or put a cin.ignore() just before the return 0; (which you don't have, but should have as the last line before the ending }). If one cin.ignore() doesn't work, try two.

Comatose
Taboo Programmer
Team Colleague
2,910 posts since Dec 2004
Reputation Points: 361
Solved Threads: 215
 

Hi,

Try Using,
[
system("pause");
return 0;
]
Just before the end of the program (last curly bracket)

rudasi
Light Poster
35 posts since Feb 2009
Reputation Points: 8
Solved Threads: 2
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You