Question:

C++ programming loops. need help please?

by  |  earlier

0 LIKES UnLike

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

 Tags:

   Report

4 ANSWERS


  1. I&#039;m not really knowledgeable on C++, but in Java I&#039;d use the main just to call another method that would be recursive, calling itself over and over again.

    Just to give you an idea:

    int main(void)

    {

    /*any message you&#039;d like to show once before starting*/

      calculate()

    }

    int calculate(void)

    {

    /*method implementation*/

    calculate()

    }

    Probably very wrong, but sorry, never got much further from &quot;Hello, World!&quot; in C++!

    Another option: you could declare a flag under main, set it to false, put your code inside a while (false) loop and, in a certain condition (maybe user input being == &quot;exit&quot;) set the flag to true, allowing the program to go ahead and return 0.

    Sorry, but I&#039;m not able to really do the implementation in C++, or else I&#039;d surely do it for you, but I hope that this at least gives you an idea...


  2. Hello Bolter,

    There are two ways to do this, both involve functions.  

    First way is a simple way, and involves recursion.  In C++, a program is executed my a main function, so you can just stick an if clause at the end, asking whether or not the user wants to continue or not, if they do call the main function again (this is recursion), otherwise return 0 and end the program. (NOTE: Tracing recursion is tricky, generally dont stick anything below the main function call other than the if-else clause.  Look recursion.)

    this assumes return 0; so you can get rid of it and stick this snipplet at the end of your code.

    cout &lt;&lt; &quot;\n\tWould you like to continue?&quot;;

    char cont[3];

    //get() is a function, the first paramter takes

    //variable, second paramter tells how many

    //characters to take in

    cin.get(cont, 3);

    if(cont[0] == int(&#039;y&#039;)) main();

    else return 0;  

    this snipplet basically asks the user at the end whether they want to continue or not. the function will take the first character of their response, if it is a y, call a recursion (call itself) if it is anything else, end it.

    The second method involves design planned from stage one.  This calls for multiple functions for each job that you have to simplify the code.  If you did this, you can easily loop a function.  If you want to learn more about how to do this shoot me and email.

    I hope that helps.

  3. the cleanest way is to declare a new function

    void doStuff()

    { }

    move all the code currently inside main() into doStuff() then in main() simply write

    while(1)

        doStuff();

    you might want to make it a bit more elaborate and give doStuff a return code so that you can make the while conditional so that the user is able to exit the program.

  4. Hi,

    U declare a char variable, say c.

    put ur code within a while loop like this

    After line &quot;cout &lt;&lt; &quot;SERIES RLC CIRCUIT CALCULATION \n&quot;;

    &quot;

    u write

    While(c!=&#039;y&#039;)

    {

    keep ur code here

    }

    Keep the closing brace of while loop just before return 0;

    u change getc(stdin) to c=getch(); it should look like this

    cout &lt;&lt; &quot;Exit (y/n) &quot;;

    c=getch();

    } //closing brace of while loop

    return 0;

    } //closing brace of main

    if user enters y then programme will terminate else it will not. It will again go up.

Question Stats

Latest activity: earlier.
This question has 4 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.