1. ## Looping Question

Hi

I am working on a RLC Circuit sim for C++ and I cant get a loop to work, I want to increase frequency by 0.1 , and restart the While loop, until Frequency is less than MaxFreq . Does anyone have any idea on how to implement this???

Code:
```#include<stdafx.h>
#include <iostream>
#include <cmath>

using namespace std;
double Resistance;
double Inductance;
double Capacitance;
double SupplyVoltage;
double MinFreq;
double MaxFreq;
double Frequency;
double Current;
double Impedance;
double InductiveReactance;
double CapacitiveInductance;
double InductorVoltage;
double CapacitorVoltage;
double ResistorVoltage;
double VoltageRatio;
double NaturalFrequency;
double Fn;
double Pi;

class RLC
{
public :

void attributes ()
{
std::cout<<"RLC Series Circuit Simulation \n";
std::cout<<"Enter the values for the circuit components \n";
std::cout<<"Enter the Resistance [Ohms] ->";
std::cin>>Resistance; std::cout<<"\n";
std::cout<<"Enter the inducance [H]->";
std::cin>>Inductance; std::cout<<"\n";
std::cout<<"Enter the Capactiance [F]->";
std::cin>>Capacitance; std::cout<<"\n";
std::cout<< endl ;
std::cout<< "******************************* \n";
std::cout<< endl ;
std::cout<< "Enter the operating conditions : \n" ;
std::cout<< "Enter the supply voltage [V] -> " ;
std::cin>> SupplyVoltage ; std::cout<<"\n";
std::cout<< "Enter the minimum supply frequency [Hz] -> " ;
std::cin>> MinFreq ;std::cout<<"\n";
std::cout<< "Enter the Maximum Supply Frequency [Hz] -> " ;
std::cin>> MaxFreq ; std::cout<<"\n";
}
void NaturalFreq()
{
Pi=3.141592654;
NaturalFrequency=1/(2*Pi*sqrt(Capacitance*Inductance));
}
void InductiveReactanceXL()
{
InductiveReactance=(2*Pi*Frequency*Inductance);
}
void CapacitiveInductanceXC()
{
CapacitiveInductance=1/(2*Pi*Frequency*Capacitance);
}
void ImpedenceZ()
{
Impedance=sqrt((Resistance*Resistance)+((InductiveReactance-CapacitiveInductance)*(InductiveReactance-CapacitiveInductance)));
}

void CurrentI()
{
Current=SupplyVoltage/Impedance;
}
void VoltageVR()
{
ResistorVoltage=Current*Resistance;
}
void VoltageVC()
{
CapacitorVoltage=Current*CapacitiveInductance;
}
void VoltageVL()
{
InductorVoltage=Current*InductiveReactance;
}
void VoltageRatioVOVR()
{
VoltageRatio = ResistorVoltage/SupplyVoltage;
}
void OutputTest()
{
std::cout<<"Voltage Ratio is!!:";
std::cout<<VoltageRatio ;
std::cout<<"[V]";
}

};

int main()
{

RLC console;
console.attributes();

RLC Calculations ;

Calculations.NaturalFreq ();
Frequency = MinFreq ; //Setting Frequency to minimum
while (Frequency <= MaxFreq)
{
Calculations.InductiveReactanceXL ();
Calculations.CapacitiveInductanceXC ();
Calculations.ImpedenceZ();
Calculations.CurrentI();
Calculations.VoltageVR();
Calculations.VoltageVC();
Calculations.VoltageVL();
Calculations.VoltageRatioVOVR();
Frequency=Frequency+0.1;
}

RLC Output;
Output.OutputTest();

return 0;
}```

Ewan :-)

2. Like this?
Code:
```for ( ; Frequency < MaxFreq; Frequency += 0.1 )
{
// do stuff
}```
You said you wanted to loop "until" its "less than", but you probably mean you want to "loop while" its less than, right? Also, in your code your using "<=" which isnt "less than". Besides that, my suggestion and your code is equivalent. Because of that, Im wondering what the actual problem is. Is your loop not working properly right now or something?

Also, I think the common practice is to start (non constant) variables, functions, etc with a lower case letter, so "MaxFreq" would be "maxFreq", etc. But of course that sort of thing is subjective--however there are recommendations and best practices.

