1 Replies - 701 Views - Last Post: 20 August 2008 - 11:58 PM Rate Topic: -----

#1 bolter  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 16-August 08

The loop is ending before I want it to

Posted 20 August 2008 - 10:30 PM

ive finished most of the code for the following program, here's the code:

#include <iostream>
#include <cmath>

using namespace std;

int main(void)
{
double X, Z, angle, f, R, L, C;
const double PI = 4.0 * atan(1.0); 
cout << "SERIES RLC CIRCUIT CALCULATION \n";

cout << "Please enter values for frequency, resistance, inductance, and capacitance. \n";
cout << "This program will calculate the impedance and the phase angle of the circuit. \n";

do
{
cout << "Enter the value of the frequency in hertz: ";
cin >> f; 
if (f < 0)
{
cout << "Frequency must be non-negative \n";
}
else if (f == 0)
{
cout << "Zero frequency entered. Program terminated. \n";
cout << "Goodbye. \n";
return 0; 
}
}
while (f < 0); 
do
{
cout << "Enter the value of the resistor in ohms: ";
cin >> R; 

if (R < 0)
{
cout << "Resistance must be non-negative. \n"; 
} 
}
while (R < 0);

do
{
cout << "Enter the value of the inductor in henrys: ";
cin >> L;

if (L < 0)
{
cout << "Inductance must be non-negative. \n";
}
}
while (L < 0);

do
{
cout << "Enter the value of the capacitor in farads: ";
cin >> C;

if (C < 0)
{
cout << "Capacitance must be positive. \n";
}
}
while (C < 0);

cout << "\n"; 

X = (2*PI*f*L) - (1/ (2*PI*f*C));
 
Z = sqrt((R*R) + (X*X)); 

angle = (atan(X/R))*(180/PI); 

cout << "The impedance of this series RLC circuit is " << Z << " ohms \n"; 
cout << "The phase angle is " << angle << " degrees \n\n"; 


fflush(stdin);
getc(stdin);

return 0; 
}



everything calculates properly but my problem is that i want it to continue to loop after the user finishes making a calculation. so after it displays the impedance and phase angle it should go back to where it asks the user to enter the frequency. what should i do and where should i put in a extra command?

thaNks

Is This A Good Question/Topic? 0
  • +

Replies To: The loop is ending before I want it to

#2 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 119
  • View blog
  • Posts: 1,563
  • Joined: 25-January 07

Re: The loop is ending before I want it to

Posted 20 August 2008 - 11:58 PM

#include <iostream>
#include <cmath>

using namespace std;

int main(void)
{
double X, Z, angle, f, R, L, C;
const double PI = 4.0 * atan(1.0);
cout << "SERIES RLC CIRCUIT CALCULATION \n";

while(1)
{
cout << "Please enter values for frequency, resistance, inductance, and capacitance. \n";
cout << "This program will calculate the impedance and the phase angle of the circuit. \n";

do
{
cout << "Enter the value of the frequency in hertz: ";
cin >> f;
if (f < 0)
{
cout << "Frequency must be non-negative \n";
}
else if (f == 0)
{
cout << "Zero frequency entered. Program terminated. \n";
cout << "Goodbye. \n";
return 0;
}
}
while (f < 0);
do
{
cout << "Enter the value of the resistor in ohms: ";
cin >> R;

if (R < 0)
{
cout << "Resistance must be non-negative. \n";
}
}
while (R < 0);

do
{
cout << "Enter the value of the inductor in henrys: ";
cin >> L;

if (L < 0)
{
cout << "Inductance must be non-negative. \n";
}
}
while (L < 0);

do
{
cout << "Enter the value of the capacitor in farads: ";
cin >> C;

if (C < 0)
{
cout << "Capacitance must be positive. \n";
}
}
while (C < 0);

cout << "\n";

X = (2*PI*f*L) - (1/ (2*PI*f*C));

Z = sqrt((R*R) + (X*X));

angle = (atan(X/R))*(180/PI);

cout << "The impedance of this series RLC circuit is " << Z << " ohms \n";
cout << "The phase angle is " << angle << " degrees \n\n";
}

fflush(stdin);
getc(stdin);

return 0;
}



Your program terminates only if frequency value entered is zero. I hope this will help you. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1