Thread: Help debugging a simple program

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    20

    Help debugging a simple program

    My assignment is to create a program that has 3 options:

    a) add in series
    b) add in parallel
    c) quit

    So far this is what I have. However, I'm stuck in the parallel function, I can not figure it out why I cant divide(tried multiplying but I am not able to either) and output the result. I have tried making my variables floats, converting integers to variables, using gets() instead of scanf. It will not even print "sum = u_input;" but when I added with sum like in the series() It will print out the result. Whatever I do It keeps printing out 0 as a total. I am obviously missing a huge detail (oxymoron?). If I am missing any info that would make the debugging any easier please let me know

    -Antonio

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    series()
    {
    int u_input = 0;
    int v_counter = 0;
    int sum = 0;
    
        do
        {
            printf("Enter resistor %d value, 0 to calculate: ", v_counter += 1);
    
            scanf("%d", &u_input);
    
            sum = sum + u_input;
    
        }while(u_input != 0);
    
        printf("\n\nThe sum of the resistor in series is: %d\n\n", sum);
    
        system("pause"); //scanf y poner enter any letter to continue would probly be more elegant
    
        main();
    
    }
    
    parallel()
    {
    int u_input = 0;
    int v_counter = 0;
    int sum = 0;
    
        do
        {
            printf("Enter resistor %d value, 0 to calculate:", v_counter += 1);
    
            scanf("%d", &u_input);
    
            sum = u_input; //PROBLEM
    
        }while(u_input != 0);
    
        printf("\n\nThe sum of the resistor in parallel is: %d\n\n", sum);
    
        system("pause"); //scanf y poner enter any letter to continue would probly be more elegant
    
        main();
    
    }
    
    menu()
    {
    char choice;
    
        for(;;)
    
        {
            system("cls");
    
            printf("This program will calculate resistance value in either series or parallel\n\n");
    
            printf("1) Calculate in series\n");
    
            printf("2) Calculate in parallel\n");
    
            printf("q) quit program\n");
    
            choice = getchar();
    
            switch(choice)
            {
                case '1':
                system("cls");
                series();
                break;
    
                case'2':
                system("cls");
                parallel();
                break;
    
                case'q':
                case'Q':
                system("cls");
                main();
                break;
    
            }
        }
    }
    
    int main()
    {
    
        menu();
    
        return(0);
    
        //Code::Blocks IDE has built in function that prevents screen from closing when program has been terminated
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    lines 74, 79, 85 ... lose the cls calls... nothing annoys an operator more than a screen that keeps going blank.

    You should check Ohm's law for parallel resistence... the correct calulation is 1.0 / (1.0/R1 + 1.0/R2 + 1.0/R3 ... 1.0/Rn) ... so basically you need to calulate the reciprocal of the sum of reciprocals... to get the right answer you will have to do this with floating point math.
    Code:
    float sum;
    int u_input;
    
    sum += (1.0 / u_input);
    
    // after the loop...
    
    sum = 1.0 / sum;
    You do have the correct forumula for series resistence where the total is merely the sum of all resistors.

    Do not call main() from inside a function... you will eventually blow up your program's stack. Use the return keyword and let the program find it's own way back.

    Functions, must specify a return type. If you are not returing a value and not feeding in values use ...
    Code:
    void series( void )
    
    void parallel ( void )
    
    void menu ( void )
    If you do not specify the compiler will make guesses ... and most often it gets it wrong.

    Turn up your compiler's warning levels to the max and do not ignore what it has to say... it should have been warning you about all this stuff.
    Last edited by CommonTater; 11-06-2011 at 11:13 PM.

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    20
    Thanks for pointing out the rest of the errors. Going back to the parallel formula (and dividing and multiplying in general). If user inputs 0 for multiplication, the result will always print out zero at the end and it will crash for division. How can I make this program exit with 0 as the user input?

  4. #4
    Registered User
    Join Date
    Jun 2008
    Posts
    20
    okay! i fixed my code and it works like I wanted. Can you please critiqued it and comment how I could make more efficient.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int series()
    {
    int u_input = 0;
    int v_counter = 0;
    int sum = 0;
    
        do
        {
            printf("Enter resistor %d value, 0 to calculate: ", v_counter += 1);
    
            scanf("%d", &u_input);
    
            sum = sum + u_input;
    
        }while(u_input != 0);
    
        printf("\n\nThe sum of the resistor in series is: %d\n\n", sum);
    
        system("pause"); //scanf y poner enter any letter to continue would probly be more elegant
    
        return 0;
    
    }
    
    int parallel()
    {
    float u_input = 0.0;
    int v_counter = 0;
    float sum = 0.0;
    
        do
        {
            printf("Enter resistor %d value, 0 to calculate:", v_counter += 1);
    
            scanf("%f", &u_input);
    
            if (u_input == 0)
    
                break;
    
            sum += (1/u_input);
    
        }while(u_input != 0);
    
        printf("\n\nThe sum of the resistor in parallel is: %f\n\n", 1/sum);
    
        system("pause"); //scanf y poner enter any letter to continue would probly be more elegant
    
        return 0;
    
    }
    
    int menu()
    {
    char choice;
    
        for(;;)
    
        {
            system("cls");
    
            printf("This program will calculate resistance value in either series or parallel\n\n");
    
            printf("1) Calculate in series\n");
    
            printf("2) Calculate in parallel\n");
    
            printf("q) quit program\n");
    
            choice = getchar();
    
            switch(choice)
            {
                case '1':
                system("cls");
                series();
                break;
    
                case'2':
                system("cls");
                parallel();
                break;
    
                case'q':
                case'Q':
                system("cls");
                return 0;
                break;
    
            }
        }
    }
    
    int main()
    {
    
        menu();
    
        return 0;
    
        //Code::Blocks IDE has built in function that prevents screen from closing when program has been terminated
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help debugging program.
    By martin249 in forum C Programming
    Replies: 5
    Last Post: 09-18-2010, 09:35 AM
  2. Replies: 5
    Last Post: 12-01-2009, 03:59 PM
  3. A simple question about GDB debugging
    By meili100 in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 02:05 PM
  4. Help debugging my program
    By shoobsie in forum C Programming
    Replies: 4
    Last Post: 07-05-2005, 07:14 AM
  5. help with simple debugging
    By Wexy in forum C Programming
    Replies: 3
    Last Post: 11-19-2002, 07:26 PM