EDIT: Also, when doing the calculations, make sure to force the numbers to be interpreted as doubles, like
Code:
`NaturalFrequency=1/(2*Pi*sqrt(Capacitance*Inductance));`
would become
Code:
`NaturalFrequency=(double) 1.0/(2.0*Pi*sqrt(Capacitance*Inductance));`

EDIT: Also, when doing the calculations, make sure to force the numbers to be interpreted as doubles, like
Code:
`NaturalFrequency=1/(2*Pi*sqrt(Capacitance*Inductance));`
would become
Code:
`NaturalFrequency=(double) 1.0/(2.0*Pi*sqrt(Capacitance*Inductance));`
Strange suggestion, because you seem to be taking advantage of automatic promotion of integers to doubles when they are combined with doubles. But the expression is ALREADY combining with double -- Pi is a double, and sqrt() returns a double. So changing the integral constants to floats is redundant.

4. OK! Then he needs to ignore my suggestions.

OK! Then he needs to ignore my suggestions.
Well, it's never bad to be specific about types, it's just redundant in this particular case. I'd probably write it the same way. My point is just that the code is correct as currently written.

6. Gotcha.

His question just seemed a little strange to me, or at least I felt that I didnt understand fully what he was asking. I think he was asking how to do a loop which he described. However, it seemed that from the code, the required loop was already implemented. So I thought maybe he knows he has written the loop, but for some reason its not "working". This thought popped into my head after I had replied, so I edited it to add that maybe its not "working" because of some (implicit) casting error.

I was unaware that it "upcasted" the "int"s. I thought the compiler would interpret the entire expression to be the type of the first variable in the expression, so in this case an "int". But I guess doing this would give a "possible loss of precision" warning/error (depending on compiler flags, I believe) when later downcasting the floats to int. Anyways, good to know, thanks.

7. You're not getting any useful replies because none of us understand your question.
A class isn't doing anything useful in that code, and having a ton of globals is just a mess.

You don't seem to understand Object Oriented Programming, so you may as well just program procedurally.
Come to think of it though, it almost look like you'd you're progamming as if you're using a functional programming language, or even a relational one such as Prolog.

8. Originally Posted by iMalc
You're not getting any useful replies because none of us understand your question.
A class isn't doing anything useful in that code, and having a ton of globals is just a mess.

You don't seem to understand Object Oriented Programming, so you may as well just program procedurally.
Come to think of it though, it almost look like you'd you're progamming as if you're using a functional programming language, or even a relational one such as Prolog.
I am aware of the class, but for some reason the assignment specifies that you must use only a single class for everything I dont know why, so I just did that to keep the lecturer happy !!!

I am used to using Matlab before C++ hence why I am used to having it set up like a functional layout.

The concept that I do not understand is how to set up the loop so it repeats itself until Frequency is less than Max Frequency . When the statement becomes false, the program should output some data and then terminate.

9. It's bugging me to death and irrelevant but if you have "using namespace std;" then you don't need the "std::" before calling functions like cout.

10. Originally Posted by ewandougie
I am aware of the class, but for some reason the assignment specifies that you must use only a single class for everything I dont know why, so I just did that to keep the lecturer happy !!!
But you're not using it for everything. You've got a lot of globals outside of that class. Because you don't see the point of it, you're using it in a pointless way. If you understood why it was useful then you would most likely be making good use of it.

I am used to using Matlab before C++ hence why I am used to having it set up like a functional layout.
Yes but that wont help you in C++. You're just writing out equations. In C++ your program doesn't just say what a function is, it must say how to calculate something. Variables are not just things that need to exist in order for you to write out an equation. Whilst your program may calculate some things correctly, it's like pushing a round peg through a square hole. Sure if you make your peg out of rubber then you can probably force it through, but that's just not the sensible thing to do. You need to learn how to write a proper C++ program so that you can make a square peg that'll slide through easily.

The concept that I do not understand is how to set up the loop so it repeats itself until Frequency is less than Max Frequency . When the statement becomes false, the program should output some data and then terminate.
But you're starting at min frequency. Surely that is already less than max frequency! Why would it need to loop at all then?
It would be like me asking you to come closer until you can read this.