943,546 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 868
  • C++ RSS
Feb 10th, 2009
-1

Urgent: Need Help Writing Simple Program for length and width

Expand Post »
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:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6.  
  7. int length, width;
  8. int perimeter, area; // declarations
  9. cout << "Length = "; // prompt user
  10. cin >> length; // enter length
  11. cout << "Width = "; // prompt user
  12. cin >> width; // input width
  13. perimeter = 2*(length+width); // compute perimeter
  14. area = length*width; // compute area
  15. cout << endl
  16. << "Perimeter is " << perimeter;
  17. cout << endl
  18. << "Area is " << area
  19. << endl; // output results
  20. } // end of main program
Last edited by Ancient Dragon; Feb 10th, 2009 at 8:34 pm. Reason: add code tags
Reputation Points: 6
Solved Threads: 0
Newbie Poster
southernd0529 is offline Offline
13 posts
since Feb 2009
Feb 10th, 2009
0

Re: Urgent: Need Help Writing Simple Program for length and width

It looks correct.
Reputation Points: 72
Solved Threads: 26
Posting Whiz in Training
GDICommander is offline Offline
209 posts
since Jun 2008
Feb 10th, 2009
0

Re: Urgent: Need Help Writing Simple Program for length and width

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
Reputation Points: 6
Solved Threads: 0
Newbie Poster
southernd0529 is offline Offline
13 posts
since Feb 2009
Feb 10th, 2009
1

Re: Urgent: Need Help Writing Simple Program for length and width

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.
Last edited by Comatose; Feb 10th, 2009 at 1:41 pm.
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
Feb 17th, 2009
-1

Re: Urgent: Need Help Writing Simple Program for length and width

Hi,

Try Using,
[
system("pause");
return 0;
]
Just before the end of the program (last curly bracket)
Reputation Points: 8
Solved Threads: 2
Light Poster
rudasi is offline Offline
35 posts
since Feb 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Array help needed
Next Thread in C++ Forum Timeline: Client/Server sockets and reading a file





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC