# The loop is ending before I want it to

Page 1 of 1

## 1 Replies - 1015 Views - Last Post: 20 August 2008 - 11:58 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=61270&amp;s=84223e9c4ab0851de458cbff441dcdd6&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 bolter

Reputation: 0
• 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

• C Surfing ∞

Reputation: 119
• Posts: 1,565
• 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;
}

```