![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Aug 2005
Posts: 1
Rep Power: 0
![]() |
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? |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
Join Date: May 2005
Location: Indiana
Posts: 130
Rep Power: 4
![]() |
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); |
|
|
|
|
|
#3 |
|
Programming Guru
![]() Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5
![]() |
***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. |
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|