C++ help Please!!

Pages: 12
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
#include <iostream>
#include <string>
#include <sstream>
#include <cmath>
#include <iomanip>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
#define PI 3.14159
int main()
{
    int structure;
    cout << "Enter your 1 for Ibeam          ";
    cin >> structure;
    
    int num;
    double b1,b2,b3,d1,d2,d3,c2,I1,I2,I3,I,w,x,E,LO,theta(x),delta(x);
    string filename;
    char *outputfilename = "output.txt";
    
    ifstream inFile;
    ofstream outputfile(outputfilename);
    cout << "Please enter the name of the file you wish to open:  ";
    cin >> filename;
    inFile.open(filename.c_str());
    if (inFile.fail())
    {
       cout << "\nThe file named " << filename
            << " was not successfully opened"
            << "\n Please check that the file currently exists."
            <<endl;
       system("PAUSE");
       exit(1);
}
    cout << "\nThe file has been successfully opened for reading.\n";
    if (!outputfile){
    cout << "Error Opening Output File: " << outputfilename
           << " .\nCheck if file already exists. "<< endl;
    return 0 ;
}
    cout << "Output File Success fully Opened. " << endl;     
    
    inFile >> num;
    inFile >> b1;
    inFile >> b2;
    inFile >> b3;
    inFile >> d1;
    inFile >> d2;
    inFile >> d3;
    inFile >> w;
    inFile >> E;
    inFile >> LO;
    cout << "b1 from file:   " << b1<<endl;
    cout << "b2 from file:   " <<b2<<endl;
    cout <<"b3 from file:    " <<b3<<endl;
    cout <<"d1 from file:    " <<d1<<endl;
    cout <<"d2 from file:    " <<d2<<endl;
    cout <<"d3 from file:    " <<d3<<endl;
    cout <<"w from file:     " <<w<<endl;
    cout <<"E from file:     " <<E<<endl;
    cout <<"LO from file:    " <<LO<<endl;
    
    if (structure == 1)
    
    {
    c2= (((b1 * d1) * (d1 / 2)) + ((b2 * d2) * (d1 + (d2/2))) + ((b3 * d3) * (d1 + d2 + (d3/2)))) / ((b1 * d1) + (b2 * d2) + (b3 * d3));
    cout << "c2:    " << c2 << "  m\n";    
    I1= ((b1 * (pow(d1,3))) / 12) + (b1 * d1) * (c2 - (d1 / 2)) * (c2 - (d1 / 2));
    cout << "I1:    " << I1 << "  m\n";
    I2= (((b2 * (pow(d2,3))) / 12) + ((b2 * d2) * ((d1 + (d2 / 2) - c2) * (d1 + (d2 / 2) - c2))));
    cout << "I2:    " << I2 << "  m\n";
    I3= ((b3 * (pow(d3,3))) / 12) + (b3 * d3) * (pow((d1 + d2 + (d3/2) - c2),2));
    cout << "I3:    " << I3 << "  m\n";
    cout << "Sum of I:   " << I1 + I2 + I3  << "    m\n"; 
    theta= (((w * x) / (6 * E * ((b1 * (d1 * d1 * d1)) / 12) + (b1 * d1) * (c2 - (d1 / 2)) * (c2 - (d1 / 2)) + ((b2 * (d2 * d2 * d2)) / 12) + (b2 * d2) * ((d1 + (d2 / 2) - c2) * (d1 + (d2 / 2) - c2)) +((b3 * (d3 * d3 * d3)) / 12) + b3 * d3 * ((d1 + d2 + (d3/2) - c2) * (d1 + d2 + (d3/2) - c2))))) * (((3 * (LO * LO)) - (3 * LO * x) + (x * x)));
    cout << "theta(x):   " << theta << endl;
    
    outputfile << "I1 from file:    " <<I1<< "m\n";
    system("PAUSE");
    return 0;
}
    else if (structure>1)
    {
        cout <<"You did not type a valid number." <<endl;
        system("PAUSE");
        return 0;
    }
    system ("PAUSE");
    return 0;
}



Can anyone tell me why when I run the program it sets my I1 equal to my I2? Also it is giving an outrageous number for theta(x), any thoughts? I think for theta(x) it is telling me the character measurement, but idk how to fix it.

Thanks
Not sure aboot I1 and I2 but line 18 should be crashing the compilation because the variable 'x' is being used to initialize theta and delta without being initialized itself...
Can anyone tell me why when I run the program it sets my I1 equal to my I2?

