dimanche 22 mars 2015

Or-condition in a C program doesn't give the desired result

I'm writing a program which identifies what triangle a user can make by inserting 3 integers. After which based on the conditions and statements it goes through, it identifies whether the triangle they can make is a EQUILATERAL(all sides the same), or SCALENE(No sides the same), or ISOSCELES(Two sides the same). The user is to enter a value between 1-15cm in each occasion its asked. If the user types anything below or above the limit, the program would advise them it cannot be done and simply stop.


Here is my code



/*** Purpose: To create a C program which takes 3 integers and produces a result that shows which triangle(If valid) they have chosen.***/

#include <stdio.h>
int main()
{
/*** Declaring triangle variable sides ****/

int sideA;
int sideB;
int sideC;

printf("Lets explore triangles! Please insert a value for side 'A' of your triangle.\n");
printf(" Ranging from 1-15cm.\n");
scanf("%d%*c", &sideA);

printf(" Now insert a value for side 'B' of your triangle ranging from 1-15cm.\n");
scanf("%d%*c", &sideB);

printf(" And finally, insert a value for side C of your triangle ranging from 1-15cm.\n");
scanf("%d%*c", &sideC);

/*** List of conditions based on user input to identify if the triangle is valid and if so, what type of triangle they get***/

if(sideA || sideB || sideC <=0)
{
printf(" You cannot have a triangle with any side having a value of 0.\n");
}
else
if(sideA || sideB || sideC >15)
{
printf("Please insert a value between 1cm-15cm only\n.");
}
else
if(sideA && sideB==sideC || sideB && sideC==sideA || sideC && sideA==sideB) /*** Code to determine EQUILATERAL TRIANGLE***/
{
printf(" Your input creates a valid EQUILATERAL triangle.\n");
}
else
if(sideA==sideB || sideB==sideC || sideC==sideA)/*** Code to determine ISOSCELES TRIANGLE***/
{
printf("Your input creates a valid ISOSCELES triangle.\n");
}
else
if(sideA!= sideB && sideC || sideB!= sideC && sideA || sideC!= sideA && sideB)/*** Code to determine SCALENE triangle ***/
{
printf("Your input creates a valid SCALENE triangle.\n");
}
else
{
printf("You have inserted invalid range of values, as a result your triangle is invalid.\n");
printf("Please restart the program by closing it and opening it again to retry\n.");
printf("Goodbye.\n");
}
return(0);
}


The image below is the result of when the program is compiled and running. This is the result when the program is running.


Basically whether i insert a valid integer or invalid integer, it still gives the same result as the screenshot.


I'm still learning C so I may be missing a thing here and there, so any help is really much appreciated.


Also I want to be able to add a conditional while loop in order to stop the user from inserting an incorrect value or inserting a character when an integer is required. Where about's would I insert the while loop and how would I write it exactly?


Aucun commentaire:

Enregistrer un commentaire