Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,661 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,181 people online right now. Registration is fast and FREE... Join Now!




The loop is ending before I want it to

 
Reply to this topicStart new topic

The loop is ending before I want it to

bolter
post 20 Aug, 2008 - 09:30 PM
Post #1


New D.I.C Head

*
Joined: 16 Aug, 2008
Posts: 2

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

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
User is offlineProfile CardPM

Go to the top of the page

AmitTheInfinity
post 20 Aug, 2008 - 10:58 PM
Post #2


C Surfing ∞

Group Icon
Joined: 25 Jan, 2007
Posts: 1,015



Thanked 34 times

Dream Kudos: 125
My Contributions


cpp

#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. smile.gif
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/23/08 05:40AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month