because ((b1 * (pow(d1,3))) / 12) + (b1 * d1) * (c2 - (d1 / 2)) * (c2 - (d1 / 2)) happens to equal (((b2 * (pow(d2,3))) / 12) + ((b2 * d2) * ((d1 + (d2 / 2) - c2) * (d1 + (d2 / 2) - c2)))). If you work that out by hand, you'll see why I guess.

it is giving an outrageous number for theta(x)

I'm surprised that you don't have an error. You've declared double theta(x); which almost looks like a function call. Get rid of the (x)

double b1,b2,b3,d1,d2,d3,c2,I1,I2,I3,I,w,x,E,LO,theta,delta;
Okay that makes sense about the I1 and I2, but for theta my output screen says:

Enter your 1 for Ibeam 1
Please enter the name of the file you wish to open: input.txt

The file has been successfully opened for reading.
Output File Success fully Opened.
b1 from file: 5
b2 from file: 5
b3 from file: 5
d1 from file: 5
d2 from file: 5
d3 from file: 5
w from file: 5
E from file: 5
LO from file: 5
c2: 7.5 m
I1: 677.083 m
I2: 52.0833 m
I3: 677.083 m
Sum of I: 1406.25 m
theta(x): 1.#INF
Press any key to continue . . .
I changed theta(x) to theta as well
Nevermind I see that my x is undeclared. But for this project I have to say that x is equal to .1 * LO, .2 *LO, .3 * LO,..........., 1*LO so how would I input that into my code?
that means that you have a "divide by zero" somewhere in that code. I'll see if I can pinpoint it.
Nevermind I see that my x is undeclared. But for this project I have to say that x is equal to .1 * LO, .2 *LO, .3 * LO,..........., 1*LO so how would I input that into my code?
I was told wrong, your right about the "divide by zero".
Your X was also undeclared. I was just reducing this with your inputs and saw that.
1
2
3
4
5
6
7
theta= (((w * x) / (6 * E * ((b1 * (d1 * d1 * d1)) / 12) + (b1 * d1) * (c2  - (d1 / 2)) * (c2  - (d1 / 2)) + ((b2 * (d2 * d2 * d2)) / 12) + (b2 * d2) * ((d1 + (d2 / 2) - c2 ) * (d1 + (d2 / 2) - c2 )) +((b3 * (d3 * d3 * d3)) / 12) + b3 * d3 * ((d1 + d2 + (d3/2) - c2 ) * (d1 + d2 + (d3/2) - c2 ))))) * (((3 * (LO * LO)) - (3 * LO * x) + (x * x)));
theta= (((5 * x) / (6 * 5 * ((5  * (5  * 5  * 5 )) / 12) + (5  * 5 ) * (7.5 - (5  / 2)) * (7.5 - (5  / 2)) + ((5  * (5  * 5  * 5 )) / 12) + (5  * 5 ) * ((5  + (5  / 2) - 7.5) * (5  + (5  / 2) - 7.5)) +((5  * (5  * 5  * 5 )) / 12) + 5  * 5  * ((5  + 5  + (5 /2) - 7.5) * (5  + 5  + (5 /2) - 7.5))))) * (((3 * (5  * 5 )) - (3 * 5  * x) + (x * x)));
theta= (((5 * x) / (30 * ((5  * (125)) / 12) + (25) * (7.5 - (2.5)) * (7.5 - (2.5)) + ((5  * (125)) / 12) + (25) * ((5  + (2.5) - 7.5) * (5  + (2.5) - 7.5)) +((5  * (25)) / 12) + 25 * ((10 + (2.5) - 7.5) * (10 + (2.5) - 7.5))))) * (((3 * (25)) - (15 * x) + (x * x)));
theta= (((5 * x) / (30 * ((625) / 12) + (25) * (5) * (5) + ((625) / 12) + (25) * ((0) * (0)) +((625) / 12) + 25 * ((5) * (5))))) * (((625) - (15 * x) + (x * x)));
theta= (((5 * x) / (30 * (52.08333) + 625 + (52.08333) + 0 +(52.08333) + 25 * (25)))) * (((625) - (15 * x) + (x * x)));
theta= (((5 * x) / (1562.5 + 625 + (52.08333) + 0 +(52.08333) + 625))) * (((625) - (15 * x) + (x * x)));
theta= (((5 * x) / 2916.6667)) * (((625) - (15 * x) + (x * x)));


To set x to LO, just go x = LO at line 63.

