# Thread: help with multiple switch statements and arrays

1. ## help with multiple switch statements and arrays

Hi, I'm relatively new to C programming, (I've been reading a lot of tutorials and taken a few classes) and I need some help with something.

I have a program where I'm trying to use multiple switch statements in the same program to handle different sets of user input in different ways, but it's not working, and I can't figure out what I'm doing wrong.

Basically, I have already written a working program that does simple Ohm's Law calculations for electronic series circuits, but it requires the input of base quantities and doesn't allow for using engineering notation, which can get pretty tedious when there's a lot of zeros to type in for certain values. So I'm just trying to facilitate a way for the program to accept the notations in numerical format and still do the normal calculations afterwards, which seems like it should be simple enough.

And I'm just trying to get it to the point where it will accept the notations (certain powers of ten) as input, so I'm not worried about the making it do that for the output yet, so the output can still be in base quantities for now.

Anyway, please bear in mind that I am still pretty new at this, but here's my code so far:

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;
ter_ary =V*T;
ter_ary =I*T;
ter_ary =R*T;
ter_ary =P*T;
TV = ter_ary ;
TA = ter_ary ;
TO = ter_ary ;
TW = ter_ary ;

double gig_ary;
gig_ary =V*G;
gig_ary =I*G;
gig_ary =R*G;
gig_ary =P*G;
GV = gig_ary ;
GA = gig_ary ;
GO = gig_ary ;
GW = gig_ary ;

double meg_ary;
meg_ary =V*M;
meg_ary =I*M;
meg_ary =R*M;
meg_ary =P*M;
MV = meg_ary ;
MA = meg_ary ;
MO = meg_ary ;
MW = meg_ary ;

double kil_ary;
kil_ary =V*k;
kil_ary =I*k;
kil_ary =R*k;
kil_ary =P*k;
kV = kil_ary ;
kA = kil_ary ;
kO = kil_ary ;
kW = kil_ary ;

double zer_ary;
zer_ary =V*z;
zer_ary =I*z;
zer_ary =R*z;
zer_ary =P*z;
zV = zer_ary ;
zA = zer_ary ;
zO = zer_ary ;
zW = zer_ary ;

double mil_ary;
mil_ary =V*m;
mil_ary =I*m;
mil_ary =R*m;
mil_ary =P*m;
mV = mil_ary ;
mA = mil_ary ;
mO = mil_ary ;
mW = mil_ary ;

double mic_ary;
mic_ary =V*u;
mic_ary =I*u;
mic_ary =R*u;
mic_ary =P*u;
uV = mic_ary ;
uA = mic_ary ;
uO = mic_ary ;
uW = mic_ary ;

double nan_ary;
nan_ary =V*n;
nan_ary =I*n;
nan_ary =R*n;
nan_ary =P*n;
nV = nan_ary ;
nA = nan_ary ;
nO = nan_ary ;
nW = nan_ary ;

double pic_ary;
pic_ary =V*p;
pic_ary =I*p;
pic_ary =R*p;
pic_ary =P*p;
pV = pic_ary ;
pA = pic_ary ;
pO = pic_ary ;
pW = pic_ary ;

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.

Thanks for any help that can be provided. 2. Originally Posted by dangalong801 but it requires the input of base quantities and doesn't allow for using engineering notation,
I don't know why you believe that, but it isn't true. You can type "6.5e+03" at a prompt and everybody will be happy. 3. If your current compiler isn't giving you warnings, then get a better compiler.
Code:
```\$ gcc -Wall bar.c
bar.c: In function ‘main’:
bar.c:231: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘double’
bar.c:235: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘double’
bar.c:239: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘double’
bar.c:243: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘double’
bar.c:247: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘double’
bar.c:251: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘double’
bar.c:255: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘double’
bar.c:259: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘double’
bar.c:263: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘double’
bar.c:274: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘double’
bar.c:278: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘double’
bar.c:282: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘double’
bar.c:286: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘double’
bar.c:290: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘double’
bar.c:294: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘double’
bar.c:298: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘double’
bar.c:302: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘double’
bar.c:306: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘double’
bar.c:317: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘double’
bar.c:321: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘double’
bar.c:325: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘double’
bar.c:329: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘double’
bar.c:333: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘double’
bar.c:337: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘double’
bar.c:341: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘double’
bar.c:345: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘double’
bar.c:349: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘double’
bar.c:360: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘double’
bar.c:364: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘double’
bar.c:368: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘double’
bar.c:372: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘double’
bar.c:376: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘double’
bar.c:380: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘double’
bar.c:384: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘double’
bar.c:388: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘double’
bar.c:392: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘double’```
All those %d formats for printing a double need to be changed to %f

Then your code desperately needs to be indented so that people will feel at least inclined to read it.
SourceForge.net: Indentation - cpwiki

Here, this is what it should like when posted on a forum AND inside your IDE.
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,\n"
"then 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;
ter_ary = V * T;
ter_ary = I * T;
ter_ary = R * T;
ter_ary = P * T;
TV = ter_ary;
TA = ter_ary;
TO = ter_ary;
TW = ter_ary;

double gig_ary;
gig_ary = V * G;
gig_ary = I * G;
gig_ary = R * G;
gig_ary = P * G;
GV = gig_ary;
GA = gig_ary;
GO = gig_ary;
GW = gig_ary;

