Switch Function Problems!
Author |
Message |
blissfullydysphoric
|
Posted: Thu Oct 02, 2008 7:33 pm Post subject: Switch Function Problems! |
|
|
Hi, I am having troubles with my switch. All my program does right now is takes the users input and uses it to determine what the value is for that specific type of pipeing. code: |
#include <stdio.h>
#include <math.h>
int main(void)
{
double pressure_drop; //pressure drop in pounds per square inch (psi) / foot of pipe
double flow; //flow in gallons per minute
double diameter; //inside pipe diameter (inches)
double c_factor; //factor (roughness or friction loss coefficient)
int type; //type of pipeing
int age; //age of the cast-iron pipeing
printf ("Welcome to the Pressure Drop Calculator\n");
printf ("1. Cast-Iron\n");
printf ("2. Concrete\n");
printf ("3. Copper\n");
printf ("4. Steel\n");
printf ("5. PVC (polyvinyl chloride)\n");
printf ("Please enter the type of pipe according to the numbers shown above: ");
scanf ("%d", &type);
switch (type) {
case 1: printf("What is the age of the cast-iron pipeing: ");
scanf ("%d",age);
if (0 <= age && age > 10)
c_factor = 130;
else if (10 <= age && age > 20)
c_factor = 110;
else if (20 <= age && age > 30)
c_factor = 95;
else if (30 <= age && age > 40)
c_factor = 83;
else c_factor = 70;
break;
case 2: c_factor = 100;
break;
case 3: c_factor =150;
break;
case 4: c_factor =120;
break;
case 5: c_factor =150;
break;
default : printf("Unknown pipe type %d\n", type);
}
printf ("The C factor of the pipe is %lf", c_factor);
return 0;
}
|
It works for all of the cases except for the first because I cannot get the if statements to work :S When I enter an age in the program crashes. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Clayton
|
Posted: Thu Oct 02, 2008 7:37 pm Post subject: RE:Switch Function Problems! |
|
|
c: | if (0 <= age && age > 10)
c_factor = 130;
else if (10 <= age && age > 20)
c_factor = 110;
else if (20 <= age && age > 30)
c_factor = 95;
else if (30 <= age && age > 40)
c_factor = 83;
else c_factor = 70; |
The logic in your conditions here seem a little off...
rewriting it we get this:
c: |
if (age >= 0 && age > 10)
c_factor = 130;
else if (age >= 20 && age > 30)
c_factor = 95;
...
|
What exactly are you trying to do? |
|
|
|
|
|
blissfullydysphoric
|
Posted: Thu Oct 02, 2008 7:42 pm Post subject: RE:Switch Function Problems! |
|
|
Basically if the age is between 0 and 10 the c_factor is 130, likewise if the age is between 20 and 30 the c_factor is 95. If the age is 0 then the c_Factor is still 130 and if the age is 20 then the c_factor is 95. |
|
|
|
|
|
blissfullydysphoric
|
Posted: Thu Oct 02, 2008 8:02 pm Post subject: RE:Switch Function Problems! |
|
|
Thanks for the help you did solve my problem, I didn't realize the greater than signs were going the wrong direction :S |
|
|
|
|
|
|
|