Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Aug 28th, 2005, 9:25 PM   #1
yarold
Newbie
 
Join Date: Aug 2005
Posts: 1
Rep Power: 0 yarold is on a distinguished road
Help with a program

Hi, I'm new programming and I've come across a problem. I have to write a program that will read in the length and width of two rooms and then tells the user which room is larger. The program has to continue to ask for the dimensions until the user enters a width of the first room of zero, when it'll terminate and print "Bye".

Here's what it should look like
First room width: 15
First room length: 20
Second room width: 10
Second room length: 18
The first room is larger.
First room width: 12
First room length: 8
Second room width: 13
Second room length: 13
The second room is larger.
First room width: 0
Bye

I've come up with this after reading some tutorials, but it doesn't terminate the way I want it to.

#include <iostream.h>

main ()
{
     int w1, w2, l1, l2, a1, a2;

     do {
     cout << "First room width: ";
     cin >> w1;
     cout << "First room length: ";
     cin >> l1;
     cout << "Second room width: ";
     cin >> w2;
     cout << "Second room length: ";
     cin >> l2;
     
     a1 = w1 * l1;
     a2 = w2 * l2;
     
     if  (a1 > a2)
     cout << "The first room is larger.\n";
     else if (a1 < a2)
     cout << "The second room is larger.\n";
     else if (a1 == a2)
     cout << "The rooms are equal.\n";
     }
     while (w1 !=0);
     cout << "Bye";
     
     return 0; 
}

Can someone offer some insight as to how to get it to work properly?
yarold is offline   Reply With Quote
Old Aug 28th, 2005, 9:30 PM   #2
EverLearning
Hobbyist Programmer
 
EverLearning's Avatar
 
Join Date: May 2005
Location: Indiana
Posts: 130
Rep Power: 4 EverLearning is on a distinguished road
if you enter w1 = 0 your entire loop will have to execute before this input is detected.
One way you can fix this is check if w1 = 0 right after you cin>>w1. Something to this effect:
do{
.........
      cin >> w1;
      if (w1 == 0)
          break;
......
}while(1);
edit: "break" returns control to whatever follows the loop, which is print statement in your example.
__________________
got math? yumm...

"All men by nature desire to know" -Aristotle, Metaphysics
EverLearning is offline   Reply With Quote
Old Aug 30th, 2005, 2:47 AM   #3
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5 bl00dninja is on a distinguished road
***edited cuz i was drizzunk and missed something***

good luck!
__________________
i put on my robe and wizard hat...

Have you ever heard of Plato, Aristotle, Socrates?...Morons.
bl00dninja is offline   Reply With Quote
Old Aug 30th, 2005, 6:23 AM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
You should add status checks to cin. One missed keystroke or malicious input and you're hosed.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:22 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC