Thread: help with multiple switch statements and arrays

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    4

    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[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.

    Thanks for any help that can be provided.
    Last edited by dangalong801; 05-22-2011 at 10:22 PM. Reason: updated desk check

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by dangalong801 View Post
    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. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,490
    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[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);
    }
    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.

    Lastly, read this
    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).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    May 2011
    Posts
    4
    Quote Originally Posted by tabstop View Post
    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. #5
    Registered User
    Join Date
    May 2011
    Posts
    4
    Quote Originally Posted by Salem View Post
    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. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,490
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    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.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Registered User
    Join Date
    May 2011
    Posts
    4
    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.
    Last edited by dangalong801; 05-23-2011 at 02:02 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. switch statements.
    By elixon in forum C Programming
    Replies: 2
    Last Post: 11-30-2006, 05:57 PM
  2. switch statements
    By joshua in forum C Programming
    Replies: 3
    Last Post: 11-21-2005, 03:26 AM
  3. If, Else and switch statements
    By Soul.India in forum C Programming
    Replies: 4
    Last Post: 11-10-2003, 10:01 AM
  4. arrays within switch statements
    By divinyl in forum C++ Programming
    Replies: 6
    Last Post: 07-17-2003, 01:56 PM
  5. help with switch statements
    By Wexy in forum C Programming
    Replies: 3
    Last Post: 11-06-2002, 05:44 PM