Blocking While Loop Placement & Program Continuation Problem

Pages: 12
Hey guys, this is my first post I hope to learn some new things. This is my 3rd homework assignment in my first programming class. I attend the University of Iowa. This homework is a monster and i did a lot of it but i'm lost on when to use my blocking while loops and how to continue on with my problem once the user grading my program enters his values within the parameters given below. Here's my code and the equation I have. Any help would be appreciated greatly. Thanks !

The program should ask the user for the following parameters:
{L – thickness of the circuit board between 0.005m and 0.008m}
{Tside – temperature of the sides between 200C and 250C}
{type – type of material: material 1, material 2, material 3}
{t – the time after which you want to check the temperature, in seconds} {Tinitial – the initial temperature inside the board between 500C and 1000C}
{x – the spatial point at which you want to check the temperature, in meters}

The equation i have to solve is in my code but its only part of it. It's:
[Solve T for x=L/2 & x=0]
T(x,t) = T[side] + 2(T[initial - T[side] * Sum for n=1 to n=100 (of the equation below)

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
  #include <iostream>
#include <cmath>
#include <fstream>
using namespace std;
int main () {
    
    double Tside=25, Tinitial=50, Lthickness=0.005, typeM=0.00001, SpatialPoint=Lthickness/2, time=5;
    double Temp1, Temp2, CoolingOff;
    int number, n;
   
    
    
        cout << "Enter a number between 20 and 25";
        cin >> number;
        while (number >= 20 || number <= 25)
        {
            cout << "Error ! Enter number again !" << flush;
            cin >> number;
        }
        
        cout << "Enter a number between 50 and 100";
        cin >> number;
        while (number >= 50 || number <= 100)
        {
            cout << "Error ! Enter number again !" << flush;
            cin >> number;
        }
        
        cout << "Enter a number between 0.005 and 0.008";
        cin >> number;
        while (number >= 0.005 || number <= 0.008)
        {
            cout << "Error ! Enter number again !" << flush;
            cin >> number;
        }
        
        cout << "Enter a number between 1 and 50";
        cin >> number;
        while (number >= 1 || number <= 50)
        {
            cout << "Error ! Enter number again !" << flush;
            cin >> number;
        }


    cout << "Please enter the temperature of the sides." << endl;
    cin >> Tside;
    cout << "Please enter the initial temperature inside the board." << endl;
    cin >> Tinitial;
    cout << "Please enter the thickness of the circuit board." << endl;
    cin >> Lthickness;
    cout << "Please enter the type of material." << endl;
    cin >> typeM;
    cout << "Please enter the spatial point at which you want to check the temperature." << endl;
    cin >> SpatialPoint;
    cout << "Please enter the the after which you want to check the temperature (in seconds)" << endl;
    cin >> time;
    
    int Temp1Sum, Sum = 0;
    
    for( n=1; n <= 100; n++)
    {
        Temp1Sum = exp((-(n*M_PI)*(n*M_PI))*((typeM*(time))/(Lthickness*Lthickness)))*((1.0-cos(n*M_PI))/(n*M_PI))*sin((n*M_PI*(Lthickness/2))/(Lthickness));

    {    Sum += Temp1Sum;
    
    
    {   cout << Sum;
    
    }}} return 0;}
Last edited on
closed account (48T7M4Gy)
line 61 has an error.

line 15 has a logic error.

Tell me friend, have you run this program yourself? Most graders would give you a fail if you haven't and both these errors are desperately simple indicating you haven't. I mean this in the nicest way.
Last edited on
im not even 30% done with the program. It is due friday. There are 5 parts to this assignment. I know there are errors. I'm just wondering if my blocking while loops are correct and if they are and the user grading my program enters numbers within the parameter of the 4 variables, how do i continue running my program because it is a lot longer i have a lot more work to do. And no I dont take anything the wrong way. I need constructive criticism thank you
I also have this
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
#include <iostream>
#include <cmath>
#include <fstream>
using namespace std;
int main () {
    
    double Tside=25, Tinitial=50, Lthickness=0.008, typeM=0.00001, SpatialPoint=0, time=5;
    double const PI = acos(-1);

    double x;
    double Sum;

    
    int n, number;
    double Temp1;

        cout << "Enter a number between 20 and 25";
        cin >> number;
        while (number >= 20 || number <= 25)
        {
            cout << "Error ! Enter number again !" << flush;
            cin >> number;
        }
        
        cout << "Enter a number between 50 and 100";
        cin >> number;
        while (number >= 50 || number <= 100)
        {
            cout << "Error ! Enter number again !" << flush;
            cin >> number;
        }
        
        cout << "Enter a number between 0.005 and 0.008";
        cin >> number;
        while (number >= 0.005 || number <= 0.008)
        {
            cout << "Error ! Enter number again !" << flush;
            cin >> number;
        }
        
        cout << "Enter a number between 1 and 50";
        cin >> number;
        while (number >= 1 || number <= 50)
        {
            cout << "Error ! Enter number again !" << flush;
            cin >> number;
        }


  
    cout << "Pi = " << PI << endl;
    cout << Tside <<  endl;
    cout << Tinitial <<  endl;
    cout << Lthickness <<  endl;
    cout << typeM <<  endl;
    cout << SpatialPoint <<  endl;
    cout << "time = " <<time <<  endl;
    
    x = 0.002;  // this is a value that the user must enter

    SpatialPoint = Lthickness/2;
    
    
    for(n=1; n <= 100; n++)
    {
        Temp1 = exp((-(n*PI)*(n*PI))*(typeM*time)/(Lthickness*Lthickness))*((1.0-cos(n*PI))/(n*PI))*sin((n*PI*x)/Lthickness);

       // cout << "Temp1 = " <<  Temp1 << endl;
       // cout << "Sum = " <<  Sum << endl;

        Sum = Sum + Temp1;
    }
    
    cout << "Sum = " <<  Sum << endl;
    
    return 0;


}
Last edited on
I realize 61 had an error yes. I needed a ; after n=1 . It is just a rough draft i just had the question regarding my blocking while loops and it they will work and then to continue on with the program.
closed account (48T7M4Gy)
Well, I'm not sure what you mean by a 'blocking' while loop in the context of your program but all I can say is you have the logic of the first one wrong. As far as I can see it doesn't accept input in the range you display. The display prompt contradicts the program.

Why use number to capture input, why use flush?
ok so i have an equation which i posted its a long one. I need to solve for T(x,t) when x=L/2 and x=0 . T[side], T[initial], L & t(time have parameters). When i did my own hand example i used certain values. But I need my program to have the user pick his/her own number. If they dont pick the right number i need to have them pick it again. Once they pick the right numbers for the 4 variables, then i will continue my program to solve the equation which is the sum of a series * what i wrote in my first post
My teacher wanted us to use a blocking while loop. Maybe I'm saying it wrong :/. I'm so new to programming I don't want to come off as an idiot not explaining my issue clearly
Flush was something that i saw someone use online in their while loop like mine. Im not sure what it does though. I originally didn't have it
what would you use to capture input ? If for "L" i want the user to enter between .005 and .008 how do i get him to pick his own number. Do i use an .. int L; or something or each one individually -> int Tside, Tinitial, Lthickness, time;
then do my while loops. sorry so confused :/
closed account (48T7M4Gy)
I would just call it a while loop. But maybe the blocking aspect is that it blocks the user from going further until the input is within range , who knows.

If I was you I would call the variables their real name so you know which is which and you can plug them directly into the formula.

You need to run the program yourself and debug it. You'll see that you'll need to review your choice of and's & or's in your while conditions. Once you get that fixed in each case you can then get the inputs together with the formula and produce some output. I don't think that will be a huge job. Get one while loop working and the rest follow the same pattern.


ok thank you. So should go create a seperate xcode project and create one while loop then go back and change them ? because i looked at line 16 and it looks the same as every other while loop i wrote. I did notice though that when i started typing number xcode popped up a box asking me if it was my int number; .. then i clicked enter. But for the 'number's in the first part of my while loops while (number<=integer) xcode wouldn't recognize that number as my int... maybe that is why it didnt work
ok so i just did it and it worked but hold on heres the code. When i do enter a number between 20 and 25 it keeps saying error try again... i cant get it to work then leave loop
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <cmath>
#include <fstream>
using namespace std;
int main () {

    int number;
    
    
    
    cout << "Enter a number between 20 and 25";
    cin >> number;
    while (number >= 20 || number <= 25)
    {
        cout << "Error ! Enter number again !" << flush;
        cin >> number;
    }
    
    return 0;}
closed account (48T7M4Gy)
hint: there must be something wrong with the logic at line 13 for it to keep going around and around.

&& what are you going to do about it?
ugh lol... ok basically what it says is number must be greater than or equal to 20 OR number must be less than or equal to 25. I tried && meaning they both have to be true but it still didnt work. Do i have to do an if statement after my error message
i also tried this but it still didnt work.. at least once i learn how to do this after struggling so much i will always know how to do it forever !
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <cmath>
#include <fstream>
using namespace std;
int main () {

    int number;
    
    
    
    cout << "Enter a number between 20 and 25";
    cin >> number;
    while (number >= 25 || number <= 20)
    {
        cout << "Error ! Enter number again !";
        cin >> number;
    }
    

    return 0;
}
Last edited on
i now see what my problem is... number= 25 for all the values. Im at a loss though. Do i need a seperate variable for 21, 22, 23, 24 ,25 ?
closed account (48T7M4Gy)
no you won't need an if statement. To debug just record the values and response you get for number = 21,22,23 and 24 which are the only values in the range.
Does the output make sense?
I haven't learned about debugging :/ The output meaning what
now im getting a thread 1 breakpoint 1.1 and it says my int number; is like 187657383 ... I right clickled line 16 and added a condition sand debugger command but i dont know how to make it work.. i dont see why this is so complicated. I have my condition while (number >=20 || number <=25) why is it so much more complicated
Pages: 12