#ifndef CIRCUIT_H
#define CIRCUIT_H
#include <iostream>
using namespace std;

class Resistor
{
    public:
    void setResistance(double);
    double getResistance();
    double GenerateRandomCircuit();
    double calctotalResistance();
    void GenerateQuiz();
    int op[7],res[8];

    private:
    double r;
};
#endif // CIRCUIT_H

#include "Circuit.h"
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;

void Resistor::setResistance(double r1)
{
    r=r1;
}

double Resistor::getResistance()
{
    return r;
}
double Resistor::GenerateRandomCircuit()
{
    int j=0,k=0,x;
    double y,z;
    srand(time(NULL));
    int random=rand()%6+3;
    for(int i=1;i<=random;i++)
    {
        x=1+rand()%9;
        y=(1+rand()%9)*0.1;
        z=x+y;
        int sign=rand()%2+1;
        if(sign==1)
        {
            op[x]='+';
            cout<<"+";
        }else if(sign==2)
        {
            op[x]='/';
            cout<<"/";
        }
        cout<<fixed<<setprecision(1)<<z;
    }
    return z;
}

double Resistor::calctotalResistance()
{
    int x=0,y=0;
    double tempres[99]={};
    double temptotal,total;
    for(int i=0;i<4;i++)
    {
        if(op[x]=='/')  //If operator of symbol "/", it will convert temporary resistor to 1/(value of resistor)
        {
            temptotal =((tempres[i]*tempres[i+1])/(tempres[i]+tempres[i+1]));
            i++;
        }else if(op[x]=='+')
        {
            total=tempres[x]+tempres[x+1];
        }else if(i==3)
        {
            total=tempres[x]+tempres[x+1];
        }
    }
    return total;
}

void Resistor::GenerateQuiz()
{
    Resistor a;
    int mark=0;
    double answer,resistance;
    string formula;
    for(int i=1;i<=5;i++)
    {
        cout<<"Circuit "<<i<<" formula is "; a.GenerateRandomCircuit(); cout<<endl;
        cout<<"Enter circuit "<<i<<" total resistance:";
        cin>>answer;
        resistance=calctotalResistance();
        if(resistance==answer)
        {
            mark++;
        }
    }
    cout<<endl;
    cout<<"Your mark for this quiz is "<<mark<<"/5"<<endl;
}

#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include "Circuit.h"
using namespace std;

int main()
{
int choice,quiz;
string formula;
double formula1,totalres;
Resistor b;
cout <<"*** Welcome to Total Resistance Calculator Program ***"<<endl;
cout<<"1. Enter circuit formula."<<endl;
cout<<"2. Generate circuit randomly."<<endl;
cout<<"3. Show circuit formula."<<endl;
cout<<"4. Calculate total resistance."<<endl;
cout<<"5. Play a quiz."<<endl;
while(choice!=-1)
{
    cout<<"Your choice [1-5] : ";
    cin>>choice;
    switch(choice)
    {
        case 1:
            cout<<"Enter circuit formula: ";
            cin>>formula;
            break;
        case 2:
            cout<<"Random circuit is generated."<<endl;
            break;
        case 3:
            cout<<"Circuit formula is ";
            b.GenerateRandomCircuit();
            cout<<endl;
            break;
        case 4:
            cout<<"Circuit total resistance is "<<setprecision(1)<<b.calctotalResistance()<<" ohm"<<endl;
            break;
        case 5:
            cout<<"Quiz is selected."<<endl;
            b.GenerateQuiz();
            break;
    }
}
return 0;
}

I had write the coding to calculate total resistance of circuit by using series and parallel formula, but it keeps prompt out total resistance is 0 ohm. Can anyone help me to solve it?

Dani AI

Generated

Quick diagnosis (why the program prints 0):
The calculation routine never sees the generated resistor values. Generated values are either not stored in the object state the calculator reads, or they are written to the wrong indices (out-of-bounds). The calculation loop reads an uninitialized array (tempres) and returns an uninitialized total. srand(time(NULL)) is being called inside the generator (seed once only), and GenerateQuiz builds a local Resistor a but calls calctotalResistance() on the wrong object. Also choice in main is used before initialization. As suggested, stepping the code or printing the arrays will immediately reveal these symptoms.

Concrete, prioritized fixes

  • Keep the circuit in paired containers: one vector<double> for resistor values and one vector<char> for operators. Invariant: values.size() == ops.size()+1. Use push_back so indices stay consistent and safe.
  • Generate once: call srand(time(NULL)) in main, then create n resistors and n-1 ops with a simple loop (use index i, not a random index to store).
  • Calculate deterministically: initialize total = values[0]; then for each op i combine with values[i+1]. For series (+) do total += next; for parallel (/) do total = (total * next) / (total + next). Return total.
  • In GenerateQuiz call the calculator on the same object that stored the last generated circuit (or call a.calctotalResistance() if a was used to generate it).
  • Never compare doubles with ==; use an epsilon like fabs(user - correct) < 0.05. Initialize loop/control variables (e.g., choice = 0) and check for division-by-zero before parallel formula.

Minimal illustrative snippet (keeps logic different from the original submission):

#include <vector>
#include <cstdlib>
#include <ctime>

std::vector<double> resistors;
std::vector<char> ops;

void makeRandomCircuit() {
    resistors.clear();
    ops.clear();
    int n = 3 + rand()%6; // 3..8
    for (int i = 0; i < n; ++i) {
        double v = (1 + rand()%9) + (rand()%10)/10.0;
        resistors.push_back(v);
        if (i < n-1) ops.push_back((rand()%2) ? '+' : '/');
    }
}

double totalResistance() {
    if (resistors.empty()) return 0.0;
    double total = resistors[0];
    for (size_t i = 0; i < ops.size(); ++i) {
        double next = resistors[i+1];
        if (ops[i] == '+') total += next;
        else total = (total * next) / (total + next); // parallel
    }
    return total;
}

Final notes
Fix the storage/indexing bugs first, then the control-flow issues (choice, seeding). Use simple prints or a debugger to confirm resistors and ops before calling the calculator. After those corrections the quiz comparison and output should behave as expected.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.