Thread: stcuk in loop

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    24

    stcuk in loop

    why is my program stuck in the last while loop? compile this code.. then enter 3,4,5 for a,b,c and then enter "y'' for your choice to continue... it continues to loop forever

    Code:
    #include <iostream.h>
    #include <math.h>
    #include <string.h>
    
    int main ()
    {
    	double a, b, c, s, area, perimeter, choice;
    	do
    	{
    	do
    	{
    		cout << "Enter side a of the triangle: ";
    		cin >> a;
    		while (a <= 0)
    		{
    			cout << "The sides of a triangle must be greater than 0." <<endl;
    			cout << "Please enter a number greater than 0: ";
    			cin >> a;
    		}
    		cout << "Enter side b of the triangle: "; 
    		cin >> b;
    		while (b <= 0)
    		{
    			cout << "The sides of a triangle must be greater than 0." <<endl;
    			cout << "Please enter a number greater than 0: ";
    			cin >> b;
    		}
    		cout << "Enter side c of the triangle: ";
    		cin >> c;
    		while (c <= 0)
    		{
    			cout << "The sides of a triangle must be greater than 0." <<endl;
    			cout << "Please enter a number greater than 0: ";
    			cin >> c;
    		}
    		if (a+b<c || a+c<b || b+c<a)
    		{
    			cout << "That is not a valid triangle." <<endl;
    			cout << "Please re-enter three sides: " <<endl;
    		}
    		
    	}
    	while(a+b<c || a+c<b || b+c<a);
    	
    	perimeter= a+b+c;
    	cout << "The perimeter is: " << perimeter <<endl;
    	s=(a+b+c)/2;
    	area= sqrt(s*(s-a)*(s-b)*(s-c));
    	if (area==0)
    		cout << "The area cannot be zero." <<endl;
    	else
    		cout << "The area is: " << area <<endl;
    	cout << "Do you want to try again?" <<endl;
    	cout << "Choose [Y/N]" <<endl;
    	cin >> choice;
    	while (choice != 'y' && choice != 'Y' && choice != 'n' && choice != 'N'){
    			cout << "I do not understand. Try again: ";
    			cin >> choice;
    		}
    	} while (c == 'y' || c == 'Y');
    	return 0;
    	cin.ignore(); cin.ignore();
    }

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Because choice is a double and y is a char.
    Btw, indent properly. Makes code easier to read.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    edit: I'm wrong. Thanks Elysia.
    Code:
    while(a+b<c || a+c<b || b+c<a);
    this loop? it's an infinite loop. the conditional never changes.
    Last edited by robwhit; 11-12-2007 at 12:18 AM.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No, the syntax is correct - it's a do...while loop.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You're not handling some cases of invalid triangles such as 1, 2, 4.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    incidently, try using more discriptive variable names rather than a,b,c etc. it stands out better what they represent and makes reading and espcially debugging the code a lot easier.
    Double Helix STL

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Although in this case, a, b and c are commonly used names for the sides of the triangle, and they are even used in the prompts for their values, so it's not that bad. The variable s, on the other hand, doesn't seem to be as self-explanatory.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My loop within loop won't work
    By Ayreon in forum C Programming
    Replies: 3
    Last Post: 03-18-2009, 10:44 AM
  2. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  3. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  4. while loop help
    By bliznags in forum C Programming
    Replies: 5
    Last Post: 03-20-2005, 12:30 AM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM