I have a finished working program. Question on final part please

i want to loop back to beginning of program to have user choose new values if my if statement proves false. How do i loop back if the user hasn't chosen the right parameters in my if statement ? I already created a while loop at the end of my program but i want that to be only after the chosen parameters were met. So that should be seperate

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <iostream>
#include <cmath>
#include <fstream>
using namespace std;
int main () {
    
    double Material1=0.00001, SpatialPoint=0, Tside, Tinitial, L, time;
    double const PI = acos(-1);
    
    double x1=0.0025, x2=0;
    double Sum = 0.0, T1 = 0.0, T2 = 0.0, CoolingOff1, CoolingOff2;
    
    
    int n;
    double Temp1Series;
    
    
    cout << "Please enter your T side value. " << "Enter a number between 20 and 25: ";
    cin >> Tside;
    while (Tside < 20 || Tside > 25)
    {
        cout << "Error ! Enter number again !: ";
        cin >> Tside;
    }
    
    cout << "Please enter your T initial value. " << "Enter a number between 50 and 100: ";
    cin >> Tinitial;
    while (Tinitial < 50 || Tinitial > 100)
    {
        cout << "Error ! Enter number again !: ";
        cin >> Tinitial;
    }
    
    cout << "Please enter the thickness of the circuit board (L). " << "Enter a number between 0.005 and 0.008: ";
    cin >> L;
    while (L < 0.005 || L > 0.008)
    {
        cout << "Error ! Enter number again !: ";
        cin >> L;
    }
    
    cout << "Please enter the time after which you want to check the temperature. " << "Enter a number between 1 and 50: ";
    cin >> time;
    while (time < 1 || time > 50)
    {
        cout << "Error ! Enter number again!: ";
        cin >> time;
    }
    
    cout << "Pi = " << PI << endl;
    cout << "Please enter the number of your type of material. " << "M1 = 0.00001, M2 = 0.00004 or M3 = 0.000006?: " << endl;
    cin >> Material1;
    cout << "Please enter 'x' the spatial point at which you want to check your temperature. Divide what you picked for L by 2. " << endl;
    cin >> x1;
    
    x2 = 0;
    SpatialPoint = L/2;
    
    
    for(n=1; n <= 100; n++)
    {
        Temp1Series = exp((-(n*PI)*(n*PI))*(Material1*time)/(L*L))*((1.0-cos(n*PI))/(n*PI))*sin((n*PI*x1)/L);
        
        cout << "Temp1Series = " <<  Temp1Series << endl;
        
        Sum = Sum + Temp1Series;
        
        T1 = Tside + ((2*(Tinitial - Tside))*(Sum));
        T2 = Tside + (2*(Tinitial - Tside))*0;
    }
    
    cout << "Sum = " <<  Sum << endl;
    cout << "The final temperature for T(L/2,t) = " << T1 << endl;
    cout << "The final temperature for T(0,t) = " << T2 << endl;
    
    
    CoolingOff1 = (T1 - Tside)/(Tinitial-Tside);
    CoolingOff2 = 0.735*(x1/L)*exp(-time);
    
    if (CoolingOff1>CoolingOff2) {
        cout << "Sorry, the chosen parameters have not met the specifications.";
    } else if(CoolingOff1<=CoolingOff2) {
        cout << "Great ! The chosen parameters are good !";
    }
    
    
    
    
    return 0;

Last edited on
I would really appreciate a push in the rightdirection on how to loop back to beggining of program if CoolingOff1>CoolingOff2. Thanks
Last edited on
Simply wrap the entire code with a

1
2
3
do
{
} while(1);


loop, and break when you done.
ok because at the end of my code i need to ask the user if they want to quit (y/n) which i just did. but that would be after the user's parameters had been bad. I want to loop to beggining right after it says "Sorry, the chosen parameters have not met the specifications." and at the end of the code. Does that make sense or am i wrong. Heres my code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <iostream>
#include <cmath>
#include <fstream>
using namespace std;
int main () {
    
    double Material1=0.00001, SpatialPoint=0, Tside, Tinitial, L, time;
    double const PI = acos(-1);
    
    double x1=0.0025, x2=0;
    double Sum = 0.0, T1 = 0.0, T2 = 0.0, CoolingOff1, CoolingOff2;
    
    
    int n;
    double Temp1Series;
    char again = 'Y';
    
    while (again == 'y' || again == 'Y')
    {
        //Get users values
    cout << "Please enter your T side value. " << "Enter a number between 20 and 25: ";
    cin >> Tside;
    while (Tside < 20 || Tside > 25)
    {
        cout << "Error ! Enter number again !: ";
        cin >> Tside;
    }
    
    cout << "Please enter your T initial value. " << "Enter a number between 50 and 100: ";
    cin >> Tinitial;
    while (Tinitial < 50 || Tinitial > 100)
    {
        cout << "Error ! Enter number again !: ";
        cin >> Tinitial;
    }
    
    cout << "Please enter the thickness of the circuit board (L). " << "Enter a number between 0.005 and 0.008: ";
    cin >> L;
    while (L < 0.005 || L > 0.008)
    {
        cout << "Error ! Enter number again !: ";
        cin >> L;
    }
    
    cout << "Please enter the time after which you want to check the temperature. " << "Enter a number between 1 and 50: ";
    cin >> time;
    while (time < 1 || time > 50)
    {
        cout << "Error ! Enter number again!: ";
        cin >> time;
    }
    
    cout << "Pi = " << PI << endl;
    cout << "Please enter the number of your type of material. " << "M1 = 0.00001, M2 = 0.00004 or M3 = 0.000006?: " << endl;
    cin >> Material1;
    cout << "Please enter 'x' the spatial point at which you want to check your temperature. Divide what you picked for L by 2. " << endl;
    cin >> x1;
    
    x2 = 0;
    SpatialPoint = L/2;
    
    
    for(n=1; n <= 100; n++)
    {
        Temp1Series = exp((-(n*PI)*(n*PI))*(Material1*time)/(L*L))*((1.0-cos(n*PI))/(n*PI))*sin((n*PI*x1)/L);
        
        cout << "Temp1Series = " <<  Temp1Series << endl;
        
        Sum = Sum + Temp1Series;
        
        T1 = Tside + ((2*(Tinitial - Tside))*(Sum));
        T2 = Tside + (2*(Tinitial - Tside))*0;
    }
    
    cout << "Sum = " <<  Sum << endl;
    cout << "The final temperature for T(L/2,t) = " << T1 << endl;
    cout << "The final temperature for T(0,t) = " << T2 << endl;
    
    
    CoolingOff1 = (T1 - Tside)/(Tinitial-Tside);
    CoolingOff2 = 0.735*(x1/L)*exp(-time);
    
    if (CoolingOff1>CoolingOff2) {
        cout << "Sorry, the chosen parameters have not met the specifications.";
    } else if(CoolingOff1<=CoolingOff2) {
        cout << "Great ! The chosen parameters are good !";
    }
    
        cout << "Would you like to perform another computation? (y/n) " ;
        cin >> again;
    }
    
    cout << "Have a great day ! Goodbye." << endl;
    
    return 0;

}
they should say different things. If.. "Sorry, the chosen parameters have not met the specifications." I want to say. pick new parameters. Then at the end of my code i have what i already want and have
can someone please help me ???
Try thiis
1
2
3
4
5
6
7
while(true){
  //some code
  if(/*you want to startover*/) continue;
  //another code ?
  if(/*you want to quit*/) break;
}
cout<<"Have a greatday ! Goodbye"<<endl;
im not sure what you mean Lendra. Can you enter my already written code into what you wrote so i have some perspective on where to start the while..if...if loop? is my if else statement in one of those or both or what? sorry im confused. thank you for the post though
Just add continue; inside if(CoolingOff1>CollingOff2) this "if" is belong to while(again=='y') rght ? So it'll start again from loop, and skip the question

Thatswhat you ask right ? I'm afraid I msunderstood something
so heres my final code. In my if else statement if the user gets the prompt "Sorry, the chosen parameters have not met the specifications. Please choose new ones", then i want it to automatically loop to beginning of program. If he gets the prompt "Great ! The chosen parameters have met the specifications !" Then my program should continue and go through the while loop i already created. So i wont have to change anything for that. Its just for the "Sorry.... etc" prompt that i want it to loop to beginning of program.
P.S. I am not allowed to use break or continue statements :/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <iostream>
#include <cmath>
#include <fstream>
using namespace std;
int main () {
    
    double Material1=0.00001, SpatialPoint=0, Tside, Tinitial, L, time;
    double const PI = acos(-1);
    
    double x1=0.0025, x2=0;
    double Sum1 = 0.0, Sum2 = 0.0, T1 = 0.0, T2 = 0.0, CoolingOff1, CoolingOff2;
    
    
    int n;
    double Temp1Series, Temp2Series;
    char again = 'Y';
    
    {   cout << " This program computes the change in temperature after a given time interval, at a given location and at a specified board thickness. " << endl;
    }

    
    while (again == 'y' || again == 'Y')
    {
        //Get users values
    cout << "Please enter your T side value. " << "Enter a number between 20 and 25: ";
    cin >> Tside;
    while (Tside < 20 || Tside > 25)
    {
        cout << "Error ! Enter number again !: ";
        cin >> Tside;
    }
    
    cout << "Please enter your T initial value. " << "Enter a number between 50 and 100: ";
    cin >> Tinitial;
    while (Tinitial < 50 || Tinitial > 100)
    {
        cout << "Error ! Enter number again !: ";
        cin >> Tinitial;
    }
    
    cout << "Please enter the thickness of the circuit board (L). " << "Enter a number between 0.005 and 0.008: ";
    cin >> L;
    while (L < 0.005 || L > 0.008)
    {
        cout << "Error ! Enter number again !: ";
        cin >> L;
    }
    
    cout << "Please enter the time after which you want to check the temperature. " << "Enter a number between 1 and 50: ";
    cin >> time;
    while (time < 1 || time > 50)
    {
        cout << "Error ! Enter number again!: ";
        cin >> time;
    }
    
    cout << "Pi = " << PI << endl;
    cout << "Please enter the number of your type of material. " << "M1 = 0.00001, M2 = 0.00004 or M3 = 0.000006?: " << endl;
    cin >> Material1;
    cout << "Please enter 'x' the spatial point at which you want to check your temperature. Divide what you picked for L by 2. " << endl;
    cin >> x1;
    
    x2 = 0;
    SpatialPoint = L/2;
    
    
   // Compute the sum of the series
        for(n=1; n <= 100; n++)
    {
        Temp1Series = exp((-(n*PI)*(n*PI))*(Material1*time)/(L*L))*((1.0-cos(n*PI))/(n*PI))*sin((n*PI*x1)/L);
        Temp2Series = exp((-(n*PI)*(n*PI))*(Material1*time)/(L*L))*((1.0-cos(n*PI))/(n*PI))*sin((n*PI*x2)/L);
       
        cout << "Temp1Series = " <<  Temp1Series << endl;
        cout << "Temp2Series = " << Temp2Series << endl;
        
        Sum1 = Sum1 + Temp1Series;
        Sum2 = Sum2 + Temp2Series;
        
        T1 = Tside + ((2*(Tinitial - Tside))*(Sum1));
        T2 = Tside + ((2*(Tinitial - Tside))*(Sum2));
    }
    
    cout << "Sum T(L/2,t) = " <<  Sum1 << endl;
    cout << "Sum T(0,t) = " << Sum2 << endl;
    cout << "The final temperature T(L/2,t) = " << T1 << endl;
    cout << "The final temperature T(0,t) = " << T2 << endl;
    
    
    CoolingOff1 = (T1 - Tside)/(Tinitial-Tside);
    CoolingOff2 = 0.735*(x1/L)*exp(-time);
    
    if (CoolingOff1>CoolingOff2) {
        cout << "Sorry, the chosen parameters have not met the specifications. Please choose new ones";
    } else if(CoolingOff1<=CoolingOff2) {
        cout << "Great ! The chosen parameters have met the specifications ! ";
    }
        {
        cout << "Would you like to perform another computation? (y/n) " ;
        cin >> again;
        }
        }
        
        cout << "Have a great day ! Goodbye." << endl;
    
    return 0;

}
anyone ? please ?
> Its just for the "Sorry.... etc" prompt that i want it to loop to beginning of program.

Use a jump statement to transfer control:

To loop back to beginning of the program ie. beginning of main():
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int main () {

    beginning_of_program: 
        
            // ...
            
            while (again == 'y' || again == 'Y')
            {
                
                // ...    
                
                if (CoolingOff1>CoolingOff2) {
                        
                    cout << "Sorry, the chosen parameters have not met the specifications. Please choose new ones\n";

                    goto beginning_of_program ; // jump statement
                } 
                
                // ...
                
            }

            cout << "Have a great day ! Goodbye." << endl;
}


To loop back to beginning of the outermost while loop:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int main () {

    // ...

    while (again == 'y' || again == 'Y')
    {
        beginning_of_while_loop:        
            // ...    

            if (CoolingOff1>CoolingOff2) {

                cout << "Sorry, the chosen parameters have not met the specifications. Please choose new ones\n";

                goto beginning_of_while_loop ; // jump statement with explicit label
                continue ; // jump statement with implicit label; same as above 
            } 

            // ...

    }

    cout << "Have a great day ! Goodbye." << endl;
}
ya man i would do that definately but i cannot use break, continue and in no way goto statements otherwise i totally would
> i cannot use break, continue and in no way goto statements

It's good that you recognise that break, continue and goto are birds of a feather -
three different forms of jump statements.

It is not so good that an irrational phobia of non-deprecated language features may lead to more convoluted code.
Spaghetti with nested loops within nested loops within nested loops ...

Something like this perhaps, if jump statements are to be avoided at any cost:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
int main () {

    double CoolingOff1 = 0 ;
    double CoolingOff2 = 0 ;

    do
    {
        // ...
        char again = 'y' ;
        while (again == 'y' || again == 'Y')
        {
                // ...

                // compute CoolingOff1, CoolingOff1

                if( CoolingOff1 > CoolingOff2 ) {

                    cout << "Sorry, the chosen parameters have not met the specifications. Please choose new ones\n";
                }

                else {

                    cout << "Great ! The chosen parameters have met the specifications !\n"
                         << "Would you like to perform another computation? (y/n): " ;
                    cin >> again;
                }
        }
    }
    while( CoolingOff1 > CoolingOff2 ) ;

    cout << "Have a great day ! Goodbye." << endl;
}
thanks JL sorry for getting back to you so late. This is exactly what i ended up doing ! Thanks !
Topic archived. No new replies allowed.