minor alteration required

Guys i have a small problem. i made a program coding where the impedance of two different types of circuits are to be calculated. it work fine but there is a small problem. okay let me just give a brief explanation about how this program should work. when u start the program, the user will be given a choice to either select a series circuit or a parallel circuit. once the user has selected the type of circuit, the programs goes on with by asking the user to key in R, L, C and f and then it calculates the impedance and displays the answer. the problem is that i need to make sure if the user keys in any other number to choose the circuit, it should display invalid input. for example
press 1 for series
press 2 for parallel
if the user presses any other number besides 1 and 2, it should display the "invalid input' statement. it does happen but u have to renter the values R,L,C and f all over again n then it displays the statement.
this is my program coding :

#include
#include
using namespace std;
void main()
{
double R, yr, L, yl, f, C, yc, option, conti, pi=3.142;
double xc, xl, z, radian, degree;
cout<<" RLC Impendance Calculator
";
cout<<"
";
do
{
cout<<"Choose a circuit

";
cout<<"Press 1 for RLC SERIES circuit
";
cout<<"Press 2 for RLC PARALLEL circuit

";
cout<<"Key in your choice
";
cin>>option;
cout<<"
";
cout<<"Key in the value of resistance 'R'
";
cin>>R;
cout<<"Key in the value of capacitance 'C'
";
cin>>C;
cout<<"Key in the value of inductance 'L'
";
cin>>L;
cout<<"Key in the frequency 'f'
";
cin>>f;

xc=1/(2*pi*f*C);
xl=2*pi*f*L;

if (option==1)
{z=sqrt(pow(R,2) +pow((xl-xc),2));
radian=atan((xl-xc)/R);
degree=(radian*180)/pi;
cout<<"
";
cout<<"The total impedance of RLC in series is Z = "<<z<<" ohms and "<<degree<<" degree
";}

if (option==2)
{yr=1/R; yc=1/xc; yl=1/xl;
z=1/(sqrt((pow(yr,2)+pow((yl-yc),2))));
radian=atan((yl-yc)/(1/R));
degree=(radian*180)/pi;
cout<<"
";
cout<<"The total impedance of RLC in parallel is Z = "<<z<<" ohms and "<<degree<<" degree
";}

else
{cout<<"Sorry the value is invalid
";}
cout<<"
";
cout<<"Do you wish to continue using the calculator?
";
cout<<"Enter 1 for yes 2 for no
";
cin>>conti;}

while (conti==1);
cout<<"

";

cout<<"Thank You For Using The Calculator
";

system("pause");
}

Comments

  • the reason for your problem is that you are taking input for the values of r c f before checking for the condition that option is either 1 or 2 or not .. so the compiler first asks the user for input and then checks option for validity .. so u could either put an extra if statement before taking input or take separate inputs in ur if statements

    for ex
    [code]#include
    #include
    using namespace std;
    void main()
    {
    double R, yr, L, yl, f, C, yc, option, conti, pi=3.142;
    double xc, xl, z, radian, degree;
    cout<<" RLC Impendance Calculator
    ";
    cout<<"
    ";
    do
    {
    cout<<"Choose a circuit

    ";
    cout<<"Press 1 for RLC SERIES circuit
    ";
    cout<<"Press 2 for RLC PARALLEL circuit

    ";
    cout<<"Key in your choice
    ";
    cin>>option;
    cout<<"
    ";
    if(option==1 || option==2)
    {cout<<"Key in the value of resistance 'R'
    ";
    cin>>R;
    cout<<"Key in the value of capacitance 'C'
    ";
    cin>>C;
    cout<<"Key in the value of inductance 'L'
    ";
    cin>>L;
    cout<<"Key in the frequency 'f'
    ";
    cin>>f;
    xc=1/(2*pi*f*C);
    xl=2*pi*f*L;
    }

    if (option==1)
    {z=sqrt(pow(R,2) +pow((xl-xc),2));
    radian=atan((xl-xc)/R);
    degree=(radian*180)/pi;
    cout<<"
    ";
    cout<<"The total impedance of RLC in series is Z = "<<z<<" ohms and "<<degree<<" degree
    ";}

    if (option==2)
    {yr=1/R; yc=1/xc; yl=1/xl;
    z=1/(sqrt((pow(yr,2)+pow((yl-yc),2))));
    radian=atan((yl-yc)/(1/R));
    degree=(radian*180)/pi;
    cout<<"
    ";
    cout<<"The total impedance of RLC in parallel is Z = "<<z<<" ohms and "<<degree<<" degree
    ";}

    else
    {cout<<"Sorry the value is invalid
    ";}
    cout<<"
    ";
    cout<<"Do you wish to continue using the calculator?
    ";
    cout<<"Enter 1 for yes 2 for no
    ";
    cin>>conti;}

    while (conti==1);
    cout<<"

    ";

    cout<<"Thank You For Using The Calculator
    ";
    system("pause");
    }[/code]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion