简体   繁体   中英

How to use do-while loop continuously with a new values for the variables involved (re-input by user)?

This code solves for the root of a cubic function. Setting aside any mathematical concepts, SPECIFICALLY I want to do my DO-WHILE LOOP THREE TIMES (for it's cubic). Consequently, the user will input a guess of the root, v THREE TIMES.

Now the problem is after the FIRST DO-WHILE LOOP, the values of the variables v, seemed to be NOT OVERWRITTEN by the re-input of the user for the SECOND and THIRD DO-WHILE LOOP.

在此处输入图片说明

Please help guys!

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

const double R = 0.082054;

double f(double a,double b,double P,double T,double v);        
double f(double a,double b,double P,double T,double v)
{
    double q =P*pow(v,3)-(P*b+R*T)*pow(v,2)+a*v-a*b;
    return q;
}
double fPrime(double a,double b,double P,double T,double v);
double fPrime(double a,double b,double P,double T,double v)
{
    double s =3*P*pow(v,2)-2*(P*b+R*T)*pow(v,1)+a;
    return s;
}

int main()
{
    double a,b,P,T,Tc;
    cin >> a;
    cin >> b;
    cin >> P;
    cin >> T;

    double v,v1=0,fv,fv1,N,e;                                   //FIRST DO-WHILE
    int iteration = 0;
    cout << "\nUse initial guess for molal volume, v: ";
    cin >> v;
    cout << "\nUse maximum number of iterations, N: ";
    cin >> N;
    cout << "\nUse tolerance, e: ";
    cin >> e;
    do
    {
        v = v1;
        fv = f(a,b,P,T,v);
        fv1 = fPrime(a,b,P,T,v);
        if (fv1==0)
        {
           cout << "Math Error";
           break;
        }
        v1 = v-(fv/fv1);
        cout << iteration+1 << setw(12) << v << setw(16) << v1 << setw(16) << abs(v1-v) << endl;
        iteration++;
    }
    while(iteration<N && abs(v1-v)>e);
    cout << "The approximate root/solution is " << v1 << endl;

    iteration = 0;                                              //SECOND DO-WHILE
    cout << "\nUse initial guess for molal volume, v: ";
    cin >> v;
    cout << "\nUse maximum number of iterations, N: ";
    cin >> N;
    cout << "\nUse tolerance, e: ";
    cin >> e;
    do
    {
        v = v1;
        fv = f(a,b,P,T,v);
        fv1 = fPrime(a,b,P,T,v);
        if (fv1==0)
        {
           cout << "Math Error";
           break;
        }
        v1 = v-(fv/fv1);
        cout << iteration+1 << setw(12) << v << setw(16) << v1 << setw(16) << abs(v1-v) << endl;
        iteration++;
    }
    while(iteration<N && abs(v1-v)>e);
    cout << "The approximate root/solution is " << v1 << endl;

    iteration = 0;                                              //THIRD DO-WHILE
    cout << "\nUse initial guess for molal volume, v: ";
    cin >> v;
    cout << "\nUse maximum number of iterations, N: ";
    cin >> N;
    cout << "\nUse tolerance, e: ";
    cin >> e;
    do
    {
        v = v1;
        fv = f(a,b,P,T,v);
        fv1 = fPrime(a,b,P,T,v);
        if (fv1==0)
        {
           cout << "Math Error";
           break;
        }
        v1 = v-(fv/fv1);
        cout << iteration+1 << setw(12) << v << setw(16) << v1 << setw(16) << abs(v1-v) << endl;
        iteration++;
    }
    while(iteration<N && abs(v1-v)>e);
    cout << "The approximate root/solution is " << v1 << endl;

return 0;
}

It seems to me that your line 'cin>>v;' is pointless in your code, because every time you are reading v, you immediately afterwards assign the value of v1 to v with 'v=v1' . So your initial guess is lost. Moreover, since the exit condition for your second do-while is the same as the exit condition for your first do-while, the second do-while should only execute once and the value of v after that execution should equal v1 = v-fv/fv1, so you will never get a new second value for v distinct from the first value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM