Search code examples
c++dev-c++

how to implement the calculation of voltage drop?


I'm pretty new to this so try to put notes. Thanks in advance. Write a program to calculate parallel and series circuits for a circuit of up to 5 resistors. The program must: Prompt for the number of resistors in the circuit Prompt the user to select series or parallel For a series circuit the output will be the total resistance and the voltage drop across each resistor. For a parallel circuit the output will be the total resistance and the current through each resistor.

series circuit calculation:
total resistance= r1 +r2+rn
I=v/r_total
voltage drop in r1 = r1*I and so on until r5.
i'm trying to do the calculations for voltage drop using an array but its not working and i'm not sure why.

i'm getting an error and i'm not sure why



    #include <iostream>
    #include <string>
    #include <cmath>

    using namespace std;

    string circuitType(string x)
    {
        string P, S; //Parallel circuit (p) and series circuit (s)

        while (x!="P" && x!="S")
        {

        if (x=="P")
            {
            cout<<"     Parallel Circuit Calculator\n\n";
            }
        else if(x=="S")
            {
            cout<<"     Series Circuit Calculator\n\n";
            }
        else 
            {
            cout << "Please Enter the type of circuit (P/S):";
            cin >>x;
            }
        }
        return(x);
    }

    int main()
    {

        string userChoice = "Yes";
        string userans ;
        string P, S; //Parallel circuit (p) and series circuit (s)

        while (userChoice == "Yes")
        {
        double R [5];//[5] = {1, 2,3,4,5};//an array of Resistors 1 to 5

        double V, I; //Voltage and Current

        double e_R= 0.0; //Equivalent Resistance

        double r_T= 0.0; //Total resistance

        double v_D= 0.0; //Voltage drop

        cout <<"                Welcome to Circuit Calculator \n";
        cout <<"\n";

        cout <<"\n (Main) Please Enter the type of circuit (P/S):";
        cin >>userans;
        userans = circuitType(userans);

    if (userans == "S")
        {

    // a fuction for the prompt the user to enter resistance values

        cout<<"Enter the values for Resistors 1 to 5: \n";
        for(int i=0; i<=5;i++) 

    //implementing that i has a value more than 0and less than or equal                     
 to 5

            {
           cout<<"Enter R"<<i+1<<" = ";//output of Resistance values 
           from 1 to 5
           cin>>R[i];   
            }       
    //calculating Equivalent Resistance

       for (int i=0; i<=5;i++)
       r_T+= R[i];
       cout<< "The Total Resistance is "<<r_T<<"ohms.\n"; 

    // prompting the user to enter voltage value

        cout<< "enter V: ";
        cin >> V;

    //calculating current

        I = V/(r_T);
        cout<<"\n current = "<<I;

    // voltage drop at Resistors.

        for (int i=0; i<=5;i++) 

    //error 1 [Error] invalid types 'double[int]' for array subscript

        v_D[i] *= (R[i],I);

//  2[Error] name lookup of 'i' changed for ISO 'for' scoping [- 
    fpermissive]
//  3[Note] (if you use '-fpermissive' G++ will accept your code)
//  4[Error] invalid types 'double[int]' for array subscript

    cout << "The voltage drop at R"<<i <<"=" <<v_D[i]++<<"V\n";

        }

    //if resistance connected in parallel       
    //calculating equivalent resistance
    //eR=1/resistance;
    //printing current accross all resistance
    //I=voltage/resistance;

    //when circuit is connected in series   

        cout <<"Do you want to run calculator again?(Yes/No)\n";
        cin >>userChoice;
        cout <<"\n\n";
    }
        return 0;
    }

Solution

  • I believe you forgot to declare v_D as an array.

    You declare the variable as double (not an array): double v_D= 0.0;

    Yet, in the code, you treat it as an array: v_D[i] and v_D[i]++.

    To review, to the declaration might want to be:

    static const unsigned int MAX_RESISTORS = 5;
    double Resistors[MAX_RESISTORS];
    double voltage_drops[MAX_RESISTORS];
    

    I recommend walking away from your code for a while, then come back for an objective inspection. :-)