double meg_ary;
meg_ary = V * M;
meg_ary = I * M;
meg_ary = R * M;
meg_ary = P * M;
MV = meg_ary;
MA = meg_ary;
MO = meg_ary;
MW = meg_ary;

double kil_ary;
kil_ary = V * k;
kil_ary = I * k;
kil_ary = R * k;
kil_ary = P * k;
kV = kil_ary;
kA = kil_ary;
kO = kil_ary;
kW = kil_ary;

double zer_ary;
zer_ary = V * z;
zer_ary = I * z;
zer_ary = R * z;
zer_ary = P * z;
zV = zer_ary;
zA = zer_ary;
zO = zer_ary;
zW = zer_ary;

double mil_ary;
mil_ary = V * m;
mil_ary = I * m;
mil_ary = R * m;
mil_ary = P * m;
mV = mil_ary;
mA = mil_ary;
mO = mil_ary;
mW = mil_ary;

double mic_ary;
mic_ary = V * u;
mic_ary = I * u;
mic_ary = R * u;
mic_ary = P * u;
uV = mic_ary;
uA = mic_ary;
uO = mic_ary;
uW = mic_ary;

double nan_ary;
nan_ary = V * n;
nan_ary = I * n;
nan_ary = R * n;
nan_ary = P * n;
nV = nan_ary;
nA = nan_ary;
nO = nan_ary;
nW = nan_ary;

double pic_ary;
pic_ary = V * p;
pic_ary = I * p;
pic_ary = R * p;
pic_ary = P * p;
pV = pic_ary;
pA = pic_ary;
pO = pic_ary;
pW = pic_ary;

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);
}```
You've completely mis-understood the nature of switch/case.
Code:
```        switch (form) {
case 1:
if (form == 1)
printf("total current (It) in (x)amperes: ");```
Comparing form again is pointless, it can only ever be equal to 1 at this point in the code - the switch sees to that

Similarly,
Code:
```        switch (engV) {
case 7:
if (engV == 12)
```
This is NEVER going to happen.

A development process
The idea being is that you make sure an idea is going to work in say 50 lines of code, before you basically copy/paste the same conceptual mistake into 500 lines of code (which is what you have basically done). 4. Originally Posted by tabstop I don't know why you believe that, but it isn't true. You can type "6.5e+03" at a prompt and everybody will be happy.
I didn't know that, thanks for the info. 5. Originally Posted by Salem If your current compiler isn't giving you warnings, then get a better compiler.
I'm using Pelles C. I don't know of a better one to use, this is the one I got from my programming class.

All those %d formats for printing a double need to be changed to %f
Ok, I’ll do that.

Then your code desperately needs to be indented so that people will feel at least inclined to read it.
SourceForge.net: Indentation - cpwiki
Alright, I’ll look into that before I post any more.

You've completely mis-understood the nature of switch/case.
I’m not sure what you mean by that. I’m sure I don’t fully understand it yet, but I’m also sure that I haven’t “completely” misunderstand it either. You would have to be more specific about what you think I don’t understand. Remember, I am a beginner.

Comparing form again is pointless, it can only ever be equal to 1 at this point in the code - the switch sees to that
That’s good to know, because that’s the way my programming teacher taught me to do it (the way I had it).

Similarly,
Code:
```        switch (engV) {
case 7:
if (engV == 12)
```
This is NEVER going to happen.
Can you explain why? Can you give me any examples of what I could do instead?

Anyway, I did manage to figure out some of the stupid mistakes that weren’t pointed out here on my own, and I have made a lot of changes to the code now. The only problem I’m having now, it seems, is getting the extra math functions to work the way I want them to. For now it’s just spitting out zeros. But I think I’m done here. Thanks for trying to help, though. 6. Because engV CANNOT be 12, if it was just 7 the line before.

As far as I can see, none of your case n: if ( something == n ) code has any value at all. 7. Because that piece of code says "when engV is 7, only do something if it is 12". You just want case 12:
The number of the case isn't meant to be just whatever number you haven't used yet, it's the value that the input variable will be in order to run that bit of code. 8. The number 12 in that line didn't have anything to do with a case number, or it wasn't supposed to, it was just a number that the user was supposed to enter at a prompt and then scanf was to save it to the engV variable. I know now the way I was trying to use it was wrong, but that's what the intention for it was.

Because engV CANNOT be 12, if it was just 7 the line before.
Ok, but just so you know there was never supposed to be an instance where engV would equal 7, so that was not intentional.

Because that piece of code says "when engV is 7, only do something if it is 12". You just want case 12:
That's not what I wanted, the if(engV==12) was supposed to be if engV is equal to 12 due to the user having input that number in the first switch statement then do this, but obviously it doesn't work that way, which was one of my major problems I can see now.

The number of the case isn't meant to be just whatever number you haven't used yet, it's the value that the input variable will be in order to run that bit of code.
Ok, I guess that's where I'm getting the most confused here, because that's what I was assuming. But in that case I wouldn't know how to make the cases in a switch statement to work for things that don't use plain numbers, but I haven't tried that yet. Or maybe you just can't do that, I'm guessing.

Anyway, I think I'm going to go ahead and give up on this project now since I now know it's not necessary because Windows console supports using 'e' for notations, which I didn't know before. That was a great tip, so I'll just put some notes to that effect in my original version of the program and keep it that way until I figure out something else to do with it.

Thanks for the help, guys. I've learned some things I didn't know. Popular pages Recent additions 