我对此很陌生,所以请尝试做笔记。提前致谢。 编写程序以计算最多5个电阻器电路的并联和串联电路。 该程序必须: 提示电路中电阻的数量 提示用户选择串联或并联 对于串联电路,输出将是总电阻和每个电阻两端的压降。 对于并联电路,输出将是总电阻和通过每个电阻的电流。
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;
}
答案 0 :(得分:0)
我相信您忘记了将v_D
声明为数组。
您将变量声明为double(不是数组):
double v_D= 0.0;
但是,在代码中,您将其视为数组:
v_D[i]
和v_D[i]++
。
要进行审查,可能要声明如下:
static const unsigned int MAX_RESISTORS = 5;
double Resistors[MAX_RESISTORS];
double voltage_drops[MAX_RESISTORS];
我建议暂时离开您的代码一段时间,然后再回来进行客观检查。 :-)