Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,413 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,441 people online right now. Registration is fast and FREE... Join Now!




c++ 3integers

 
Reply to this topicStart new topic

c++ 3integers

cystal1
14 Oct, 2008 - 10:04 PM
Post #1

New D.I.C Head
*

Joined: 1 Sep, 2008
Posts: 6

C++ 3integers can form 3 sides of triangle,pls help mi,i realli need help!!!!?
i just cnnot gt the (4,5,6) to be a triangle,
(8,8,8) to be equilateral n isosceles triangle.



include<iostream>
#include <iomanip>
using namespace std;

int main ()
{
int A,B,C, // 3 side lengths for triangle
S;


cout << "This program determines the type of triangle from the side lengths";

//Input 3 sides of triangle
cout << "\n\nEnter length of side A: ";
cin >> A;
cout << "\nEnter length of side B: ";
cin >> B;
cout << "\nEnter length of side C: ";
cin >> C;

//Determine the triangle types based on side equality
if ((A>0) && (B>0) && (C>0))
{
S=1;
}

else if((A>=0) && (B>=0) || (C>=0))
{
S=2;
}
else if((A == cool.gif || (B == C) || (A == C))
{
S=3;
}
else if ((A == cool.gif && (B == C))
{
S=4;
}


switch (S)
{
case 1:cout << "(" << A << "," << B << "," << C << ")is not a triangle" << endl;
break;
case 2:cout << "(" << A << "," << B << "," << C << ")is a triangle" <<endl;
break;
case 3:cout << "(" << A << "," << B << "," << C << ")is an equilateral triangle" <<endl;
break;
case 4:cout << "(" << A << "," << B << "," << C << ")is an isosceles triangle" <<endl;
break;
default:cout << "unknown";
break;
}

return 0;
}

output:
Enter 3 integers:4 5 6
(4,5,6) is a triangle

Enter 3 integers: 8 8 8
(8,8,8) is an equilateral triangle
(8,8,8) is an isosceles triangle
(8,8,8) is a triangle



User is offlineProfile CardPM
+Quote Post

OliveOyl3471
RE: C++ 3integers
14 Oct, 2008 - 10:13 PM
Post #2

It's all about the code ♥
Group Icon

Joined: 11 Jul, 2007
Posts: 1,629



Thanked: 18 times
Dream Kudos: 150
My Contributions
You need to change this:
cpp

//Determine the triangle types based on side equality
if ((A>0) && (B>0) && (C>0))
{
S=1;
}


If A and B and C are all greater than zero then S=1. So no matter what you put in, you're always going to get the ouput from case 1, which is that it is NOT a triangle.
Change the > signs to < signs and see how that works. And change the AND's (&&'s) to OR's (||'s).
I would also remove all the = from here. A side cannot be zero inches or feet or whatever.
cpp

else if((A>=0) && (B>=0) || (C>=0))//you also have OR where you should have another AND in this line
{
S=2;
}

I didn't try compiling it, but I'm pretty sure this is your problem.

If it was me I wouldn't bother with the switch at all, unless it's a required part of the assignment. You're always going to end up with S=1 or S=2 if you keep the switch, even with my changes. Just put the cout statements instead of the S=1 or whatever. You need some of your ifs to be nested as well.

Like this:
cpp


//Determine the triangle types based on side equality
if ((A<=0) || (B<=0) || (C<=0))
{
cout << "(" << A << "," << B << "," << C << ")is not a triangle" << endl;
}

else if((A>0) && (B>0) && (C>0))
{
cout << "(" << A << "," << B << "," << C << ")is a triangle" <<endl;

if ((A == cool.gif && (B == C))
{
cout << "(" << A << "," << B << "," << C << ")is an equilateral triangle" <<endl;
}
else if((A == cool.gif || (B == C) || (A == C))
{
cout << "(" << A << "," << B << "," << C << ")is an isosceles triangle" <<endl;
}

}
else
{
cout << "unknown";
}




This post has been edited by OliveOyl3471: 14 Oct, 2008 - 10:58 PM
User is online!Profile CardPM
+Quote Post

cystal1
RE: C++ 3integers
15 Oct, 2008 - 01:41 AM
Post #3

New D.I.C Head
*

Joined: 1 Sep, 2008
Posts: 6

QUOTE(OliveOyl3471 @ 14 Oct, 2008 - 11:13 PM) *

You need to change this:
cpp

//Determine the triangle types based on side equality
if ((A>0) && (B>0) && (C>0))
{
S=1;
}


If A and B and C are all greater than zero then S=1. So no matter what you put in, you're always going to get the ouput from case 1, which is that it is NOT a triangle.
Change the > signs to < signs and see how that works. And change the AND's (&&'s) to OR's (||'s).
I would also remove all the = from here. A side cannot be zero inches or feet or whatever.
cpp

else if((A>=0) && (B>=0) || (C>=0))//you also have OR where you should have another AND in this line
{
S=2;
}

I didn't try compiling it, but I'm pretty sure this is your problem.

If it was me I wouldn't bother with the switch at all, unless it's a required part of the assignment. You're always going to end up with S=1 or S=2 if you keep the switch, even with my changes. Just put the cout statements instead of the S=1 or whatever. You need some of your ifs to be nested as well.

Like this:
cpp


//Determine the triangle types based on side equality
if ((A<=0) || (B<=0) || (C<=0))
{
cout << "(" << A << "," << B << "," << C << ")is not a triangle" << endl;
}

else if((A>0) && (B>0) && (C>0))
{
cout << "(" << A << "," << B << "," << C << ")is a triangle" <<endl;

if ((A == cool.gif && (B == C))
{
cout << "(" << A << "," << B << "," << C << ")is an equilateral triangle" <<endl;
}
else if((A == cool.gif || (B == C) || (A == C))
{
cout << "(" << A << "," << B << "," << C << ")is an isosceles triangle" <<endl;
}

}
else
{
cout << "unknown";
}





the switch case is part of my assignment.so my programme onli need 2 use if-else n switch statement.









User is offlineProfile CardPM
+Quote Post

OliveOyl3471
RE: C++ 3integers
16 Oct, 2008 - 07:05 AM
Post #4

It's all about the code ♥
Group Icon

Joined: 11 Jul, 2007
Posts: 1,629



Thanked: 18 times
Dream Kudos: 150
My Contributions
QUOTE(cystal1 @ 15 Oct, 2008 - 04:41 AM) *

the switch case is part of my assignment.so my programme onli need 2 use if-else n switch statement.


In that case you can still use the switch, I just think it's not really necessary to this program.

Anyway since I did start helping you, I will continue.
Here's how you can do this with a switch. Note the order of things. Your switch statements need to be in pretty much this order so that the output will be correct.

cpp

#include<iostream>//you forgot the # sign

//your code here

//Determine the triangle types based on side equality
if ((A<=0) || (B<=0) || (C<=0))
{
S=1;
}
else if ((A == cool.gif && (B == C))
{
S=2;
}
else if((A == cool.gif || (B == C) || (A == C))
{
S=3;
}
else
{
S=4;
}

switch (S)
{
case 1:cout << "(" << A << "," << B << "," << C << ")is not a triangle" << endl;
break;
case 2:cout << "(" << A << "," << B << "," << C << ")is an equilateral triangle" <<endl;
break;
case 3:cout << "(" << A << "," << B << "," << C << ")is an isosceles triangle" <<endl;
break;
case 4:cout << "(" << A << "," << B << "," << C << ")is a triangle" <<endl;
break;
default:cout << "unknown";
break;
}



User is online!Profile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 12:36PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month