If you are wanting a bunch of values for x, you'll need a bunch of inputs of LO, or maybe use an array or something. But that's ussually a more academic thing to do.
In this project there is just one LO value, im gonna post what I need

Distance x (m) 
0.0  L0
0.1  L0
0.2  L0
0.3  L0
.. .. ..
.. .. ..
0.9  L0
1.0  L0
Last edited on
So can I just assign different LO s like LO1 and so forth? and then just ask the user which LO they have?
In this case it's easiest to put this in a loop. The following will calculate and output theta for each L0 between 0 and 1 with increments of 0.1

1
2
3
4
5
for (L0 = 0.0; L0 <= 1.0; L0+=0.1)
{
    theta= (((w * x) / (6 * E * ((b1 * (d1 * d1 * d1)) / 12) + (b1 * d1) * (c2 - (d1 / 2)) * (c2 - (d1 / 2)) + ((b2 * (d2 * d2 * d2)) / 12) + (b2 * d2) * ((d1 + (d2 / 2) - c2) * (d1 + (d2 / 2) - c2)) +((b3 * (d3 * d3 * d3)) / 12) + b3 * d3 * ((d1 + d2 + (d3/2) - c2) * (d1 + d2 + (d3/2) - c2))))) * (((3 * (LO * LO)) - (3 * LO * x) + (x * x)));
    cout << "theta(" << L0 << "):   " << theta << endl;
}
Last edited on
Now I get `L0' undeclared (first use this function). I still have it as a double. Where exactly do I put that for statement?
Last edited on
1
2
3
4
5
for (double L0 = 0.0; L0 <= 1.0; L0+=0.1)
{
    theta= (((w * x) / (6 * E * ((b1 * (d1 * d1 * d1)) / 12) + (b1 * d1) * (c2 - (d1 / 2)) * (c2 - (d1 / 2)) + ((b2 * (d2 * d2 * d2)) / 12) + (b2 * d2) * ((d1 + (d2 / 2) - c2) * (d1 + (d2 / 2) - c2)) +((b3 * (d3 * d3 * d3)) / 12) + b3 * d3 * ((d1 + d2 + (d3/2) - c2) * (d1 + d2 + (d3/2) - c2))))) * (((3 * (LO * LO)) - (3 * LO * x) + (x * x)));
    cout << "theta(" << L0 << "):   " << theta << endl;
}


I may have gotten confused between LO and L0 though
Okay it still is giving me the 1.#INF though

Ill post what my code looks like now.

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
#include <iostream>
#include <string>
#include <sstream>
#include <cmath>
#include <iomanip>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
#define PI 3.14159
int main()
{
    cout <<"==============================="<<endl;
    cout << "Alexander M. Thompson"<<endl;
    cout << "HW #2"<<endl;
    cout << "9/5/2012"<<endl;
    cout <<"==============================="<<endl;
    int structure;
    cout << "Enter your 1 for Ibeam          ";
    cin >> structure;
    
    int num;
    double b1,b2,b3,d1,d2,d3,c2,I1,I2,I3,I,w,x,E,LO,theta,delta;
    string filename;
    char *outputfilename = "output.txt";
    
    ifstream inFile;
    ofstream outputfile(outputfilename);
    cout << "Please enter the name of the file you wish to open:  ";
    cin >> filename;
    inFile.open(filename.c_str());
    if (inFile.fail())
    {
       cout << "\nThe file named " << filename
            << " was not successfully opened"
            << "\n Please check that the file currently exists."
            <<endl;
       system("PAUSE");
       exit(1);
}
    cout << "\nThe file has been successfully opened for reading.\n";
    if (!outputfile){
    cout << "Error Opening Output File: " << outputfilename
           << " .\nCheck if file already exists. "<< endl;
    return 0 ;
}
    cout << "Output File Success fully Opened. " << endl;     
    
    inFile >> num;
    inFile >> b1;
    inFile >> b2;
    inFile >> b3;
    inFile >> d1;
    inFile >> d2;
    inFile >> d3;
    inFile >> w;
    inFile >> E;
    inFile >> LO;
    cout << "b1 from file:   " << b1<<endl;
    cout << "b2 from file:   " <<b2<<endl;
    cout <<"b3 from file:    " <<b3<<endl;
    cout <<"d1 from file:    " <<d1<<endl;
    cout <<"d2 from file:    " <<d2<<endl;
    cout <<"d3 from file:    " <<d3<<endl;
    cout <<"w from file:     " <<w<<endl;
    cout <<"E from file:     " <<E<<endl;
    cout <<"LO from file:    " <<LO<<endl;
    
    if (structure == 1)
    
    {
    c2= (((b1 * d1) * (d1 / 2)) + ((b2 * d2) * (d1 + (d2/2))) + ((b3 * d3) * (d1 + d2 + (d3/2)))) / ((b1 * d1) + (b2 * d2) + (b3 * d3));
    cout << "c2:    " << c2 << "  m\n";    
    I1= ((b1 * (pow(d1,3))) / 12) + (b1 * d1) * (c2 - (d1 / 2)) * (c2 - (d1 / 2));
    cout << "I1:    " << I1 << "  m\n";
    I2= (((b2 * (pow(d2,3))) / 12) + ((b2 * d2) * ((d1 + (d2 / 2) - c2) * (d1 + (d2 / 2) - c2))));
    cout << "I2:    " << I2 << "  m\n";
    I3= ((b3 * (pow(d3,3))) / 12) + (b3 * d3) * (pow((d1 + d2 + (d3/2) - c2),2));
    cout << "I3:    " << I3 << "  m\n";
    cout << "Sum of I:   " << I1 + I2 + I3  << "    m\n"; 
    for (double LO = 0.0; LO <= 1.0; LO+=0.1)
{
    theta= (((w * x) / (6 * E * ((b1 * (d1 * d1 * d1)) / 12) + (b1 * d1) * (c2 - (d1 / 2)) * (c2 - (d1 / 2)) + ((b2 * (d2 * d2 * d2)) / 12) + (b2 * d2) * ((d1 + (d2 / 2) - c2) * (d1 + (d2 / 2) - c2)) +((b3 * (d3 * d3 * d3)) / 12) + b3 * d3 * ((d1 + d2 + (d3/2) - c2) * (d1 + d2 + (d3/2) - c2))))) * (((3 * (LO * LO)) - (3 * LO * x) + (x * x)));
    cout << "theta(" << LO << "):   " << theta << endl;
}
    outputfile << "I1 from file:    " <<I1<< "m\n";
    system("PAUSE");
    return 0;
}
    else if (structure>1)
    {
        cout <<"You did not type a valid number." <<endl;
        system("PAUSE");
        return 0;
    }
    system ("PAUSE");
    return 0;
}
You still haven't initialized x.

I bet you meant to do 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <iostream>
#include <string>
#include <sstream>
#include <cmath>
#include <iomanip>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
#define PI 3.14159
int main()
{
    cout <<"==============================="<<endl;
    cout << "Alexander M. Thompson"<<endl;
    cout << "HW #2"<<endl;
    cout << "9/5/2012"<<endl;
    cout <<"==============================="<<endl;
    int structure;
    cout << "Enter your 1 for Ibeam          ";
    cin >> structure;
    
    int num;
    double b1,b2,b3,d1,d2,d3,c2,I1,I2,I3,I,w,x,E,LO,theta,delta;
    string filename;
    char *outputfilename = "output.txt";
    
    ifstream inFile;
    ofstream outputfile(outputfilename);
    cout << "Please enter the name of the file you wish to open:  ";
    cin >> filename;
    inFile.open(filename.c_str());
    if (inFile.fail())
    {
       cout << "\nThe file named " << filename
            << " was not successfully opened"
            << "\n Please check that the file currently exists."
            <<endl;
       system("PAUSE");
       exit(1);
}
    cout << "\nThe file has been successfully opened for reading.\n";
    if (!outputfile){
    cout << "Error Opening Output File: " << outputfilename
           << " .\nCheck if file already exists. "<< endl;
    return 0 ;
}
    cout << "Output File Success fully Opened. " << endl;     
    
    inFile >> num;
    inFile >> b1;
    inFile >> b2;
    inFile >> b3;
    inFile >> d1;
    inFile >> d2;
    inFile >> d3;
    inFile >> w;
    inFile >> E;
    inFile >> LO;
    cout << "b1 from file:   " << b1<<endl;
    cout << "b2 from file:   " <<b2<<endl;
    cout <<"b3 from file:    " <<b3<<endl;
    cout <<"d1 from file:    " <<d1<<endl;
    cout <<"d2 from file:    " <<d2<<endl;
    cout <<"d3 from file:    " <<d3<<endl;
    cout <<"w from file:     " <<w<<endl;
    cout <<"E from file:     " <<E<<endl;
    cout <<"LO from file:    " <<LO<<endl;
    
    if (structure == 1)
    
    {
    c2= (((b1 * d1) * (d1 / 2)) + ((b2 * d2) * (d1 + (d2/2))) + ((b3 * d3) * (d1 + d2 + (d3/2)))) / ((b1 * d1) + (b2 * d2) + (b3 * d3));
    cout << "c2:    " << c2 << "  m\n";    
    I1= ((b1 * (pow(d1,3))) / 12) + (b1 * d1) * (c2 - (d1 / 2)) * (c2 - (d1 / 2));
    cout << "I1:    " << I1 << "  m\n";
    I2= (((b2 * (pow(d2,3))) / 12) + ((b2 * d2) * ((d1 + (d2 / 2) - c2) * (d1 + (d2 / 2) - c2))));
    cout << "I2:    " << I2 << "  m\n";
    I3= ((b3 * (pow(d3,3))) / 12) + (b3 * d3) * (pow((d1 + d2 + (d3/2) - c2),2));
    cout << "I3:    " << I3 << "  m\n";
    cout << "Sum of I:   " << I1 + I2 + I3  << "    m\n"; 
    for (double x = 0.0; x <= LO; x +=0.1)
    {
        theta= (((w * x) / (6 * E * ((b1 * (d1 * d1 * d1)) / 12) + (b1 * d1) * (c2 - (d1 / 2)) * (c2 - (d1 / 2)) + ((b2 * (d2 * d2 * d2)) / 12) + (b2 * d2) * ((d1 + (d2 / 2) - c2) * (d1 + (d2 / 2) - c2)) +((b3 * (d3 * d3 * d3)) / 12) + b3 * d3 * ((d1 + d2 + (d3/2) - c2) * (d1 + d2 + (d3/2) - c2))))) * (((3 * (LO * LO)) - (3 * LO * x) + (x * x)));
        cout << "theta(" << LO << "):   " << theta << endl;
    }
    outputfile << "I1 from file:    " <<I1<< "m\n";
    system("PAUSE");
    return 0;
}
    else if (structure>1)
    {
        cout <<"You did not type a valid number." <<endl;
        system("PAUSE");
        return 0;
    }
    system ("PAUSE");
    return 0;
}
Last edited on
Okay that is giving me an answer for theta, but there are too many of them

input screen:


Enter your 1 for Ibeam          1
Please enter the name of the file you wish to open:  input.txt

The file has been successfully opened for reading.
Output File Success fully Opened.
b1 from file:   5
b2 from file:   5
b3 from file:    5
d1 from file:    5
d2 from file:    5
d3 from file:    5
w from file:     5
E from file:     5
LO from file:    5
c2:    7.5  m
I1:    677.083  m
I2:    52.0833  m
I3:    677.083  m
Sum of I:   1406.25    m
theta(5):   0
theta(5):   0.0126017
theta(5):   0.0246994
theta(5):   0.0363034
theta(5):   0.047424
theta(5):   0.0580714
theta(5):   0.068256
theta(5):   0.077988
theta(5):   0.0872777
theta(5):   0.0961354
theta(5):   0.104571
theta(5):   0.112596
theta(5):   0.120219
theta(5):   0.127452
theta(5):   0.134304
theta(5):   0.140786
theta(5):   0.146907
theta(5):   0.152679
theta(5):   0.158112
theta(5):   0.163215
theta(5):   0.168
theta(5):   0.172476
theta(5):   0.176654
theta(5):   0.180543
theta(5):   0.184155
theta(5):   0.1875
theta(5):   0.190587
theta(5):   0.193428
theta(5):   0.196032
theta(5):   0.19841
theta(5):   0.200571
theta(5):   0.202527
theta(5):   0.204288
theta(5):   0.205863
theta(5):   0.207264
theta(5):   0.2085
theta(5):   0.209582
theta(5):   0.210519
theta(5):   0.211323
theta(5):   0.212004
theta(5):   0.212571
theta(5):   0.213036
theta(5):   0.213408
theta(5):   0.213698
theta(5):   0.213915
theta(5):   0.214071
theta(5):   0.214176
theta(5):   0.214239
theta(5):   0.214272
theta(5):   0.214284
theta(5):   0.214286
Press any key to continue . . .
Last edited on
Theta outputs as u wrote it to...
LO from file:    5

Line 81 bounds 0<=x<= LO and increments at +0.1 each time from 0.
How many increments will that make for x ?
5/0.1 = 50 loops which means 50 cout outputs for theta
Last edited on
Okay so what do I have to change in my code to make it input only 10 outputs of theta, besides change LO?
Last edited on
Pages: 12