Code:
/* Circalc2.exe, Calculator for Series Circuits (Second Edition), C Version */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(void){
#define T pow(10,12)
#define G pow(10,9)
#define M pow(10,6)
#define k pow(10,3)
#define z pow(10,0)
#define m pow(10,-3)
#define u pow(10,-6)
#define n pow(10,-9)
#define p pow(10,-12)
double TV,TA,TO,TW;
double GV,GA,GO,GW;
double MV,MA,MO,MW;
double kV,kA,kO,kW;
double mV,mA,mO,mW;
double uV,uA,uO,uW;
double nV,nA,nO,nW;
double pV,pA,pO,pW;
double zV,zA,zO,zW;
int engV,engI,engR,engP;
int form;
double V,I,R,P;
char res;
printf("Welcome to Circalc2!\n\n");
printf("Directions:\nThis is an electronic series circuit calculator based on Ohm's Law.\n");
printf("Select which formulas you need from the following numbered menu,\nthen enter two known values to calculate the other two you're missing.\n\n");
printf("Note: All answers will be in base quantities of x10^0.\n");
printf("\tEnter numbers only.\n\tEngineering notation is only supported in numerical format.\n");
printf("\tErrors will occur if invaid characters are entered at the prompts.\n\n");
printf("Guide: Voltage (V) | Current (I) | Resistance (R) | Power (P) | Total (t)\n\n");
/*
desk check:
V (voltage) = 20 volts (20 x10^ 0)
I (current) = 0.0025 amperes (2.5 x10^ -3)
R (resistance) = 8000 ohms (8000 x10^ 0)
P (power) = 0.05 watts (50 x10^ -3)
*/
double ter_ary[5];
ter_ary [1]=V*T;
ter_ary [2]=I*T;
ter_ary [3]=R*T;
ter_ary [4]=P*T;
TV = ter_ary [1];
TA = ter_ary [2];
TO = ter_ary [3];
TW = ter_ary [4];
double gig_ary[5];
gig_ary [1]=V*G;
gig_ary [2]=I*G;
gig_ary [3]=R*G;
gig_ary [4]=P*G;
GV = gig_ary [1];
GA = gig_ary [2];
GO = gig_ary [3];
GW = gig_ary [4];
double meg_ary[5];
meg_ary [1]=V*M;
meg_ary [2]=I*M;
meg_ary [3]=R*M;
meg_ary [4]=P*M;
MV = meg_ary [1];
MA = meg_ary [2];
MO = meg_ary [3];
MW = meg_ary [4];
double kil_ary[5];
kil_ary [1]=V*k;
kil_ary [2]=I*k;
kil_ary [3]=R*k;
kil_ary [4]=P*k;
kV = kil_ary [1];
kA = kil_ary [2];
kO = kil_ary [3];
kW = kil_ary [4];
double zer_ary[5];
zer_ary [1]=V*z;
zer_ary [2]=I*z;
zer_ary [3]=R*z;
zer_ary [4]=P*z;
zV = zer_ary [1];
zA = zer_ary [2];
zO = zer_ary [3];
zW = zer_ary [4];
double mil_ary[5];
mil_ary [1]=V*m;
mil_ary [2]=I*m;
mil_ary [3]=R*m;
mil_ary [4]=P*m;
mV = mil_ary [1];
mA = mil_ary [2];
mO = mil_ary [3];
mW = mil_ary [4];
double mic_ary[5];
mic_ary [1]=V*u;
mic_ary [2]=I*u;
mic_ary [3]=R*u;
mic_ary [4]=P*u;
uV = mic_ary [1];
uA = mic_ary [2];
uO = mic_ary [3];
uW = mic_ary [4];
double nan_ary[5];
nan_ary [1]=V*n;
nan_ary [2]=I*n;
nan_ary [3]=R*n;
nan_ary [4]=P*n;
nV = nan_ary [1];
nA = nan_ary [2];
nO = nan_ary [3];
nW = nan_ary [4];
double pic_ary[5];
pic_ary [1]=V*p;
pic_ary [2]=I*p;
pic_ary [3]=R*p;
pic_ary [4]=P*p;
pV = pic_ary [1];
pA = pic_ary [2];
pO = pic_ary [3];
pW = pic_ary [4];
do{
printf("\nWhich formula set do you need now?\n\n");
printf("\t1: V=I*R = current * resistance = voltage\n\t P=(I*I)*R = current^2 * resistance = power\n");
printf("\tEnter the current and resistance to get the voltage and power.\n\n");
printf("\t2: I=V/R = voltage / resistance = current\n\t P=(V*V)/R = voltage^2 / resistance = power\n");
printf("\tEnter the voltage and resistance to get the current and power.\n\n");
printf("\t3: R=V/I = voltage / current = resistance\n\t P=V*I = voltage * current = power\n");
printf("\tEnter the voltage and current to get the resistance and power.\n\n");
printf("\t4: V=P/I = power / current = voltage\n\t R=P/(I*I) = power / current^2 = resistance\n");
printf("\tEnter the power and current to get the voltage and resistance.\n\n");
printf("\t5: V=sqrt(P*R) = square root of power * resistance = voltage\n\t I=sqrt(P/R) = square root of power / resistance = current\n");
printf("\tEnter the power and resistance to get the voltage and current.\n\n");
printf("\t6: I=P/V = power / voltage = current\n\t R=(V*V)/P = voltage^2 / power = resistance\n");
printf("\tEnter the power and voltage to get the current and resistance.\n\n");
printf("Enter the number that corresponds to your choice of formulas (1-6): ");
scanf("%d",&form);
switch(form){
case 1:
if(form==1)
printf("total current (It) in (x)amperes: ");
scanf("%lf",&I);
printf("I[ENG]x10^: ");
scanf("%d",&engI);
printf("total resistance (Rt) in (x)ohms: ");
scanf("%lf",&R);
printf("R[ENG]x10^: ");
scanf("%d",&engR);
break;
case 2:
if(form==2)
printf("total voltage (Vt) in (x)volts: ");
scanf("%lf",&V);
printf("V[ENG]x10^: ");
scanf("%d",&engV);
printf("total resistance (Rt) in (x)ohms: ");
scanf("%lf",&R);
printf("R[ENG]x10^: ");
scanf("%d",&engR);
break;
case 3:
if(form==3)
printf("total voltage (Vt) in (x)volts: ");
scanf("%lf",&V);
printf("V[ENG]x10^: ");
scanf("%d",&engV);
printf("total current (It) in (x)amperes: ");
scanf("%lf",&I);
printf("I[ENG]x10^: ");
scanf("%d",&engI);
break;
case 4:
if(form==4)
printf("total power (Pt) in (x)watts: ");
scanf("%lf",&P);
printf("P[ENG]x10^: ");
scanf("%d",&engP);
printf("total current (It) in (x)amperes: ");
scanf("%lf",&I);
printf("I[ENG]x10^: ");
scanf("%d",&engI);
break;
case 5:
if(form==5)
printf("total power (Pt) in (x)watts: ");
scanf("%lf",&P);
printf("P[ENG]x10^: ");
scanf("%d",&engP);
printf("total resistance (Rt) in (x)ohms: ");
scanf("%lf",&R);
printf("R[ENG]x10^: ");
scanf("%d",&engR);
break;
case 6:
if(form==6)
printf("total power (Pt) in (x)watts: ");
scanf("%lf",&P);
printf("P[ENG]x10^: ");
scanf("%d",&engP);
printf("total voltage (Vt) in (x)volts: ");
scanf("%lf",&V);
printf("V[ENG]x10^: ");
scanf("%d",&engV);
break;
default:
if(form<1||form>6)
printf("\nInvalid entry. Please restart the program to try again.\n\n");
system("pause");
exit(0);
}
switch(engV){
case 7:
if(engV==12)
printf("total voltage (Vt) = %d volts",TV);
break;
case 8:
if(engV==9)
printf("total voltage (Vt) = %d volts",GV);
break;
case 9:
if(engV==6)
printf("total voltage (Vt) = %d volts",MV);
break;
case 10:
if(engV==3)
printf("total voltage (Vt) = %d volts",kV);
break;
case 11:
if(engV==-3)
printf("total voltage (Vt) = %d volts",mV);
break;
case 12:
if(engV==-6)
printf("total voltage (Vt) = %d volts",uV);
break;
case 13:
if(engV==-9)
printf("total voltage (Vt) = %d volts",nV);
break;
case 14:
if(engV==-12)
printf("total voltage (Vt) = %d volts",pV);
break;
case 15:
if(engV==0)
printf("total voltage (Vt) = %d volts",zV);
break;
default:
if(engV!=12||engV!=9||engV!=6||engV!=3||engV!=0||engV!=-3||engV!=-6||engV!=-9||engV!=-12)
printf("Invalid entry. Please restart the program to try again.");
system("pause");
exit(0);
}
switch(engI){
case 16:
if(engI==12)
printf("total current (It) = %d amperes",TA);
break;
case 17:
if(engI==9)
printf("total current (It) = %d amperes",GA);
break;
case 18:
if(engI==6)
printf("total current (It) = %d amperes",MA);
break;
case 19:
if(engI==3)
printf("total current (It) = %d amperes",kA);
break;
case 20:
if(engI==-3)
printf("total current (It) = %d amperes",mA);
break;
case 21:
if(engI==-6)
printf("total current (It) = %d amperes",uA);
break;
case 22:
if(engI==-9)
printf("total current (It) = %d amperes",nA);
break;
case 23:
if(engI==-12)
printf("total current (It) = %d amperes",pA);
break;
case 24:
if(engI==0)
printf("total current (It) = %d amperes",zA);
break;
default:
if(engI!=12||engI!=9||engI!=6||engI!=3||engI!=0||engI!=-3||engI!=-6||engI!=-9||engI!=-12)
printf("Invalid entry. Please restart the program to try again.");
system("pause");
exit(0);
}
switch(engR){
case 25:
if(engR==12)
printf("total resistance (Rt) = %d ohms",TO);
break;
case 26:
if(engR==9)
printf("total resistance (Rt) = %d ohms",GO);
break;
case 27:
if(engR==6)
printf("total resistance (Rt) = %d ohms",MO);
break;
case 28:
if(engR==3)
printf("total resistance (Rt) = %d ohms",kO);
break;
case 29:
if(engR==-3)
printf("total resistance (Rt) = %d ohms",mO);
break;
case 30:
if(engR==-6)
printf("total resistance (Rt) = %d ohms",uO);
break;
case 31:
if(engR==-9)
printf("total resistance (Rt) = %d ohms",nO);
break;
case 32:
if(engR==-12)
printf("total resistance (Rt) = %d ohms",pO);
break;
case 33:
if(engR==0)
printf("total resistance (Rt) = %d ohms",zO);
break;
default:
if(engR!=12||engR!=9||engR!=6||engR!=3||engR!=0||engR!=-3||engR!=-6||engR!=-9||engR!=-12)
printf("Invalid entry. Please restart the program to try again.");
system("pause");
exit(0);
}
switch(engP){
case 34:
if(engP==12)
printf("total power (Pt) = %d watts",TW);
break;
case 35:
if(engP==9)
printf("total power (Pt) = %d watts",GW);
break;
case 36:
if(engP==6)
printf("total power (Pt) = %d watts",MW);
break;
case 37:
if(engP==3)
printf("total power (Pt) = %d watts",kW);
break;
case 38:
if(engP==-3)
printf("total power (Pt) = %d watts",mW);
break;
case 39:
if(engP==-6)
printf("total power (Pt) = %d watts",uW);
break;
case 40:
if(engP==-9)
printf("total power (Pt) = %d watts",nW);
break;
case 41:
if(engP==-12)
printf("total power (Pt) = %d watts",pW);
break;
case 42:
if(engP==0)
printf("total power (Pt) = %d watts",zW);
break;
default:
if(engP!=12||engP!=9||engP!=6||engP!=3||engP!=0||engP!=-3||engP!=-6||engP!=-9||engP!=-12)
printf("Invalid entry. Please restart the program to try again.");
system("pause");
exit(0);
}
printf("Do you want to do more calculations? Enter Y to continue or N to quit: ");
scanf(" %c",&res);
}while(res=='y'||res=='Y');
printf("\nThank you for using Circalc!\n\n");
system("pause");
return(0);
}
The problem I am having is the program runs without errors from the compiler, so there's no syntax errors, but it doesn't do what it's supposed to. From what I can tell, it executes the code in the first switch statement ok, but then it just skips to my error message for invalid input and doesn't recognize any of the switch statements that come after that. At least that's what it looks like to me, and I don't know why. Hopefully this is just a simple problem I am overlooking that can be resolved with a little advice, but feel free to tinker with the code if you like.