Is it wrong to ask for debugging help?

Status
Not open for further replies.

LimpBagel

Ars Legatus Legionis
11,778
code:<HR>
Code:
  #include <iostream.h><P>
int main()
{<P>	int side1, side2, side3;
	char another_set[2];<P>
	do
	{
		cout <<"Please enter side 1." << endl;
		cin >>side1;
		while (side1 <=0)
		{
			cout <<"Input should be positive, please try again." << endl;
			cin >> side1;
		}<P>		cout <<"Please enter side 2." << endl;
		cin >> side2;
		while (side2 <=0)
		{
			cout <<"Input should be positive, please try again." << endl;
			cin >> side2;
		}<P>		cout <<"Please enter side 3." << endl;
		cin >> side3;
		while (side3 <=0)
		{
			cout <<"Input should be positive, please try again." << endl;
			cin >> side3;}
		
		
		if(side1 + side2 > side3) && (side3 + side2 > side1) && (side1 + side3 > side2)
		{
			if (side1 == side2)
				{
				if (side2 == side3)
					cout <<"This is an equilateral triangle." << endl;
				else
					cout <<"This is an isosceles triangle." << endl;
				}
			else
					{
				if (side1 != side3)
					cout <<"This is a scalene triangle." << endl;
				else
					cout <<"This is an isosceles triangle." << endl;
					}	
		}
		
		else
			{
			cout <<"This is not a triangle.  Would you like to enter another set?" << endl;
			cin >> another_set;
			}
	
	}
	while(another_set == "y");<P>return 0;
}

Errors:

error C2143: syntax error : missing ';' before '&&'
error C2143: syntax error : missing ';' before '{'
error C2181: illegal else without matching if


The "missing ; before &&" is on the
"if(side1 + side2 > side3) && (side3 + side2 > side1) && (side1 + side3 > side2)" line.

The "missing ; before {" is on the very next line. For some reason putting a ; at the end of the if statement will eliminate this error even though ; dont belong at the end of an if statement. :confused:

The "else without matching if" is on the second to the last else. I really don't understand this one because all of the ifs have matching elses and are braced correctly. :confused:
 

earl

Well-known member
3,066
first of all, when you have an if statement, you need parens around the entire statement. So:
code:<HR>
Code:
if(side1 + side2 > side3) && (side3 + side2 > side1) && (side1 + side3 > side2)<P>//would become<P>if ( (condition1) && (condition2) && (condition3))<P>

Also, you know that you could use a for loop to grab the 3 variables you need? A good rule of thumb is if you find yourself copy/pasting code or typing essentially the same thing more than twice you ought to think about a loop or function.

-earl-
 

LimpBagel

Ars Legatus Legionis
11,778
first of all, when you have an if statement, you need parens around the entire statement

Aargh! I can't believe I didn't pick that up!

Also, you know that you could use a for loop to grab the 3 variables you need? A good rule of thumb is if you find yourself copy/pasting code or typing essentially the same thing more than twice you ought to think about a loop or function.

I can't seem to think of a loop that does all 3 at once. It's not too necessary so I'll figure that out once I get it running.


EDIT: ok the parens around the if statement fixed it, its running but the do/while doesnt loop when "y" is entered for another_set. Am I borking the char?
 

Richard Berg

Ars Legatus Legionis
43,037
Subscriptor
Better to compare like so: while(another_set[0] == 'y'). another_set without the [] is a pointer - C++ is nice enough to let you compare it with a string literal, but I wouldn't, since you're bound to introduce silent bugs by forgetting or adding one to many implied nulls, etc. etc.

Though not much better - it's very easy to give this program input that will cause it to blow up, obviously. I know the point of this exercise isn't robustness, but it's something that should always nag on you when you write code to handle user (or any kind of) input.
 

LimpBagel

Ars Legatus Legionis
11,778
Better to compare like so: while(another_set[0] == 'y'). another_set without the [] is a pointer - C++ is nice enough to let you compare it with a string literal, but I wouldn't, since you're bound to introduce silent bugs by forgetting or adding one to many implied nulls, etc. etc.

Ah yes, I can see how that would be a problem. I finally got it working fine after fixing the [ ] around the char. Thanks to everyone who helped me out!
 
Status
Not open for further replies.