Small issue when i run my program. Advice appreciated

So i have my code and it runs. But when i run it and i enter all the values like im supposed to, it tells me my sum and my Temp1Series at least 50 times . How do i make it so it only posts it once. Thanks guys ! Here's 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
  #include <iostream>
#include <cmath>
#include <fstream>
using namespace std;
int main () {
    
    double Material1=0.00001, Material2=0.00004, Material3=0.000006, SpatialPoint=0, Tside, Tinitial, L, time;
    double const PI = acos(-1);
    
    double x1, x2;
    double Sum, T1, T2;
    
    
    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;
    
    x1 = 0.0025;   // this is a value that the user must enter
    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;
    
    
    }  cout << "Sum = " <<  Sum << endl;
    
    
    T1 = Tside + ((2*(Tinitial - Tside))*(Sum));
    T2 = Tside + (2*(Tinitial - Tside))*0;
    
    }
    cout << "The final temperature for T(L/2,t) = " << T1 << endl;
    cout << "The final temperature for T(0,t) = " << T2 << endl;

    
    return 0;}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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;
    
    
    }  cout << "Sum = " <<  Sum << endl;
    
    
    T1 = Tside + ((2*(Tinitial - Tside))*(Sum));
    T2 = Tside + (2*(Tinitial - Tside))*0;
    
    }


The "{" before Sum = Sum + Temp1Series; and "}" before cout << "Sum = " << Sum << endl; have no real effect on the code. They are just creating an block inside the for loop;
Move the cout << "Sum = " << Sum << endl; outside the "} (after line 75 in your code)
awesome thank you ! are my "{" and "}" in the right places? thats omething i always have a tough time on. My code now says to initialize my Temp1Series double and make it = 0.0 but i dont think that will ake my solved equation come out right.
if i move cout << sum = << etc to line 75 after } will that affect my T1 and T2? I need to calculate the sum then put that into T1 and T2. thanks
No it wont affect the sum if you DONT move line 8 but DO MOVE line 11 to 76, welcome.
what the heck.. when i ran my program now i get a long thread of random stuff, like my code dissappeared. uh oh
now this is my code. but it keeps making it into a thread
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
#include <iostream>
#include <cmath>
#include <fstream>
using namespace std;
int main () {
    
    double Material1=0.00001, Material2=0.00004, Material3=0.000006, SpatialPoint=0, Tside, Tinitial, L, time;
    double const PI = acos(-1);
    
    double x1, x2;
    double Sum, T1, T2;
    
    
    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;
    
    x1 = 0.0025;   // this is a value that the user must enter
    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;
    
    
        return 0;}}
This is the output from your program
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Please enter your T side value. Enter a number between 20 and 25: 20
Please enter your T initial value. Enter a number between 50 and 100: 55
Please enter the thickness of the circuit board (L). Enter a number between 0.005 and 0.008: .006
Please enter the time after which you want to check the temperature. Enter a number between 1 and 50: 5
Pi = 3.14159
Please enter the number of your type of material. M1 = 0.00001, M2 = 0.00004 or M3 = 0.000006?: 
.000006
Temp1Series = 0.000164776
Sum = 0.000164776
Temp1Series = 0
Sum = 0.000164776
Temp1Series = -1.06877e-33
Sum = 0.000164776
Temp1Series = -0
...
Temp1Series = -0
Sum = 0.000164776
The final temperature for T(L/2,t) = 20.0115
The final temperature for T(0,t) = 20


This is the output after the modification I suggested
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Please enter your T side value. Enter a number between 20 and 25: 21
Please enter your T initial value. Enter a number between 50 and 100: 55
Please enter the thickness of the circuit board (L). Enter a number between 0.005 and 0.008: .006
Please enter the time after which you want to check the temperature. Enter a number between 1 and 50: 34
Pi = 3.14159
Please enter the number of your type of material. M1 = 0.00001, M2 = 0.00004 or M3 = 0.000006?: 
.000006
Temp1Series = 3.16015e-25
Temp1Series = 0
Temp1Series = -3.75139e-220
Temp1Series = -0
Temp1Series = 0
...
Temp1Series = 0
Temp1Series = -0
Temp1Series = -0
Sum = 3.16015e-25
The final temperature for T(L/2,t) = 21
The final temperature for T(0,t) = 21


Basically the only difference is that sum is getting printed in every iteration of the for loop

The modified code looks like
1
2
3
4
5
6
7
8
9
10
11
12
13
   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;
Last edited on
awesome ! ok now it worked. idk why my code dissappeared into something ineligible. thank you codewalker. For variables: Sum, T1 & T2 Xcode says the variable may be uninitialized when used here. Is that a problem or will it not affect my program
In this case initialize sum to zero, but it is generally a good idea to initialize variables when you declare them
cool. thank you sir/mam ! :)
Topic archived. No new replies allowed.