Urgent: Need Help Writing Simple Program for length and width

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2009
Posts: 13
Reputation: southernd0529 has a little shameless behaviour in the past 
Solved Threads: 0
southernd0529 southernd0529 is offline Offline
Newbie Poster

Urgent: Need Help Writing Simple Program for length and width

 
-1
  #1
Feb 10th, 2009
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:
  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
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 147
Reputation: GDICommander is an unknown quantity at this point 
Solved Threads: 19
GDICommander's Avatar
GDICommander GDICommander is offline Offline
Junior Poster

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

 
0
  #2
Feb 10th, 2009
It looks correct.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 13
Reputation: southernd0529 has a little shameless behaviour in the past 
Solved Threads: 0
southernd0529 southernd0529 is offline Offline
Newbie Poster

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

 
0
  #3
Feb 10th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

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

 
1
  #4
Feb 10th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 27
Reputation: rudasi is an unknown quantity at this point 
Solved Threads: 2
rudasi's Avatar
rudasi rudasi is offline Offline
Light Poster

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

 
-1
  #5
Feb 17th, 2009
Hi,

Try Using,
[
system("pause");
return 0;
]
Just before the end of the program (last curly bracket)
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC