GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 23-May-2004, 15:11
crystalattice's Avatar
crystalattice crystalattice is offline
Flame War Instigator
 
Join Date: Apr 2004
Location: San Diego
Posts: 1,550
crystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nice
Question

Can't find logic error


The code below creates a simple boolean output for determining the type of triangle you have based on the length of the sides, i.e. equilateral, isosceles, or scalene. The code works except for determining isosceles; if only 2 sides are the same length, all I'm told is that it's a triangle.

I've tried different ways of doing the AND/OR logic and using loops, switches, etc. Does anyone see my logic error? Thanks.

CPP / C++ / C Code:
/*
Purpose:  from 3 real numbers, determine if they represent an 
equilateral, isosceles, or scalene triangle
*/

#include <iostream>

using namespace std;

int main()
{
	//Input
	cout << "Enter 3 numbers\n";
	int n1, n2, n3;
	cin >> n1
		>> n2
		>> n3;
	
	//Calculations
	bool triangle, equilateral, isosceles, scalene;
	
	if (((n1+n2)>n3) || ((n2+n3)>n1) || ((n3+n1)>n2))
		triangle = true;
	else triangle = false;
	
	if ((triangle == true) && ((n1!=n2)&&(n2!=n3)&&(n3!=n1)))
		scalene = true;
	else scalene = false;
	
	if ((triangle == true) && ((n1==n2)&&(n2==n3)))
		equilateral = true;
	else equilateral = false;
	
	if ((triangle == true) && (((n1==n2)&&(n1==n3))||((n1==n2)&&(n2==n3))))
		isosceles = true;
	else isosceles = false;
	
	
	
	//Output
	cout << "\nTriangle " << triangle
		<< "\nEquilateral " << equilateral
		<< "\nIsosceles " << isosceles
		<< "\nScalene " << scalene;
	return 0;
}
__________________
Common Sense v2.0-Striving to make the world a little bit smarter.
  #2  
Old 23-May-2004, 17:19
machinated machinated is offline
Regular Member
 
Join Date: Mar 2004
Location: victoria, canada
Posts: 324
machinated has a spectacular aura aboutmachinated has a spectacular aura about
1. you have not initialized your boolean variables.
using uninitialized variables may cause your program to malfunction.
also if you were to initialize your boolean variables to false, you would be able to get rid
of those else statements where you define them to be false each time.

2. your triangle determination logic is flawed. type in 1 1 0 or 1 1 2
it shouldnt form a triangle, but it does. fix your statement. change the ORs to ANDs.

3. your isosceles triangle determination logic is flawed.
a. Apparently when your triangle turns out to be equilateral, it's also an isosceles - do you want it to do that?
b. you are checking for equilateral triangles in your isosceles statement. you need to change it to reflect the isosceles triangle properties:

CPP / C++ / C Code:
if( ((n1==n2)&&(n2!=n3)) || ((n1==n3)&&(n3!=n2)) || ((n2==n3)&&(n3!=n1)) )
  isosceles = true;
__________________
spasms!!!
  #3  
Old 24-May-2004, 19:31
crystalattice's Avatar
crystalattice crystalattice is offline
Flame War Instigator
 
Join Date: Apr 2004
Location: San Diego
Posts: 1,550
crystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nice
Thanks. I knew it was something easy, but I think I was "too close to the problem" to see how to fix it.
__________________
Common Sense v2.0-Striving to make the world a little bit smarter.
 
 

Recent GIDBlogWriting a book by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Operator Overloading: << aaroncohn C++ Forum 36 07-Dec-2004 19:22
Visual C++ 6 Compiler error vip3r C++ Forum 2 13-Apr-2004 14:34
Simple error that i cant find... irritating me Homestar C Programming Language 8 30-Mar-2004 16:28
[script] E-mail webmaster error page BobbyDouglas PHP Code Library 0 19-Aug-2003 20:10
3 line sql querry causes error, but i cant find the raeson norok MySQL / PHP Forum 2 01-Jul-2003 09:06

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 21:08.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.